【问题标题】:Not saving a new bitmap c#不保存新的位图c#
【发布时间】:2016-10-14 07:49:33
【问题描述】:

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

我在 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 );
    }
}

【问题讨论】:

  • Winforms?在Form 上使用PictureBox
  • 请问您使用的是 WinForms 还是 WPF?
  • 它是我自己的类控制。它将在一个 aspx 页面上。 @FredrikRedin
  • private void GenerateBanner( string titleText )改成private Bitmap GenerateBanner( string titleText ),最后加上return bannerSource

标签: c# bitmap


【解决方案1】:

要从 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();

【讨论】:

  • 一些详细信息:你需要在你的html页面中创建一个<img src="getimage.aspx?id=123">getimage.aspx应该运行上面的代码。所以你需要分两步来实现它。首先是生成图片链接。第二个是解释查询字符串以返回正确的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
相关资源
最近更新 更多