alina
alina

Reputation: 281

convert bitmap to image for ASP.NET

this is how my code look now:

System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("aaa.jpg"));

int height = objImage.Height;
int width = objImage.Width;

System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
System.Drawing.Image bitmap2 = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("sem.png"));

g.DrawImage(bitmap2, (objImage.Width - bitmap2.Width) / 2, (objImage.Height - bitmap2.Height) / 2);

MemoryStream stream = new MemoryStream();     
bitmapimage.Save(stream, ImageFormat.Jpeg);

String saveImagePath = Server.MapPath("ImagesMerge/") + "aaa.jpg";
bitmapimage.Save(saveImagePath);
imgBig.ImageUrl = saveImagePath;

The problem I have now is that the image is not displayed in browser, I don't understand why .

Upvotes: 1

Views: 23194

Answers (6)

azamsharp
azamsharp

Reputation: 20068

You might be forgetting to set the Response.Headers. Check out the following example that shows how to create bar chart images and then display it on the screen:

http://www.highoncoding.com/Articles/399_Creating_Bar_Chart_Using__NET_Graphics_API.aspx

Upvotes: 0

Fredou
Fredou

Reputation: 20090

like jmaglasang said, I would suggest to you to use an ashx file and if you don't need to keep the image, just send the image stream directly to the http without saving it on the disk

so you only need to do something like

       <img src="Handler.ashx?action=merge&image1=blah.jpg&image2=bloh.jpg">

look at this code for an example of how to send an image made in memory that does not exist on the drive

Upvotes: 3

jerjer
jerjer

Reputation: 8760

You can also try:

imgBig.ImageUrl = ResolveUrl(saveImagePath);

EDIT:

If saveImagePath is under the WebApplication Directory, doing some modifications on the directory structure i.e. modifying files, deleting and creating can cause the application pool to recycle, and once it reaches the maximum recycle count the application pool will be stopped causing "Server unavailable" error.

I would suggests to add/save/modify images on a separate directory (not under the Apps Directory) then create a Handler(ASHX) that will read the images, just an advice though.

Upvotes: 1

Ot&#225;vio D&#233;cio
Ot&#225;vio D&#233;cio

Reputation: 74250

MapPath will give you a physycal address, not a virtual address which is what the browser needs to get to the image.

Upvotes: 0

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158289

Probably because saveImagePath will be a local path (such as c:\somepath\aaa.jpg) that is not reachable from the browser. You probably want to set the ImageUrl = "ImagesMerge/aaa.jpg" instead.

Upvotes: 2

Arjan Einbu
Arjan Einbu

Reputation: 13672

Bitmap is a subclass of Image, so there no need to convert Bitmap to Image. It already is...

Upvotes: 2

Related Questions