Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
Assalam-o-Alaikum,

I am trying to convert bitmap image to Image data type....what I have learned so far by searching that
Image new = (Image)myBitmap;

But its not working....

cannot convert from system.drawing.bitmap to system.web.ui.controls.Image

Any suggestion?
Posted

The first and easiest way to convert an image to bytes is to use the ImageConverter class under the System.Drawing namespace. The ConvertTo function is not static, so you'll need to create an instance of the class. The function returns an object so the result needs to be converted to the proper type.


C#
public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
 
Share this answer
 
Wale Kum,
Did you got your answer.?
 
Share this answer
 
Comments
Patrice T 3-Nov-15 22:29pm    
Not an answer, please delete
saad_lah 4-Nov-15 13:15pm    
yeah, but its pretty old question...so, i really dont know what i did
System.Drawing.Bitmap Inherits from System.Drawing.Image, not from System.Web.UI.Controls.Image

You cannot convert to the control Image simply by stating that the Bitmap is an Image.
If you intended to do this for an Image (that is a System.Drawing.Image)
that would simply be:
C#
Bitmap bm = new Bitmap("yourBitmap.jpg");
Image img = (Image)bm;


So you see that what you are trying to do is impossible, what you could try to do is show the bitmap in the Image control ... was that what you were trying?, if so, try something like this:
C#
ImageSourceConverter c = new ImageSourceConverter();
this.picture.Source = (ImageSource)c.ConvertFrom(bmp);




Good luck,
Edo
 
Share this answer
 
v4
Comments
saad_lah 21-Jul-13 9:56am    
I know that they are total different classes, but I want to know how can I convert it?
is there any different way? because I am retrieving the image from database(sql) then covert the memory stream into bitmap

if (reader.Read())
{
byte[] picData = reader["Image"] as byte[] ?? null;

if (picData != null)
{
using (MemoryStream ms = new MemoryStream(picData))
{
bmp = new System.Drawing.Bitmap(ms);
return bmp;
}
}
}

here is the part of function...
Joezer BH 21-Jul-13 10:01am    
Where do you get the exception?
saad_lah 21-Jul-13 10:10am    
It is the part of the function, I have used exception handling! and the function is returning bitmap dataType
Joezer BH 21-Jul-13 10:13am    
1. When you reply to a post, use the "Reply" link on that specific post.
2. I don't understand on which line you get the exception - the code looks OK and it does not use the System.Web.UI.Controls.Image that was implied in your question, so please explain a little more
saad_lah 21-Jul-13 10:17am    
SD.Bitmap bmp = SQL_STUFF.GetImageFromDB(IDNo); //The above function is returning bmp
//Now i want to convert the returned bmp into Image type in to show image in my //website(asp.net)
this.picture = bmp; //some conversion

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