Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GetPixel from Graphics object

GetPixel from Graphics object

Scheduled Pinned Locked Moved C#
graphicshelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fouad_kayali
    wrote on last edited by
    #1

    Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks

    J R 2 Replies Last reply
    0
    • F Fouad_kayali

      Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks

      J Offline
      J Offline
      Jakob Farian Krarup
      wrote on last edited by
      #2

      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!

      L 1 Reply Last reply
      0
      • J Jakob Farian Krarup

        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!

        L Offline
        L Offline
        Leyu
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Fouad_kayali

          Hi, I am trying to get the color of particular pixel of a panel object, or Graphicx object... can you help me urgently thanks

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups