生成透明PNG c#

发布于 2024-08-03 23:52:27 字数 1640 浏览 3 评论 0原文

我有下面的函数来生成示例徽标。我想要做的是返回透明的 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 to objGraphics.Clear(Color.Transparent)
  • new SolidBrush(Color.FromArgb(102, 102, 102)) changed to new SolidBrush(Color.FromArgb(255, 255, 255))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-08-10 23:52:27

你可以这样做:

Bitmap bmp = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(bmp);

g.Clear(Color.Transparent);
g.FillRectangle(Brushes.Red, 100, 100, 100, 100);

g.Flush();
bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);

You can do something like this:

Bitmap bmp = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(bmp);

g.Clear(Color.Transparent);
g.FillRectangle(Brushes.Red, 100, 100, 100, 100);

g.Flush();
bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文