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

时间:2011-09-14 23:44:46

标签: c# asp.net image image-processing dynamic

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

干杯。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

保存文件后:

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