Cannot save a Transparent PNG from my canvas in C#

我的梦境 提交于 2020-01-30 12:09:27

问题


I made a WPF canvas program which lets users add some PNG templates and make their own new design.

I could complete the working

However when I try to save , I get the white background Is there any way to get a transparent output ?

This is my code on saving image

public void SaveTo(string f)
    {
        Visual v = Dc;
        /// get bound of the visual
        Rect b = VisualTreeHelper.GetDescendantBounds(v);

        /// new a RenderTargetBitmap with actual size of c
        RenderTargetBitmap r = new RenderTargetBitmap(
            (int)b.Width, (int)b.Height,
            96, 96, PixelFormats.Pbgra32);

        /// render visual
        r.Render(v);

        /// new a JpegBitmapEncoder and add r into it 
        JpegBitmapEncoder e = new JpegBitmapEncoder();
        e.Frames.Add(BitmapFrame.Create(r));

        /// new a FileStream to write the image file
        FileStream s = new FileStream(f,
            FileMode.OpenOrCreate, FileAccess.Write);
        e.Save(s);
        s.Close();
    }

回答1:


You are encoding to a Jpeg.

Jpegs have no transparency information (aka alpha channel).

You should use a PngBitmapEncoder.



来源:https://stackoverflow.com/questions/25826556/cannot-save-a-transparent-png-from-my-canvas-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!