שתף באמצעות


How to use getpixel and setpixel?

Question

Tuesday, May 18, 2010 12:43 AM

Ok i have a icon maker program. i have two pictureboxes pic1 and pic2 and a open image button. i need a code that will if the pic1.image color has any red in it, it will take that red and turn it into the desired color. I know you can use getpixel and setpixel but i dont know how to use them so if you could give me a code and a little info on how it works that would be great. Thanks a lot -civilwarrock

All replies (13)

Tuesday, May 18, 2010 1:44 AM ✅Answered | 1 vote

You see the image is red i need it to just change the red to blue no other color is this possible? Thanks -civilwarrock

Yes of course it is.

See these two nested FOR NEXT loops.>>

    Dim img As Image = Image.FromFile(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\ExampleFile.jpg")

    Dim bmp As Bitmap = CType(img, Bitmap)

    For x As Integer = 0 To bmp.Width - 1
      For y As Integer = 0 To bmp.Height - 1
        If bmp.GetPixel(x, y) = Color.Red Then
          bmp.SetPixel(x, y, Color.Blue)
        End If
      Next
    Next

    bmp.Save(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\ChangedPicture.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Regards,

John

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Tuesday, May 18, 2010 1:15 AM

Try this link on how to use getpixel and set pixel http://www.vb-helper.com/howto_net_image_compare.html. You should be able to adapt the code to your need

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Tuesday, May 18, 2010 1:21 AM | 1 vote

Hi cilvilwarrock ,

1) Create a Bitmap from the image.

2) Use GetPixel and SetPixel on the Bitmap.

3) Set the Image or BackGroundImage to your Bitmap.

 

But how many shades of RED are you thinking of looking for with your code?

 

 Dim img As Image = Image.FromFile(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\ExampleFile.jpg")

 PictureBox1.BackgroundImage = img
 PictureBox1.BackgroundImageLayout = ImageLayout.Zoom

 Dim bmp As Bitmap = CType(img, Bitmap)

 Dim myColor As Color
 'Get the color of the Pixel at coordinates 20,30
 myColor = bmp.GetPixel(20, 30)

 'Set the pixel at 20,30 to BLUE.>>
 bmp.SetPixel(20, 30, Color.Blue)

 PictureBox1.BackgroundImage = bmp

 

 

Regards,

John

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Tuesday, May 18, 2010 1:36 AM

You see the image is red i need it to just change the red to blue no other color is this possible? Thanks -civilwarrock


Tuesday, May 18, 2010 2:11 AM

I have a problem i tryd the code and all it would do is change the image to its self not blue? I wonder why it is doing this? Any help. Thanks -civilwarrock


Tuesday, May 18, 2010 2:36 AM

Doug gave you a forum search link that contains threads with sample code and explanations in your other similar thread.


Tuesday, May 18, 2010 2:47 AM

Ok i have a code were you import a image into a picturebox and if the image contains any red the red will trun transparent. So here is what i have.

Dim img As Image = picturebox1.image
    Dim bmp As Bitmap = CType(img, Bitmap)

    For x As Integer = 0 To bmp.Width - 1
      For y As Integer = 0 To bmp.Height - 1
        If bmp.GetPixel(x, y) = Color.Red Then
          bmp.SetPixel(x, y, Color.Transparent)

         picturebox2.image = bmp
        End If
      Next
    Next

but the problem i am having is that it things there is no red in the image but there is. I even checked the colors to make sure they were exact. So is there a another code that if there is any red in the image the red with turn transparent like the code but ine that works. I know i put some other posts about this but this is the final post for this question so please do not tell me that i have other posts cause i know i just am starting fresh. Thanks -civilwarrock
  


Tuesday, May 18, 2010 2:49 AM

I have a problem i tryd the code and all it would do is change the image to its self not blue? I wonder why it is doing this? Any help. Thanks -civilwarrock

Hi again civilwarrock ,

1) Are you after a SOLID blue image ?

Or

2) Just changing the red pixels of an image to blue ones?

My code was to change the RED pixels to BLUE ones.

Or

3) Are you after giving an image a blue tint like this image of a DALEK eye view of Matt Smith as "THE DOCTOR "? >>

 

I can not think of any more possibilities. ;-)

 

Regards,

John

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Tuesday, May 18, 2010 3:23 AM | 1 vote

Don't use Color.Red.  Use theactual ARGB value that you are trying to match: 

If bmp.GetPixel(x, y) = Color.FromArgb(255, 255, 0, 0) Then

However, you are overwriting the picturebox image on every pixel replacement - this will take forever.  Update the PictureBox2 image only when the pixels have been replaced - at th endo of the Sub.


Tuesday, May 18, 2010 3:35 AM

Well here is the code i used.

 

Dim img As

Image = PictureBox1.Image

 

Dim bmp As Bitmap = CType

(img, Bitmap)

 

 

 

 

 

For x As Integer = 0 To

bmp.Width - 1

 

For y As Integer = 0 To

bmp.Height - 1

 

If bmp.GetPixel(x, y) = Color.Red Then

bmp.SetPixel(x, y, Color.Blue)

PictureBox2.Image = bmp

 

End If

PictureBox2.Image = bmp

 

 

Next

 

Next

Also the image i am using is black with red dots every ware. Now i dont know exacly what you used but if you could tell me that would be great and maybe i did something wrong in the code, but who knows. Thanks -civilwarrock


Tuesday, May 18, 2010 3:36 AM

Could the red pixels be semi transparent?Bill Gates look out!


Tuesday, May 18, 2010 4:29 AM

Doug gave you a forum search link that contains threads with sample code and explanations in your other similar thread.

 This is problem on this MSDN forum, most people dont even bother to make any effort on their own to solve problem, even if there is link to exact answer to the problem. I dont blame them, there are two reasons for that, 1) No MSDN policy that requires any poster to show some code before getting any code in the forum. 2) Most forum members will just dump code in the forum for the OP which turns the forum to answer outlet.

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Tuesday, May 18, 2010 4:50 AM | 1 vote

How many threads will this question take for you to get the answer? Posting thread on the same issue and abandoned it to post another will not solve your problem.

Other threads

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/1cc4a211-5ef8-45b5-9977-6b5d77f24b71/

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/dc51d94e-9a77-4eae-af4a-95173bf9b6e7

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.