在 WPF 应用程序中使用 C# 确定位图中像素的颜色
到目前为止,我发现的唯一方法是 System.Drawing.Bitmap.GetPixel(),但 Microsoft 对 System.Drawing 有警告,这让我想知道这是否是“老方法”来做到这一点。还有其他选择吗?
以下是 Microsoft 关于 System.Drawing 命名空间的说法。我还注意到,当我创建新的 WPF 项目时,System.Drawing 程序集不会自动添加到引用中。
System.Drawing 命名空间
System.Drawing 命名空间提供对 GDI+ 基本图形功能的访问。 System.Drawing.Drawing2D、System.Drawing.Imaging 和 System.Drawing.Text 命名空间提供了更高级的功能。
Graphics 类提供了在显示设备上绘图的方法。诸如 Rectangle 和 Point 之类的类封装了 GDI+ 原语。 Pen 类用于绘制直线和曲线,而从抽象类 Brush 派生的类用于填充形状的内部。
注意
不支持在 Windows 或 ASP.NET 服务中使用 System.Drawing 命名空间中的类。尝试在这些应用程序类型之一中使用这些类可能会产生意外的问题,例如服务性能下降和运行时异常。
- http://msdn.microsoft.com/en-us/library /system.drawing.aspx
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此代码
You can use this code
请参阅此问题和最佳答案以获得良好的解释。但要回答您的问题,使用 System.Drawing.Bitmap.GetPixel() 方法没有任何错误或“旧”。
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.