Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I made

C#
private void FormMario_Shown(object sender, EventArgs e)
        {
            g = CreateGraphics();
        }


and then I want:

g.getPixel(x,y);

how to do that?
Posted

This isn't possible from the Graphics object itself. However, if the Graphics is bound to a Bitmap, you can use the GetPixel method of the Bitmap:
C#
Color c = bmp.GetPixel(x, y);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-15 14:36pm    
You see, we don't know if the inquirer use any bitmap. What if there is no any bitmaps? I would approach it in a different way, by asking about the purpose of all this activity. I suspect the whole thing makes no sense.
—SA
I found this somewhere so I used the object of the form as control

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace OSOL_NAMESPACE
{
    class ClassSetGetPixel
    {


        [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);
            }
        }


    }
}




This is better for GetPixel:
static Color GetPixel(Point p)
      {
          using (var bitmap = new Bitmap(1, 1))
          {
              using (var graphics = Graphics.FromImage(bitmap))
              {
                  graphics.CopyFromScreen(p, new Point(0, 0), new Size(1, 1));
              }
              return bitmap.GetPixel(0, 0);
          }
      }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-May-15 14:35pm    
I was about to comment "you formally accepted the worst answer of all", but later noticed that you accepted your own answer. Well, this way you can accept anything. I'll call it "cheating".

I would start with: why using "CreateGraphics" at all? What do you want to achieve?

—SA
Implements Master Yoda 26-Mar-18 2:35am    
I find it silly when a supposedly experienced programmer questions an ops motive for asking a question... perhaps they answered their own question void of any acceptable alternative or viable direction? Help and direction is the scope or main idea here at the Code Project and not passing judgments and certainly not insulting another. Not offering an alternative or helpful advise to the opp places your comments in question.

The opp's answer is reasonable to start with and provides a starting point to solving their problem. Due to an absence of usage context an alternative is not possible.
john1990_1 27-Mar-20 0:38am    
I want to set and get pixels with System.Drawing.Graphics not Bitmap, to set and get the colors of individual pixels on my form, I used it now and it works awesomely!!! EDIT: Found a better way and added it to the answer.

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