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