生成透明PNG C# [英] Generate transparent PNG c#

查看:282
本文介绍了生成透明PNG C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数来产生一个样本的标识。我想要做的就是回到一个透明的PNG或GIF,而不是白色背景。
我该怎么办呢?



 私人位图CreateLogo(串子域)
{

位图objBmpImage =新位图(1,1);
INT intWidth = 0;
INT intHeight = 0;
字体objFont =新的字体(宋体,13,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Pixel);
图形objGraphics = Graphics.FromImage(objBmpImage);
intWidth =(int)的objGraphics.MeasureString(子域,objFont).WIDTH;
intHeight =(int)的objGraphics.MeasureString(子域,objFont).Height;
objBmpImage =新位图(objBmpImage,新的大小(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(子站点,objFont,新SolidBrush(Color.FromArgb(102,102,102)),0,0);
objGraphics.Flush();
回报(objBmpImage);


}

下面是最终的结果。



  context.Response.ContentType =图像/ PNG 
使用(MemoryStream的memStream =新的MemoryStream())
{
CreateLogo(_subdname).Save(memStream,ImageFormat.Png);
memStream.WriteTo(context.Response.OutputStream);
}



功能



 私人位图CreateLogo(串子域)
{

位图objBmpImage =新位图(1,1);
INT intWidth = 0;
INT intHeight = 0;
字体objFont =新的字体(宋体,13,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Pixel);
图形objGraphics = Graphics.FromImage(objBmpImage);
intWidth =(int)的objGraphics.MeasureString(子域,objFont).WIDTH;
intHeight =(int)的objGraphics.MeasureString(子域,objFont).Height;
objBmpImage =新位图(objBmpImage,新的大小(intWidth,intHeight));
objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.Transparent);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics.DrawString(子域名,objFont,新SolidBrush(Color.FromArgb(255,255,255)),0,0);
objGraphics.Flush();
回报(objBmpImage);

}


解决方案

乘坐看看 http://stackoverflow.com/questions/388677/可以任您做安-α-透明PNG-与-C


I have below function to generate a sample logo. What I want to do is to return a transparent png or gif instead of white background. How can I do it?

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

function

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.Transparent);
    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    objGraphics.DrawString(subdomain, objFont, new SolidBrush(Color.FromArgb(255, 255, 255)), 0, 0);
    objGraphics.Flush();
    return (objBmpImage);

}

解决方案

Take a look at http://stackoverflow.com/questions/388677/can-you-make-an-alpha-transparent-png-with-c

这篇关于生成透明PNG C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆