生成透明PNG c#
我有下面的函数来生成示例徽标。我想要做的是返回透明的 png 或 gif 而不是白色背景。
我怎样才能做到这一点?
private Bitmap CreateLogo(string subdomain)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
Font objFont = new Font(
"Arial",
13,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics.DrawString(
subdomain, objFont,
new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}
最终结果如下:
context.Response.ContentType = "image/png";
using (MemoryStream memStream = new MemoryStream())
{
CreateLogo(_subdname).Save(memStream, ImageFormat.Png);
memStream.WriteTo(context.Response.OutputStream);
}
在 CreateLogo
函数中:
objGraphics.Clear(Color.White)
更改为objGraphics.Clear(Color.Transparent)
new SolidBrush(Color.FromArgb(102, 102, 102))
更改为new SolidBrush(Color.FromArgb(255, 255, 255))
I have the function below to generate a sample logo. What I want to do is to return a transparent png or gif instead of a white background.
How can I do that?
private Bitmap CreateLogo(string subdomain)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
Font objFont = new Font(
"Arial",
13,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics.DrawString(
subdomain, objFont,
new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}
Here is the end result:
context.Response.ContentType = "image/png";
using (MemoryStream memStream = new MemoryStream())
{
CreateLogo(_subdname).Save(memStream, ImageFormat.Png);
memStream.WriteTo(context.Response.OutputStream);
}
In the CreateLogo
function:
objGraphics.Clear(Color.White)
was changed toobjGraphics.Clear(Color.Transparent)
new SolidBrush(Color.FromArgb(102, 102, 102))
changed tonew SolidBrush(Color.FromArgb(255, 255, 255))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
You can do something like this:
看看你能用C#制作一个alpha透明的PNG吗?
Take a look at Can you make an alpha transparent PNG with C#?