Jun 20
It’s hard to believe the comprehensive .NET framework would omit such obvious functions as GetPixel and SetPixel from its Drawing library. Fortunately, we can access the GDI functions using Interop, as shown below. Notice the conversion required between the COLORREF integer used by the GDI methods and the Color structure used by our static .NET methods.
using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; [DllImport( "user32.dll" )] static extern IntPtr GetDC( IntPtr hWnd ); [DllImport( "user32.dll" )] static extern int ReleaseDC( IntPtr hWnd, IntPtr hDC ); [DllImport( "gdi32.dll" )] static extern int GetPixel( IntPtr hDC, int x, int y ); [DllImport( "gdi32.dll" )] static extern int SetPixel( IntPtr hDC, int x, int y, int color ); static public Color GetPixel( Control control, int x, int y ) { Color color = Color.Empty; if (control != null) { IntPtr hDC = GetDC( control.Handle ); int colorRef = GetPixel( hDC, x, y ); color = Color.FromArgb( (int)(colorRef & 0x000000FF), (int)(colorRef & 0x0000FF00) >> 8, (int)(colorRef & 0x00FF0000) >> 16 ); ReleaseDC( control.Handle, hDC ); } return color; } static public void SetPixel( Control control, int x, int y, Color color ) { if (control != null) { IntPtr hDC = GetDC( control.Handle ); int argb = color.ToArgb(); int colorRef = (int)((argb & 0x00FF0000) >> 16) | (int)(argb & 0x0000FF00) | (int)((argb & 0x000000FF) << 16); SetPixel( hDC, x, y, colorRef ); ReleaseDC( control.Handle, hDC ); } }
NOTE: These functions will not return the correct color if the specified control uses an alpha channel. In that case, you will have to create a Bitmap from the control, then use the Bitmap’s GetPixel method.
Are you serious? It’s Bitmap.GetPixel and Bitmap.SetPixel
Alan, you are not reading my article closely. My code is to change the color of a pixel on a CONTROL, not on a bitmap. And the very last line of my article says:
“In that case, you will have to create a Bitmap from the control, then use the Bitmap’s GetPixel method.”
this worked fine for my application… thanks…
timm
In support of Alan I have to say that I am highlighy disappointed, you failed to write an article that doea not take reader’s stupidity into account.
Seriously tim, where is you catch( e IdiotReader ) handler?
On each appearance of “DllImport” i get an error. It says: type- or namespace wasnt found. What can i do ?
regards
– VampireSilence
@VampireSilence: Make sure you have this:
using System.Runtime.InteropServices;
Thank you, this is very cognitive!
For a Windows Mobile (Compact Framework) I did as follows:
[DllImport(“CoreDll.dll”)]
static extern int GetPixel(IntPtr hDC, int x, int y);
static public Color GetPixelColor(IntPtr hDC, int x, int y)
{
Color color = Color.Empty;
int colorRef = GetPixel(hDC, x, y);
color = Color.FromArgb(
(int)(colorRef & 0x000000FF),
(int)(colorRef & 0x0000FF00) >> 8,
(int)(colorRef & 0x00FF0000) >> 16);
return color;
}
…
brsh = new SolidBrush(GetPixelColor(e.Graphics.GetHdc(), 0, 0));
…
Hi ! Can you help me how to get color of any pixel (at specific position) of any part of monitor while my apllication will be minimized. Thank you
Has anyone found a better way of doing this? I can’t believe it’s not very simple.
hi all, i have error for calling bitmap.getpixel(x,y) when using thread;
how to convert bitmap to control ;
reply soon plz.
PictureBox is a Control that is nothing but an image. You change it’s image property which is of type Image. Image is an abstract class that Bitmap inherits from.
myPictureBox.Image = yourBitmap
i have a byte array and i want an image on my gui. Can u help me in this.
height = 08
weight= 08
and data in hex string is this.
183C7EFFFF7E3C18
Now i want an image on gui is there any way to create this.If it is then rply urgently.
thank u for your solution
the methods speed are too slow is there any solution to increase the speed?