Mira
Mira

Reputation: 195

Download generated bitmap image from ASP.NET website

I'm dynamically generating image from text, and existing image on my asp.net website.

Here is the code:

string barcode = Request.QueryString["BarCode"];
int w = barcode.Length * 40;

// Create a bitmap object of the width that we calculated and height of 100
Bitmap oBitmap = new Bitmap(w, 50);

// then create a Graphic object for the bitmap we just created.
Graphics oGraphics = Graphics.FromImage(oBitmap);

// Now create a Font object for the Barcode Font
// (in this case the IDAutomationHC39M) of 18 point size
Font oFont = new Font("BarcodeFont", 12);

// Let's create the Point and Brushes for the barcode
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);

// Now lets create the actual barcode image
// with a rectangle filled with white color
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);

// We have to put prefix and sufix of an asterisk (*),
// in order to be a valid barcode
oGraphics.DrawString(barcode, oFont, oBrushWrite, oPoint);

// Then we send the Graphics with the actual barcode
Response.ContentType = "image/png";
oBitmap.Save(Response.OutputStream, ImageFormat.Png);

As you can see the bitmap is saved and showed on aspx page after postback. What I wanna do is when user click Button1, then image is generated and browser download window pops up, without saving on server or showing on page. How to do this? Please help me.

Upvotes: 1

Views: 2700

Answers (1)

Raghuveer
Raghuveer

Reputation: 2637

You should update your response like following:

Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=downloadedFile.JPG");
Response.TransmitFile( @"c:/my documents/images/file.xxx" );
Response.End();

For more Information: http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET

Upvotes: 2

Related Questions