Master Stroke
Master Stroke

Reputation: 5128

System.Drawing.Image Doesn't contain definition for 'GetPixel','LockImage'

I am working on setting up contrast of an image in C#,but i am getting error saying

"System.Drawing.Image Doesn't contain definition for 'GetPixel','LockImage'"

for the following code.

 public static Bitmap AdjustContrast(Bitmap OriginalImage, float Value)
        {
            Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData =
                Image.LockImage(NewBitmap);

            BitmapData OldData = Image.LockImage(OriginalImage);
            int NewPixelSize = Image.GetPixelSize(NewData);
            int OldPixelSize = Image.GetPixelSize(OldData);
            Value = (100.0f + Value) / 100.0f;
            Value *= Value;

            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    Color Pixel = Image.GetPixel(OldData, x, y, OldPixelSize);
                    float Red = Pixel.R / 255.0f;
                    float Green = Pixel.G / 255.0f;
                    float Blue = Pixel.B / 255.0f;
                    Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
                    Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
                    Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
                    Image.SetPixel(NewData, x, y,
                      Color.FromArgb(MathHelper.Clamp((int)Red, 255, 0),
                        MathHelper.Clamp((int)Green, 255, 0),
                       MathHelper.Clamp((int)Blue, 255, 0)),
                       NewPixelSize);
                }
            }
            Image.UnlockImage(NewBitmap, NewData);
            Image.UnlockImage(OriginalImage, OldData);
            return NewBitmap;
        }

Any suggestion would be helpful.

Upvotes: 0

Views: 2489

Answers (1)

Jay Riggs
Jay Riggs

Reputation: 53603

You're getting that error because the compiler sees Image and attempts to find GetPixel and LockImage methods in the System.Drawing.Image class. Those methods don't exist in that class and so you get your error.

It looks like you're getting your code from James Craig's blog, specifically this page. At the bottom of his snippet he indicates that some of the methods are from his utility library and he specifically names LockImage. If you haven't done so already, download his utility library (there's a link on the page I linked) and I imagine you'll find this method and GetPixel too.

When you do use the utility library be sure to specify your namespaces to disambiguate classes found in the utility library and those in the standard Microsoft libraries.

Upvotes: 2

Related Questions