没有保存一个新的位图c#
本文关键字:一个 位图 保存 | 更新日期: 2023-09-27 18:13:12
我想显示已创建的新位图图像。不保存
我在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 );
}
}
要从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();