找不到GetPixel方法 [英] GetPixel method is not found
问题描述
我有一个简单的程序,并且其中包含System.Drawing,并且我无法使用GetPixel()方法.它说找不到.可能是什么原因?
I have a simple program, and ive included System.Drawing and I do not have an ability to use the GetPixel() method. It says its not found. What could be the reason for this?
using System.Drawing;
namespace isolatepixels
{
class Program
{
static void Main(string[] args)
{
System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\1.jpg");
int x, y;
// Loop through the images pixels to reset color.
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
image1.SetPixel(x, y, newColor);
}
}
}
}
}
推荐答案
正如汉斯在上面的评论中所说,如果您不这样做,则可以跳过Image.FromFile()
并将文件名直接传递给Bitmap
构造函数在任何地方使用图像本身.
As Hans says in his comment above, you can skip the Image.FromFile()
and pass the filename directly to the Bitmap
constructor if you are not using the image itself anywhere.
Image
对象不包含这些方法,Graphics
对象也不包含,但是Bitmap
对象包含.因此,诀窍是使用 new Bitmap(image)
这样:
An Image
object doesn't contain those methods and nor does a Graphics
object, but a Bitmap
object does. So the trick is to create a Bitmap
from the image, using new Bitmap(image)
like so:
// Don't need this: Image image1 = Image.FromFile(@"C:\1.jpg");
Bitmap bitmap = new Bitmap(@"C:\1.jpg");
// Save the image in JPEG format.
bitmap.Save(@"C:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
int x, y;
// Loop through the images pixels to reset color.
for (x = 0; x < bitmap.Width; x++)
{
for (y = 0; y < bitmap.Height; y++)
{
Color pixelColor = bitmap.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
bitmap.SetPixel(x, y, newColor);
}
}
请注意,Bitmap
是从System.Drawing.Image
派生的.
我认为应该起作用.
这篇关于找不到GetPixel方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!