我有一个简单的程序,而且我包含了System.Drawing,但我不能使用GetPixel()方法。它说它没有被找到。这可能是什么原因呢?
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);
}
}
}
}
}发布于 2013-05-06 07:37:34
编辑正如Hans在上面的评论中所说,如果您不在任何地方使用图像本身,则可以跳过Image.FromFile(),并将文件名直接传递给Bitmap构造函数。
Image对象不包含这些方法,Graphics对象也不包含这些方法,但Bitmap对象包含。因此,技巧是使用new Bitmap(image)从图像创建一个Bitmap,如下所示:
// 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派生的。
我想这应该行得通。
https://stackoverflow.com/questions/16390148
复制相似问题