Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Hi there,

How to convert Bitmap to an Image?

I want to display the returned bitmap in an Image only not on the webpage.
Thanks in advance.

Copied from Comment
C#
public Bitmap Draw() 
{ 
Bitmap obj_Bitmap = new Bitmap(500, 500); 
obj_Graphics = Graphics.FromImage(obj_Bitmap); 
obj_Graphics.Clear(Color.Wheat); 
obj_Graphics.SmoothingMode = SmoothingMode.HighQuality; 
obj_Graphics.DrawString("Line Chart", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(Color.Purple), new Point(30, 30)); 
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; obj_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg); 
obj_Bitmap.Dispose(); 
obj_Graphics.Dispose(); 
System.Web.HttpContext.Current.Response.End(); 
return obj_Bitmap; 
} 
Bitmap bmp = new Bitmap(500, 500); 
bmp=draw();


It's my code.
My actual issue is when I display this image in the webpage all controls get vanished.
So I want to assign it to an Image and display it.

How is it possible?
Posted
Updated 17-May-11 23:58pm
v5
Comments
   
Tag it! What, you don't know that there are different types named Bitmap?! Explain what you really want to do and what's the purpose of it?
--SA
aswathy.s.88 18-May-11 1:44am
   
public Bitmap Draw()
{
Bitmap obj_Bitmap = new Bitmap(500, 500);
obj_Graphics = Graphics.FromImage(obj_Bitmap);
obj_Graphics.Clear(Color.Wheat);
obj_Graphics.SmoothingMode = SmoothingMode.HighQuality;
obj_Graphics.DrawString("Line Chart", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(Color.Purple), new Point(30, 30));

System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
obj_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
obj_Bitmap.Dispose();
obj_Graphics.Dispose();
System.Web.HttpContext.Current.Response.End();

return obj_Bitmap;
}





Bitmap bmp = new Bitmap(500, 500);
bmp=draw();


It's my code.My actual issue is when I display this image in the webpage all controls get vanished. So I want to assign it to an Image and display it... How is it possible?
aswathy.s.88 18-May-11 2:24am
   
I Thank all of You for your reply. I finally got it by using a Handler
Dalek Dave 18-May-11 4:10am
   
Edited for Grammar and Readability.

Since Bitmap inherits from Image no conversion is needed - you just need a cast:
Bitmap b;
...
// load bitmap
...
Image i = (Image)b;
   
v3
Try
C#
Bitmap objBitmap = new Bitmap("myfile.jpg");
Image objImage = (Image)objBitmap;
   
Bitmap is the image. Check yourself.

—SA
   
Comments
aswathy.s.88 18-May-11 1:45am
   
Okay... How can I display that bitmap into an Image then? That is the issue
   
Display? In ASP.NET? It would be <img src="filename" /> You save jpg or png file and the file name would be in <img>.
--SA
Without much of background to your question, I can think of this, try:
C#
Bitmap bm = new Bitmap("someImage.jpg");
Image img = (Image)bm;


Further, have a look at this one too: Convert Bitmap to Image[^]
   
Bitmap obj = new Bitmap("abcd.jpg");//abcd.jpg is the file name
Image i = (Image)obj;
   
By searching google or any search engine site.
   
Comments
aswathy.s.88 18-May-11 4:24am
   
?
version_2.0 18-May-11 5:37am
   
????????????????????????????????????????

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