找不到GetPixel方法 [英] GetPixel method is not found

查看:137
本文介绍了找不到GetPixel方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,并且其中包含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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆