【问题标题】:What reference should I add to fix GetPixel error?我应该添加什么参考来修复 GetPixel 错误?
【发布时间】:2014-01-28 01:24:57
【问题描述】:

在我尝试编写一些代码时,我收到了这个错误:

错误 1 ​​'System.Drawing.Image' 不包含 'GetPixel' 并且没有扩展方法 'GetPixel' 接受第一个 可以找到“System.Drawing.Image”类型的参数(你是 缺少 using 指令或程序集引用?)

有人知道我需要做什么吗?

//Create a Image-Object on which we can draw
Image bmp = new Bitmap(100, 100);
//Create the Graphics-Object to paint on the Bitmap
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawString(randomString, myFont, new SolidBrush(Color.Black), new PointF(0, 0));
pictureBox1.Image = bmp;
bmp.Save(@"CAPTCHA.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

var backgroundPixels = 0;

for (int x = 0; x < bmp.Width; x++)
  for (int y = 0; y < bmp.Height; y++)
    if (bmp.GetPixel(x, y) == Color.White)
      backgroundPixels++;

【问题讨论】:

  • 你应该分享你的代码。
  • 你是怎么得到这个错误的?除非显示特定代码,否则它可能太宽泛了

标签: c#


【解决方案1】:

GetPixel 是 Bitmap 类的方法,而不是 Image 类的方法。您可以通过向构造函数提供 Image 来实例化 Bitmap。问题不在于您的 using 指令,而在于您使用了错误的类。

【讨论】:

  • 你能告诉我你对解决这个问题有什么建议吗?
  • 是的,我建议在您的循环之外,从您的 Image 对象实例化一个 Bitmap 对象。然后你可以使用它而不是 Image 对象本身来计算你的像素。
  • 但我已经做到了。请参阅编辑后的代码。它仍然给出参考错误......
  • 您仍在将位图存储为图像。试试这个,当你想使用 GetPixel 时,将你的图像转换为位图:((Bitmap)bmp).GetPixel(x, y)
【解决方案2】:

您的变量bmp 的类型为System.Drawing.Image,但GetPixel 方法只为System.Drawing.Bitmap 定义。

http://msdn.microsoft.com/de-de/library/system.drawing.bitmap.getpixel(v=vs.80).aspx

【讨论】:

  • 所以你的意思是我需要把它导入我的图书馆吗?
  • 我的图书馆没有.Drawing.Bitmap
  • 不明白,Image和Bitmap都是在System.Drawing.dll中定义的。
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2011-04-15
  • 2020-02-01
  • 1970-01-01
  • 2020-02-18
  • 2019-11-27
相关资源
最近更新 更多