从ASP.NET网站下载生成的位图图像

时间:2013-01-21 10:34:37

标签: c# asp.net image bitmap

我在我的asp.net网站上动态生成图像和现有图像。

以下是代码:

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);

正如您所看到的,位图已保存并在回发后显示在aspx页面上。我想要做的是当用户点击Button1,然后生成图像并弹出浏览器下载窗口,而不保存在服务器上或在页面上显示。这该怎么做?请帮帮我。

1 个答案:

答案 0 :(得分:2)

您应该像下面这样更新您的回复:

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

更多信息:http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET