GetPixel from Graphics object
-
Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks
-
Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks
The solution is here: http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=330183[^] I've put it in a helperclass so it's easier to use it:
public class PixelGetterHelperclass
{
//code gotten at:
//http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=330183\[DllImport("gdi32.dll")\] private static extern uint GetPixel(IntPtr hDC, int XPos, int YPos); \[DllImport("gdi32.dll")\] private static extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData); \[DllImport("gdi32.dll")\] private static extern bool DeleteDC(IntPtr DC); private static byte GetRValue(uint color) { return (byte)color; } private static byte GetGValue(uint color) { return ((byte)(((short)(color)) >> 8)); } private static byte GetBValue(uint color) { return ((byte)((color)>>16)); } private static byte GetAValue(uint color) { return ((byte)((color)>>24)); } public static Color GetColorFromPoint(Point p) { IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero); uint colorref = GetPixel(displayDC, p.X, p.Y); DeleteDC(displayDC); return Color.FromArgb( GetRValue(colorref), GetGValue(colorref), GetBValue(colorref)); }
}
I've made a codesample for you to download here: http://sweetsilence.dk/codeprojectprojects/CodeProjectSample_ColorOfParticularPixel/CodeProjectSample_ColorOfParticularPixel.zip[^] Kind regards - Jakob Three kinds of people in the world: - Those who can count.. - Those who can't!
-
The solution is here: http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=330183[^] I've put it in a helperclass so it's easier to use it:
public class PixelGetterHelperclass
{
//code gotten at:
//http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=330183\[DllImport("gdi32.dll")\] private static extern uint GetPixel(IntPtr hDC, int XPos, int YPos); \[DllImport("gdi32.dll")\] private static extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData); \[DllImport("gdi32.dll")\] private static extern bool DeleteDC(IntPtr DC); private static byte GetRValue(uint color) { return (byte)color; } private static byte GetGValue(uint color) { return ((byte)(((short)(color)) >> 8)); } private static byte GetBValue(uint color) { return ((byte)((color)>>16)); } private static byte GetAValue(uint color) { return ((byte)((color)>>24)); } public static Color GetColorFromPoint(Point p) { IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero); uint colorref = GetPixel(displayDC, p.X, p.Y); DeleteDC(displayDC); return Color.FromArgb( GetRValue(colorref), GetGValue(colorref), GetBValue(colorref)); }
}
I've made a codesample for you to download here: http://sweetsilence.dk/codeprojectprojects/CodeProjectSample_ColorOfParticularPixel/CodeProjectSample_ColorOfParticularPixel.zip[^] Kind regards - Jakob Three kinds of people in the world: - Those who can count.. - Those who can't!
But what if there is a window over the panel or form at X,Y what color would it return is it the color of the panel or the window over the panel. I tried the same thing before and the results were not as expected. It always returned the color of the top most form, thus to ensure that u get the correct color may be set the TopMost property of the Form TRUE; this is not the best way to do it but u prevent it from getting the color of unwanted windows.
-
Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks
I don't think there is way to get this working in general, because it interferes with how the Graphics class works. It is merely a wrapper around a target handling graphical input. This could be a bitmap, a panel (like in your case), a printer and much more. As you can image it would be near to impossible to get a pixel color from a graphics which sends all its input to a printer. Please explain a little more what you are trying to do. Probably there is another way to achieve it. For example I could assume that the Pnael is self drawn by you. If yes than you should double buffer the painting into a bitmap and in the end paint that bitmap onto the panel. Then you could use the buffered bitmap to receive your color (and moreover only regenerate the bitmap when you now the panel has changed).