积极答复者
win 7 下使用GDI+ 绘制PNG 图 文字透明异常。

问题
-
//Image img = Image.FromFile(@"e:\f.png"); Bitmap img = new System.Drawing.Bitmap(300, 300); img.MakeTransparent(); Graphics g = Graphics.FromImage(img); //Color bc=Color.(255,0,255); //g.Clear(ColorTranslator.FromHtml("#00FF00")); //g.Clear(Color.Transparent); Font font = new Font("Arial", 20, FontStyle.Bold); Color c = Color.FromArgb(50,255,0,0); //LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), c, c, 1.2f, true); SolidBrush brush = new SolidBrush(c); g.DrawString("ssssss", font, brush, 150, 160); System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save("xx.png", System.Drawing.Imaging.ImageFormat.Png);
以上代码是生成一个背景透明上面文字 alpha 50 红色的png图
在win 7 和 win7 下的 server 2003 VPC虚拟机 中生成的图片 背景透明文字黑色不透明,
在win7 下的 server 2003 VM 虚拟机中生成的图片背景透明,文字红色 半透明,这是正常的。
然后我在xp 和 xp下的 server 2003 VPC 虚拟机 中测试了下 图片还是正常的
这是怎么回事 。有什么解决方案可让片在win7 xp 以及 win7 下的VPC 虚拟机生成正常的图片
答案
-
你好,
这个问题实际上是使用 GDI+ 渲染 cleartype 文字以透明的背景在 Bitmap 上出现的( Windows7 默认使用 cleartype ,其他版本默认不是)。你可以关闭 cleartype (输入 cleartype 在 search Box )去验证这点,关闭后你的代码结果就是正确的。
产品组正在处理这个问题,但是我们现在有 2 个办法做为 workaround :
1 不使用 cleartype ,我们自己设置 mode 在代码中:
g.TextRenderingHint = System.Drawing.Text.SingleBitPerPixelGridFit;
同时参考 : http://msdn.microsoft.com/en-us/library/ssazt6bs.aspx
2 预先填充背景用不透明的白色,加下面的代码在 DrawString 之前 :
Color white = Color .FromArgb(255, 255, 255, 255);
SolidBrush w = new SolidBrush (white);
g.FillRectangle( w, 0, 0, 300, 300);
谢谢你的反馈!
Best regards,
Riquel
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- 已标记为答案 Riquel_DongModerator 2009年8月21日 8:46
全部回复
-
我服了。。。。我旋转了30度 g.RotateTransform(30); 就半透明了 alpha 50。。。。
转0度或者360度依然不透明 黑色。。。。 这个。。。 是bug 么?- 已编辑 Jerry.Huang 2009年7月27日 13:46
-
你好,
我看到你说的情况了,的确Win7的结果看上去不正确(我认为),我正在查看是否这个问题先前有人提出过,我也联系了Win7技术讨论组,当有进一步的消息我会告诉你。
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- 已建议为答案 Raymond TangModerator 2009年7月30日 3:12
-
你好,
这个问题实际上是使用 GDI+ 渲染 cleartype 文字以透明的背景在 Bitmap 上出现的( Windows7 默认使用 cleartype ,其他版本默认不是)。你可以关闭 cleartype (输入 cleartype 在 search Box )去验证这点,关闭后你的代码结果就是正确的。
产品组正在处理这个问题,但是我们现在有 2 个办法做为 workaround :
1 不使用 cleartype ,我们自己设置 mode 在代码中:
g.TextRenderingHint = System.Drawing.Text.SingleBitPerPixelGridFit;
同时参考 : http://msdn.microsoft.com/en-us/library/ssazt6bs.aspx
2 预先填充背景用不透明的白色,加下面的代码在 DrawString 之前 :
Color white = Color .FromArgb(255, 255, 255, 255);
SolidBrush w = new SolidBrush (white);
g.FillRectangle( w, 0, 0, 300, 300);
谢谢你的反馈!
Best regards,
Riquel
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- 已标记为答案 Riquel_DongModerator 2009年8月21日 8:46