简体   繁体   English

如何使我的图像透明?

[英]How to make my image transparent?

I know how to make an image control transparent in C#, but is there a way to make the image (the .jpeg file, not the image control) transparent? 我知道如何在C#中使图像控件透明,但是有没有办法使图像(.jpeg文件,而不是图像控件)透明? Or, is there a way, when I make the image control transparent, to create new image, save it in some path and give it the other image control's content? 或者,当我使图像控件透明时,有没有办法创建新图像,将其保存在某个路径中并提供其他图像控件的内容?

The JPEG file cannot be transparent. JPEG文件不能透明。 However, if you save it as a PNG image it will have a transparency channel. 但是,如果将其另存为PNG图像,它将具有透明通道。

GIFS also support transparency, but only either completely opaque or completely transparent. GIFS还支持透明性,但仅支持完全不透明或完全透明。 Nothing in between. 两者之间什么都没有。

PNG is your best bet here IMO. PNG是IMO的最佳选择。

You can set the Image-Controls's Opacity-Property to view it with transparence. 您可以设置图像控件的不透明度属性以透明方式查看它。 But to get an image-file with transparency, you'll have to render it into a new file. 但是要获得具有透明度的图像文件,必须将其渲染为新文件。

Here's an example how you can do it. 这是一个示例,您可以如何做。

Image image = new Image(); //or however you get this
image.Opacity = 0.5;
RenderTargetBitmap bmp = new RenderTargetBitmap(image.Source.Width, image.Source.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(image);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
using (Stream fs= File.Create("test.png"))
{
png.Save(fs);
}

There is property transparencycolor. 有属性透明色。 That is what you need if you want to display image on control. 如果要在控件上显示图像,这就是您需要的。

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

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