简体   繁体   English

如何在 C# WPF 中使图像(徽标)的背景透明?

[英]How to make the background of an image ( logo ) transparent in C# WPF?

I've an image ( logo ) and I would like to pass it as an argument and make the background transparent and then save it.我有一个图像( logo ),我想将它作为参数传递,并使背景透明,然后保存它。

How can I achieve this?我怎样才能做到这一点?

This what I've tried:这是我尝试过的:

public string SaveImage(Bitmap bmp,string SavePath,string Name)
    {
        string path = "";
        var format = ImageFormat.Jpeg;
        using (var m = new MemoryStream())
        {
            bmp.Save(m, format);

            var img = System.Drawing.Image.FromStream(m);


            using (var g = Graphics.FromImage(img)) // This is what I've used for making the background transparent 
            {
                g.Clear(System.Drawing.Color.Transparent);
                g.Save();
            }
            var ImgPath = Path.Combine(SavePath, Path.ChangeExtension(Name,"png"));

            img.Save(ImgPath);
            path = ImgPath;

        }

        return path;
    }

After testing this function I'm getting a black image as an output ( save image )在测试了这个 function 之后,我得到了一个黑色图像作为 output(保存图像)

Some remarks for your code:您的代码的一些注释:

  • You use System.Drawing types wrapping native GDI+ components, not WPF.您使用包装原生 GDI+ 组件的System.Drawing类型,而不是 WPF。
  • You save the Bitmap with JPEG encoder, which does not support transparency.您使用不支持透明度的 JPEG 编码器保存Bitmap Even if the logo had a transparent background it is removed by the JPEG encoder即使徽标具有透明背景,JPEG 编码器也会将其删除
  • Now you reload the saved image and clear the complete content with transparent color (which will not work either as the reloaded JPEG image will have 24-bit RGB pixel format without alpha support so the whole image will be black)现在您重新加载保存的图像并使用透明颜色清除完整内容(这也不起作用,因为重新加载的 JPEG 图像将具有 24 位 RGB 像素格式,不支持 alpha,因此整个图像将是黑色的)

Possible solutions:可能的解决方案:

  • Use PNG encoder instead, which supports transparency.请改用支持透明度的 PNG 编码器。 If the logo had a transparent background you don't even need anything else.如果徽标有透明背景,您甚至不需要其他任何东西。
  • If the logo was not transparent originally, use the MakeTransparent method instead of Clear .如果徽标最初不是透明的,请使用MakeTransparent方法而不是Clear It will replace a single color so it is not able to restore possible anti-aliased alpha blending.它将替换单一颜色,因此无法恢复可能的抗锯齿 Alpha 混合。 It will also convert the pixel format so can be used also on the reloaded JPEG.它还将转换像素格式,因此也可以在重新加载的 JPEG 上使用。
  • To do the same in WPF you can use this library (disclaimer: written by me).要在 WPF 中做同样的事情,你可以使用这个库(免责声明:由我编写)。 You can use the GetReadWriteBitmapData extension method on a WriteableBitmap to access the same MakeTransparent functionality as above along with a sort of other features .您可以使用WriteableBitmap上的GetReadWriteBitmapData扩展方法来访问与上述相同的MakeTransparent功能以及其他一些功能 But to restore a nicely anti-aliased alpha background you might want to use the TransformColors method instead if you know how to separate the blended colors.但是,如果您知道如何分离混合的 colors,则要恢复很好的抗锯齿 alpha 背景,您可能需要使用TransformColors方法。

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

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