c# - 你能用 C# 制作一个 alpha 透明的 PNG 吗?

标签 c# .net gdi+

我有一个显示垂直文本的多浏览器页面。

作为让文本在所有浏览器中垂直呈现的丑陋技巧,我创建了一个自定义页面处理程序,它返回带有垂直绘制文本的 PNG。

这是我的基本代码(C#3,但对任何其他版本进行了小的更改,直到 1):

Font f = GetSystemConfiguredFont();
//this sets the text to be rotated 90deg clockwise (i.e. down)
StringFormat stringFormat = new StringFormat { FormatFlags = StringFormatFlags.DirectionVertical };

SizeF size;
// creates 1Kx1K image buffer and uses it to find out how bit the image needs to be to fit the text
using ( Image imageg = (Image) new Bitmap( 1000, 1000 ) )
    size = Graphics.FromImage( imageg ).
        MeasureString( text, f, 25, stringFormat );

using ( Bitmap image = new Bitmap( (int) size.Width, (int) size.Height ) )
{
    Graphics g = Graphics.FromImage( (Image) image );
    g.FillRectangle( Brushes.White, 0f, 0f, image.Width, image.Height );
    g.TranslateTransform( image.Width, image.Height );
    g.RotateTransform( 180.0F ); //note that we need the rotation as the default is down

    // draw text
    g.DrawString( text, f, Brushes.Black, 0f, 0f, stringFormat );

    //make be background transparent - this will be an index (rather than an alpha) transparency
    image.MakeTransparent( Color.White );

    //note that this image has to be a PNG, as GDI+'s gif handling renders any transparency as black.
    context.Response.AddHeader( "ContentType", "image/png" );
    using ( MemoryStream memStream = new MemoryStream() )
    {
        image.Save( memStream, ImageFormat.Png );
        memStream.WriteTo( context.Response.OutputStream );
    }
}

这将创建一个看起来像我想要的图像,除了透明度是基于索引的。当我返回 PNG 时,它可以支持适当的 alpha 透明度。

有没有办法在 .net 中做到这一点?


感谢 Vlix(见评论)我做了一些更改,但仍然不对:

using ( Bitmap image = new Bitmap( (int) size.Width, (int) size.Height, PixelFormat.Format32bppArgb ) )
{
    Graphics g = Graphics.FromImage( (Image) image );
    g.TranslateTransform( image.Width, image.Height );
    g.RotateTransform( 180.0F ); //note that we need the rotation as the default is down

    // draw text
    g.DrawString( text, f, Brushes.Black, 0f, 0f, stringFormat );

    //note that this image has to be a PNG, as GDI+'s gif handling renders any transparency as black.
    context.Response.AddHeader( "ContentType", "image/png" );
    using ( MemoryStream memStream = new MemoryStream() )
    {
        //note that context.Response.OutputStream doesn't support the Save, but does support WriteTo
        image.Save( memStream, ImageFormat.Png );
        memStream.WriteTo( context.Response.OutputStream );
    }
}

现在 alpha 似乎可以工作,但文本看起来是 block 状的 - 就好像它仍然有锯齿边缘,但背景是黑色的。

这是 .Net/GDI+ 的一些错误吗?我已经发现它甚至无法为 gif 索引透明度,所以我对它没有太大信心。

这张图片显示了出错的两种方式:

vertical text comparison

顶部图像显示它没有白色背景或 MakeTransparent 调用。第二个背景填充白色,然后调用 MakeTransparent 以添加索引透明度。

这两个都不正确 - 第二张图片有我不想要的白色锯齿锯齿,第一张看起来是黑色锯齿。

最佳答案

要修复文本“ block 状”,你不能只做...

g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

在这一行之后...

Graphics g = Graphics.FromImage( (Image) image );

关于c# - 你能用 C# 制作一个 alpha 透明的 PNG 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/388677/

相关文章:

.net - 何时需要 ScriptManager/ScriptManagerProxy?

c# - 从 System.Drawing.Image 在文件系统上创建一个新图像?

image - 如何在 C++ 中使用 GDI+ 获取图像文件类型?

c# - 将元素添加到初始化之外的元组 C#

c# - 针对 Web api 方法的奇怪 405 错误

.net - 使用 Dapper-dot-net,如何将 SQL uniqueidentifier 列映射到 .Net 类型?

c# - 如何从外部 javascript 文件获取值到代码隐藏文件?

winapi - 使用 OpenGL 和 Gdi+ 的 GetDC、ReleaseDC、CS_OWNDC

c# - Microsoft.CodeAnalysis.TypeKind 枚举中的 TypeKind.Submission 和 TypeKind.Module 是什么意思?

c# - 如何接受带有 selenium firefox 驱动程序的自签名 SSL 证书?