tags:

views:

162

answers:

2

The only way I found so far is System.Drawing.Bitmap.GetPixel(), but Microsoft has warnings for System.Drawing that are making me wonder if this is the "old way" to do it. Are there any alternatives?


Here's what Microsoft says about the System.Drawing namespace. I also noticed that the System.Drawing assembly was not automatically added to the references when I created a new WPF project.

System.Drawing Namespace

The System.Drawing namespace provides access to GDI+ basic graphics functionality. More advanced functionality is provided in the System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.

The Graphics class provides methods for drawing to the display device. Classes such as Rectangle and Point encapsulate GDI+ primitives. The Pen class is used to draw lines and curves, while classes derived from the abstract class Brush are used to fill the interiors of shapes.

Caution

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

- http://msdn.microsoft.com/en-us/library/system.drawing.aspx

+2  A: 

You can use this code

    public Color GetPixel(BitmapSource bitmap, int x, int y)
    {
        Debug.Assert(bitmap != null);
        Debug.Assert(x >= 0);
        Debug.Assert(y >= 0);
        Debug.Assert(x < bitmap.PixelWidth);
        Debug.Assert(y < bitmap.PixelHeight);
        Debug.Assert(bitmap.Format.BitsPerPixel >= 24);

        CroppedBitmap cb = new CroppedBitmap(bitmap, new Int32Rect(x, y, 1, 1));
        byte[] pixel = new byte[bitmap.Format.BitsPerPixel / 8];
        cb.CopyPixels(pixel, bitmap.Format.BitsPerPixel / 8, 0);
        return Color.FromRgb(pixel[2], pixel[1], pixel[0]);
    }
Rikker Serg
+1  A: 

See this question and the top answer for a good explanation. But to answer your question, there is nothing wrong or "old" with using the System.Drawing.Bitmap.GetPixel() method.

Tim Trout