GetPixel method is not found

点点圈 提交于 2019-12-11 14:43:38

问题


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);
                }
            }



        }
    }
}

回答1:


[EDIT] 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.

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);
    }
}

Note that Bitmap derives from System.Drawing.Image.

I think that should work.



来源:https://stackoverflow.com/questions/16390148/getpixel-method-is-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!