Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
GraphicsImage = new Bitmap(Panel1.Width, Panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
openFileDialog1.Filter="JPEG|*.jpg";
GraphicsImage = Image.FromFile(openfiledialog1.FileName);

ERROR:
Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)
Posted
Updated 16-Dec-10 1:13am
v2
Comments
Toniyo Jackson 16-Dec-10 7:13am
   
Always write your code inside code block
Manfred Rudolf Bihy 17-Dec-10 0:20am
   
I modified my answer to show two ways to make the image fit better into the panel it's displayed in.

you have to change last line with following
GraphicsImage = (Bitmap)Image.FromFile(openfiledialog1.FileName);
   
v3
This would work better because they are the same type:
Bitmap graphicsImage = new Bitmap(Panel1.Width, Panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

But there is no need to create it because it is overwritten 2 lines ahead when doing:
GraphicsImage = Image.FromFile(openfiledialog1.FileName);


Good luck!
   
Comments
Manfred Rudolf Bihy 16-Dec-10 7:24am
   
Sorry but you got it the wrong way round. If in the first line the GraphicsImage were an image the error message would have been more like cant convert Bitmap to Image and not the other way aroung.
Close, but no cigar :). I do agree though that the creation of the bitmap is absolutely superflous.
E.F. Nijboer 16-Dec-10 7:33am
   
Thanks for the response. Well, I see (when looking at some of the other answers) that I forgot to mention that when loading the image from file, casting would also be necessary.
Manfred Rudolf Bihy 16-Dec-10 7:44am
   
Ok, Ok 5 for you too! :)
Exactly what the error message said.
You need to cast the Image to a Bitmap:
C#
GraphicsImage = (Bitmap)Image.FromFile(openfiledialog1.FileName);

By saying that an "explicit casts exists" you're hinted that you must do a type cast. Another verions is this:
C#
GraphicsImage = Image.FromFile(openfiledialog1.FileName) as Bitmap;


Modification:

You have two options if you want to make the image fit into the panel:

1. Keeping the images aspect ratio you need to make this adjustment
C#
panel1.BackgroundImageLayout = ImageLayout.Zoom;

2. Filling the whole panel with the image disregarding the aspect ratio of the image
C#
panel1.BackgroundImageLayout = ImageLayout.Stretch;


Caveat!
Solution 2 will leave you with a distorted image if the panel's dimensions don't match the dimensions of the image.

End Modification

That's it!

Cheers,
Manfred
   
v5
Comments
Manfred Rudolf Bihy 16-Dec-10 7:30am
   
Ah, the univoter strikes again!
Shiza Khan 16-Dec-10 11:40am
   
But, now the problem is, that this image
GraphicsImage = Image.FromFile(chosenfile) as Bitmap; don't fits the whole panel size.
how can it be possible that GraphicsImage will also include the remaining panel area?
Shiza Khan 17-Dec-10 12:20pm
   
For my paint application, i draw everything first on the DrawingImage and then draw it to on the panel1.For it i had tried it like this...
Panel1.BackgroundImageLayout = ImageLayout.Zoom;
GraphicsImage = Panel1.BackgroundImage as Bitmap;
But this doesn't works well..:-(
At run time it give the exception unhandled: "Value cannot be null, Parameter name:image".
Manfred Rudolf Bihy 17-Dec-10 13:24pm
   
If Panel1.BackgroundImage is null you have not yet assigned an image to it. Somewhere in your code you need to do something like this:
Panel1.BackgroundImage = (Image) anExistingBitmapInYourApplication;
or like this:
Panel1.BackgroundImage = anExistingImageInYourApplication;
Shiza Khan 18-Dec-10 0:58am
   
OK..This had helped me alot..

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