你能不能让一个alpha透明PNG使用C#? [英] Can you make an alpha transparent PNG with C#?

查看:327
本文介绍了你能不能让一个alpha透明PNG使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multi-browser page that shows vertical text.

作为一个丑陋的黑客获取文本垂直呈现在所有浏览器我已经创建了一个自定义页面处理程序返回一个垂直绘制的文本PNG。

As an ugly hack to get text to render vertically in all browsers I've created a custom page handler that returns a PNG with the text drawn vertically.

下面是我的基本code(C#3,但小的改动任何其他版本降低到1):

Here's my basic code (C#3, but small changes to any other version down to 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透明度。

This creates an image that looks how I want it to, except that the transparency is index based. As I'm returning a PNG it could support a proper alpha transparency.

有没有办法做到这一点。NET中?

Is there any way to do this in .net?


由于Vlix(见注释),我做了一些改动,虽然它仍然是不正确的:

Thanks to Vlix (see comments) I've made some changes, though it still isn't right:

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

现在阿尔法似乎工作,但文本出现块状 - 就像它仍然具有jaggie的边缘,但在黑色背景

Now the alpha appears to work, but the text appears blocky - as if it still has the jaggie edges but against a black background.

这是与.net / GDI +一些bug?我已经发现它失败的甚至索引透射式的GIF,所以我没有多大信心它它。

Is this some bug with .Net/GDI+? I've already found that it fails for even index transparencies for gifs, so I don't have much confidence it it.

这个图像显示了这两个方面,这出了问题:

This image shows the two ways this goes wrong:

上面的图像显示它没有白色背景或 MakeTransparent 通话。与背景的第二个用白色填充,然后 MakeTransparent 打电话来添加索引透明度。

The top image shows it with no white background or MakeTransparent call. The second with the background filled with white and then MakeTransparent called to add the index transparency.

这些都不是正确的 - 第二个图像有,我不想白色走样锯齿,首先似乎是扎扎实实地对别名黑

Neither of these is correct - the second image has white aliasing jaggies that I don't want, the first appears to be solidly aliased against black.

推荐答案

要解决该文本块效应,你就不能这样做......

To fix the text "blockiness", can't you just do...

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

这行之后...

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

这篇关于你能不能让一个alpha透明PNG使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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