Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
Hey guys, I was wondering if anybody could help me out...
I'm taking only my second delve into c# and I've been trying to get my hed round this for a few days and would really appriciate the help
Basically I need to be able to read and draw individual pixels from an image. The overall goal would be to have a loop where say every 5 seconds:
->Select 300 random pixels from the image
-> check if each random pixel is black or white
-> assign value to that pixel if white
-> colour all chosen pixels in red
-> clear and start again...
I know there is a method to call a pixel from its x,y pos., but i'm useless at c# at the moment and I don't know how to impliment it (emgu cv libaries...I have them running)
If anybody could help me with the syntax to access individual pixels, or how to acess the Image<TColor,TDepth>.Data property (using emgu) i'd really appriciate it
cheers for looking
Posted 21-Mar-11 12:50pm
Edited 29-Sep-11 8:06am
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

While this reply is late I thought best to include the answer in case any other fall across this question.
Lets assume you have read your image in as a colour image:
Image<bgr,Byte> My_Image = new Image<bgr,Byte>("filename_string");
To access each individual pixel of My_Image you call the .Data property but lets not forget that a Bgr image has 3 matrices representing Red, Green, Blue
byte Red_val = My_Image.Data[y,x,0];
byte Green_val = My_Image.Data[y,x,1];
byte Blue_val = My_Image.Data[y,x,2];
Now to check if this pixel is white "Red_Val", "Green_val" and "Blue_Val" will be equal to 255.
Now to set a pixel to a colour EMGU has made the method slighter simpler then setting the red green and blue spectrum's individually you can simply use:
My_Image[i,j] = new Bgr(Color.Red);

Now if you are using greyscale images you obviously can't assign the colour red but its worth noting that for this image type there is only 1 matrix representing it's values therefore to access its data you would use:
byte Gray_val = My_Image.Data[y,x,0];
So putting it all together you would be after something like this:
Timer timer = new Timer();
Image<bgr,byte> My_Image;
Image<bgr,byte> My_Image_todrawon;
Random xy_rand =  new Random();
public FormWithTimer()
    My_Image = new Image<bgr,byte>("filename_string");
    timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
    timer.Interval = (3000);              // Timer will tick every 3 seconds
    timer.Enabled = true;                       // Enable the timer
    timer.Start();                              // Start the timer (Make sure your Images area assigned before you start this);

void timer_Tick(object sender, EventArgs e)
    //Clear all your positions by makeing a copy image to draw on else your change 
    //will be permanent until you read the image in again
    My_Image_todrawon = My_Image.Copy();
    for(int i = 0; i< 300; i++)
        int ran_x_pos = xy_rand.Next(My_Image.Width);
        int ran_y_pos = xy_rand.Next(My_Image.Height); //limit there values to within image boundaries

        byte Red_val = My_Image.Data[ran_y_pos,ran_x_pos,0];
        byte Green_val = My_Image.Data[ran_y_pos,ran_x_pos,1];
        byte Blue_val = My_Image.Data[ran_y_pos,ran_x_pos,2];
        if(Red_val == 255 && Green_val == 255 && Blue_val == 255) //See if White
            //Mark the position within the image in red
            My_Image[i,j] = new Bgr(Color.Red);
            //Do any other operations such as record position assign values etc

This should help get you started in whatever you application may be.
RaviRanjankr at 20-Dec-11 5:40am
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

The fastest way in C# to deal with pixels it to use pointer to bitmap:
The code snippet is:
public int DoSomeStuffWithPixels(ref Bitmap b)
	BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
	System.IntPtr Scan0 = bmData.Scan0;
	int x = 4;
        int y = 70;
	int w=b.Size.Width;
		uint* p = (uint*)(void*)Scan0;
		//write pixel with coordinates (4;70)
		//ff - opacity, 11 - red, 22 - green, 33 - blue
		p[y * w + x] = (uint)(0xff112233);
		//reading pixel data from point with coordinates (4;70)
		uint dataRead = (uint)(p[y * w + x]);
	return 0;
Good luck!
P.S. Suddenly I have seen that I have duplicated --SA post.) Anyhow, hope it will be useful code snippet.
Viktor Signaievskyi
Marcus Kramer at 19-Dec-11 16:38pm
Your answer may be a little late for the OP, but I like it and have made note of it in my personal archive for future reference. +5.
Viktor Signaievskyi at 19-Dec-11 16:53pm
Marcus, thank you!)
RaviRanjankr at 20-Dec-11 5:39am
Nice answer, My 5+
Viktor Signaievskyi at 20-Dec-11 17:09pm
Thank you, RaviRanjankr!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is the easiest way but slow:
System.Drawing.Bitmap oBitmap=new Bitmap("MyFile.bmp");
System.Drawing.Color color= oBitmap.GetPixel(Y,X);
oBitmap.SetPixel(X, Y, color);
The faster way might be using Windows API functions.
Hope this can help you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

With System.Drawing, the faster way of accessing bits is System.Drawing.Bitmap.LockBits. See[^] for complete help and a code sample.
System.Drawing.Bitmap.GetPixel/SetPixel method are too slow and are note used ofter; good enough if you need to assess 1-3 pixels Smile | :) .
With WPF, use System.Windows.Media.Imaging.WriteableBitmap, see all other classes derived from System.Windows.Media.ImageSource.
Member 7773377 at 21-Mar-11 20:37pm
hey...cheers guys, but i mu st be a numbskull...i can't seem to compile the msdn link above, it compiles but nofiles open, there is some suspicious text (���) in the code, otherwise i'm still stumped! pretty sure the path is correct n just pasted the code could i go wrong?
SAKryukov at 21-Mar-11 23:39pm
What code are you talking about? Please don't use textspeak, if your want to be understood (or even any attention at all), this is considered impolite. Please, full punctuation, capitalization and correct spelling... --SA

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

  Print Answers RSS
Your Filters
0 Sergey Alexandrovich Kryukov 355
1 Abhinav S 333
2 OriginalGriff 330
3 sanket saxena 305
4 CPallini 165
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107

Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 19 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid