Click here to Skip to main content
15,064,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have a problem regarding:
Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)

On the line : Bitmap imagestream = GrayScale(Img);


C#
public Image GrayScale(Image Img)
{
    Stream imageStream = new MemoryStream();
    using (Bitmap bmp = new Bitmap(Img))
    {
        int rgb;
        Color c;

        for (int y = 0; y < bmp.Height; y++)
            for (int x = 0; x < bmp.Width; x++)
            {
                c = bmp.GetPixel(x, y);
                rgb = (int)((c.R + c.G + c.B) / 3);
                bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
            }
        bmp.Save(imageStream, ImageFormat.Jpeg);
    }
    return Image.FromStream(imageStream);
}

private void button2_Click(object sender, EventArgs e)
{
    Image Img = Image.FromFile("C:\\Users\\L31101\\Desktop\\19.male.happy.normalized.jpg");

    Bitmap imagestream = GrayScale(Img);
}


Anyone knows how to solve this?
Posted
Updated 30-Jun-14 23:29pm
v2
Comments
johannesnestler 1-Jul-14 5:46am
   
Image is a abstract class while Bitmap is a concrete implementation, so just create a new Bitmap and initialize with image data like ProgramFOX showed you

Try this:
C#
Bitmap imagestream = new Bitmap(GrayScale(Img));
   
v2
Hello

Try one one of the below

Bitmap oBitmap = new Bitmap(GrayScale(Img));

OR

Bitmap oBitmap = (Bitmap) GrayScale(Img);


OR

Image oImage = GrayScale(Img);

Bitmap oBitmap = oImage as Bitmap;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900