Grayscale Saving problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Omelskitot
    New Member
    • Aug 2012
    • 10

    Grayscale Saving problem

    I have a problem in saving grayscale picture from picture box i have this code for grayscale
    Code:
    Private Sub CMDGRAY_Click()
    
    
        Dim Color As Long
        Dim NewColor As Long
        Picture2.Picture = LoadPicture(CommonDialog1.FileName)
        Picture2.ScaleMode = vbPixels
        X = Picture2.ScaleWidth
        Y = Picture2.ScaleHeight
        
        For i = 0 To Y - 1
            For j = 0 To X - 1
               Color = (Picture2.Point(j, i) / (vbWhite / 255))
                NewColor = Color * (vbWhite / 255)
    
                Picture2.PSet (j, i), NewColor
               
            Next
        Next
    Picture2.ScaleMode = vbPixels
    End Sub
    And this is for my save
    Code:
    Private Sub mnuSave_Click()
    CommonDialog1.Filter = "Jpeg Files(*.jpg)|*.jpg"
    CommonDialog1.ShowSave
    If CommonDialog1.FileName <> Empty Then
    SavePicture Picture2.Picture, CommonDialog1.FileName
    End If
    End Sub
    when i save the picture it seems that the grayscale doesnt apply. And also when i move to other forms and return to the form where the picturebox is located the grayscale picture return to its original picture
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    The problem is in the code to change to gray: the picture in the picturebox is not changed !
    Add a image in the form and add the code after the gray change=
    Code:
    Image1.Picture = Picture2.Picture
    and you will see that the picture in the picturebox (what you are saving) is still in color!!

    Comment

    • Guido Geurs
      Recognized Expert Contributor
      • Oct 2009
      • 767

      #3
      See help vb:
      "PSet = Sets a point on an object to a specified color"
      With your code you are drawing a POINT on top of the picture in the picturebox with a color-value = the grayvalue of the underlying pixel of the picture in the picturebox!
      You have to change the color of the pixel in the picture in the picturebox !

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        To be honest, I don't understand why Guido has a problem with Pset. That's the correct statement (well, one of a few) to set the colour of a point (pixel) in a picture.

        If you change the picture, switch away, come back and it reverts to the old look, then your problem is almost certainly related to the setting of the picture's Autoredraw property.

        I've always found this a bit of a (no pun intended) gray area. But as a first attempt, try setting Picture2.AutoRe draw = True before starting your For loops.

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          Even if Autoredraw is set to True, it will not change the way of how Pset is working.

          The Picture2.Pictur e is a background in the picturebox like you can put a picture in the background of a form.

          The command Pset DRAWS a new gray point on top of the background = not in the background but in an "image" in the picturebox.

          If you analise the structure of a picturebox than you will see a picture and an image element.

          In the "picture" element is the background and in the "image" element is a copy of the background and are the elements drawn like a point with Pset.

          To get the gray pixels in the Picture you have to transfer the pixels from the image to the picture with:

          Code:
          Picture2.Picture = Picture2.Image
          now the picture is changed and you can save it.

          Attached is my testcode.
          Attached Files

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Oh yeah, the old .Image -vs- .Picture - sorry, my bad.

            It's been a while.

            One thing I do remember about that, it likes to chop off anything outside the visible area when you move .Image. Can't recall how you get around that.

            Comment

            • Omelskitot
              New Member
              • Aug 2012
              • 10

              #7
              Thanks alot. now ill have to study how to trace the color black in the pictures and store it in the array. im making a sketch/illustrating machine. only black and white pictures only thats why im using grayscale. after that trace the black color in the pixel then locate where the pixel is store it in the array [x,y] i have corresponding function for x and y. cause my machine also uses x and y axis

              Comment

              • Guido Geurs
                Recognized Expert Contributor
                • Oct 2009
                • 767

                #8
                A little rectification:
                No need to transfer the image to the picture element: you can also directly print the image with:
                Code:
                   SavePicture Picture2.Image, App.Path & "\test__" & Replace(Replace(Now, "/", "-"), ":", "+") & ".jpg"

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  I don't recommend using a "+" in a file name. It could confuse the operating system since that character is used in DOS commands to concatenate files.

                  Comment

                  Working...