简体   繁体   English

如何在C#中将具有透明截面的图像绘制到Graphics对象?

[英]How do I draw an image with a transparent section to a Graphics object in C#?

I have a Graphics object that I am drawing to and displaying. 我有一个要绘制并显示的Graphics对象。 This is working fine. 一切正常。 The thing that is not working is drawing a .png image with a transparent section. 无法正常工作的是绘制带有透明部分的.png图像。 I am drawing these images from am ImageList control (if that makes any difference) with the call: 我正在通过调用从ImageList控件(如果有任何区别)绘制这些图像:

myGraphicsObject.DrawImage(myImage, x_position, y_position);

I have not specifically set any transparent color value for the .png image and I cannot find any value in my control that specifies a transparent color for the control. 我没有为.png图像专门设置任何透明颜色值,也无法在控件中找到为控件指定透明颜色的任何值。

For the image itself I have tried filling the transparent colors for 32bit (255,0,255), 8bit (0,0,0), and white (255,255,255) in the desired transparent sections. 对于图像本身,我尝试在所需的透明区域中填充32位(255,0,255),8位(0,0,0)和白色(255,255,255)的透明颜色。 None are not transparent when drawn, though. 但是,绘制时没有一个不是透明的。

Is there a method somewhere that tells the image to become transparent for a specified color? 某处是否有方法告诉图像对于指定的颜色透明? Any other suggestions/solutions? 还有其他建议/解决方案吗?

该值在窗体本身上,而不是在控件上,它称为TransparencyKey ,将其设置为您希望在winforms设计器中透明的颜色,并且与该颜色相等的任何颜色(仅在您的窗体中)都不会呈现。

Does your PNG's display correctly (with transparency) on a ImageList? 您的PNG是否在ImageList上正确显示(透明)? Maybe your ImageList is set to 24bit RGB? 也许您的ImageList设置为24位RGB? Change to 32bit and that should help. 更改为32位,应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM