【问题标题】:Download generated bitmap image from ASP.NET website从 ASP.NET 网站下载生成的位图图像
【发布时间】:2013-01-04 09:30:33
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net image bitmap


    【解决方案1】:

    您应该更新您的回复,如下所示:

    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

    【讨论】:

    • 我可以让用户选择文件目的地吗?
    • @Mira 是的,您可以查看给定的链接
    • 找不到路径的一部分
    • 你试过了吗? Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") ); ~/images/sailbig.jpg 是你的图片路径
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多