目前我发现的唯一方法是 System.Drawing.Bitmap.GetPixel(),但是 Microsoft 对 System.Drawing 有警告,这让我想知道这是否是做这件事的“老办法”。有没有其他选择?
这是 Microsoft 对 System.Drawing 命名空间的说明。我还注意到,当我创建一个新的 WPF 项目时,System.Drawing 程序集没有自动添加到引用中。
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
请您参考如下方法:
您可以使用此代码
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]);
}
