1

我正在从文本和我的 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 时,会生成图像并弹出浏览器下载窗口,而不保存在服务器上或显示在页面上。这个怎么做?请帮我。

4

1 回答 1

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

于 2013-01-21T10:40:50.960 回答