Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.29/5 (4 votes)
See more:
help me to write a code that modify pixel color in an image.
Posted
Comments
Sandeep Mewara 16-Feb-11 2:08am    
What kind of help? What have you tried so far?
Sergey Alexandrovich Kryukov 16-Feb-11 2:43am    
Is it really that hard to just look at the help?
--SA

Hi,

you should have a look at Christian Graus' series of articles about bitmap manipulation in C#. This shows everything that you need to know:
Image Processing for Dummies with C# and GDI+ Part 1 - Per Pixel Filters[^]

or for a very simple way to set a pixel in a pictureBox:
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.SetPixel(5, 5, Color.White);
pictureBox1.Image = (Image)bmp;

Please note that GetPixel and SetPixel is very slow. You should have a look at Christian's article and see how he solves this issue using unsafe code.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 2:43am    
Good enough, 5,
--SA
P.S. Better mention: it's slow.
JF2015 16-Feb-11 2:44am    
You are right - I'll add this to my answer. That's why I mentioned CG's articles too.
Sergey Alexandrovich Kryukov 16-Feb-11 15:12pm    
Thank you. I just added my own answer: fist, I noted System.Drawing of Forms is not a tag, so in principle it could be WFP, secondly, I added a reference on fast operation with System.Drawing, please see.
--SA
The answer by JF2015 is correct, buy only about System.Drawing.

As I pointed out, GetPixel/SetPixel is too slow for any
The fast operation requires lock bit technique and Marshal, see http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^].

For WPF, fast operations are much easier; see the class System.Windows.Media.Imaging.BitmapSource and its derived classes.

—SA
 
Share this answer
 
Comments
Olivier Levrey 16-Feb-11 15:28pm    
Voted 5. Nobody should use GetPixel or SetPixel (unless the image is very small maybe...)
Sergey Alexandrovich Kryukov 16-Feb-11 19:41pm    
You're right. I came across rare cases when you use maybe a big picture but need to real only one "reference" picture from just one special location.

Thank you, Olivier,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900