Ozarraga_AB
Ozarraga_AB

Reputation: 979

Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'`

Declared a bitmap which was

private Bitmap img1 = null;  
private Bitmap img2 = null;  

the image will be putted after selecting it from openFileDialog.
the selected images were placed in an array.

imgName = openFD.FileNames;

then button1 to display these image.

pictureBox1.Image = Image.FromFile(imgName[0]);  
pictureBox2.Image = Image.FromFile(imgName[1]);

i replaced the button1 code with this

img1 = Image.FromFile(imgName[0]);  
img2 = Image.FromFile(imgName[1]);

but an error occurs

Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'

I'd try to change the code to img1 = Bitmap.FromFile(imgName[0]);. but still has the same error.
Any suggestion how to correct or do this right?

Upvotes: 9

Views: 52958

Answers (4)

Scott McAllister
Scott McAllister

Reputation: 538

For my case, my System.Drawing.Image wasn't coming from a file but rather from a device's camera. Since I didn't want to save the image to a file as an intermediary step, I used the following approach:

var imageBitmap = new Bitmap(image.Width, image.Height);
using (var graphics = Graphics.FromImage(imageBitmap))
    graphics.DrawImage(image, 0, 0, image.Width, image.Height);

Maybe this is helpful to others ;)

Upvotes: 0

TEHSEEN
TEHSEEN

Reputation: 11

I have used these three lines in my code to assing byte value to image in c#.

ImageConverter converter = new ImageConverter();
pbStudentPicture.Image =  (Image)converter.ConvertFrom(StudentPinfo.Picture);
pbStudentPicture.SizeMode = PictureBoxSizeMode.StretchImage;

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

img1 = (Bitmap) Image.FromFile(imgName[0]);
img2 = (Bitmap) Image.FromFile(imgName[1]);

As the error message says you cannot implicitly do this you need to explicitly cast it to Bitmap

Edit

Based on the comments below I would suggest either go with icktoofay's answer i.e. use the Bitmap constructor or if you can use the Image class directly instead of using Bitmap you can also go with that

Upvotes: 2

icktoofay
icktoofay

Reputation: 129001

img1 = new Bitmap(imgName[0]);
img2 = new Bitmap(imgName[1]);

Upvotes: 14

Related Questions