不保存新的位图c#

时间:2016-10-14 07:49:34

标签: c# bitmap

我想要显示已创建的新位图图像。不保存。

我在C#中使用带有图形的位图,我希望无需保存新图像即可返回新图像。

C#

private void GenerateBanner( string titleText ) {

    Bitmap bannerSource = new Bitmap( DefaultBannerPath );
    //bannerSource.Save( PhysicalBannerPath );
    RectangleF rectf = new RectangleF( 430, 50, 650, 50 );

    using (Graphics g = Graphics.FromImage(bannerSource))
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        g.DrawString(titleText, new Font("Bradley Hand ITC", 100, FontStyle.Bold), Brushes.White, rectf);
        //bannerSource.Save( PhysicalBannerPath );
    }
}

1 个答案:

答案 0 :(得分:2)

要从ASPX页面返回此图像(可在HTML中用作img src),您需要使用MemoryStream并将其转换为byte[];然后使用Response.BinaryWrite方法:

        byte[] bytes;
        using (var stream = new System.IO.MemoryStream())
        {
            bannerSource.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            bytes = stream.ToArray();
        }

        Response.ContentType = "image/jpeg";
        Response.Clear();
        Response.BinaryWrite(bytes);
        Response.End();