从ASP下载动态生成的图像.网的网站

本文关键字:图像 网站 ASP 下载 动态 | 更新日期: 2023-09-27 18:08:51

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

代码如下:

protected void Button1_Click(object sender, EventArgs e)
{
    var tytul = Request.QueryString["Tytul"];
    var tresc = Request.QueryString["Tresc"];
    var font = new Font("Verdana", 23);
    var brushForeColor = new SolidBrush(Color.Black);
    var brushBackColor = new SolidBrush(Color.FromArgb(248, 247, 182));
    var test = new Bitmap(450, 60);
    var graphicstest = Graphics.FromImage(test);
    var width = (int)graphicstest.MeasureString(tresc, font).Width;
    var height = (int)graphicstest.MeasureString(tresc, font).Height;
    while (width > 450)
    {
        height = height + 25;
        width = width - 450;
    }
    var heightall = height + 40 + 30 + 100;
    var bitmap = new Bitmap(450, heightall);
    var graphics = Graphics.FromImage(bitmap);
    var displayRectangle = new Rectangle(new Point(0, 0), new Size(450, 40));
    graphics.FillRectangle(brushBackColor, displayRectangle);
    //Define string format 
    var format1 = new StringFormat(StringFormatFlags.NoClip);
    format1.Alignment = StringAlignment.Center;
    var format2 = new StringFormat(format1);
    //Draw text string using the text format
    graphics.DrawString(tytul, font, brushForeColor, displayRectangle, format2);
    // Rysowanie drugiego boxa
    brushBackColor = new SolidBrush(Color.FromArgb(253, 253, 202));
    font = new Font("Verdana", 18);
    displayRectangle = new Rectangle(new Point(0, 40), new Size(450, height + 30));
    graphics.FillRectangle(brushBackColor, displayRectangle);
    displayRectangle = new Rectangle(new Point(0, 55), new Size(450, height + 15));

    graphics.DrawString(tresc, font, brushForeColor, displayRectangle, format2);
    graphics.DrawImage(System.Drawing.Image.FromFile(Server.MapPath(".") + "/gfx/layout/podpis.png"), new Point(0, height + 70));
    Response.ContentType = "image/png";
    bitmap.Save(Response.OutputStream, ImageFormat.Png);      
}

可以看到,位图在回发后被保存并显示在aspx页面上。我想做的是当用户点击Button1,然后图像生成,浏览器下载窗口弹出,不保存在服务器上,也不显示在页面上。如何做到这一点?请帮帮我。

欢呼。

从ASP下载动态生成的图像.网的网站

您需要添加一个Content-Disposition标题

保存文件后:

Response.AppendHeader("content-disposition", "attachment; filename=podpis.png" );
Response.WriteFile("yourfilepath/podpis.png");
Response.End;