【问题标题】:Cannot save a Transparent PNG from my canvas in C#无法在 C# 中从我的画布中保存透明 PNG
【发布时间】:2014-09-14 02:57:15
【问题描述】:

我制作了一个 WPF 画布程序,它允许用户添加一些 PNG 模板并进行自己的新设计。

我可以完成工作

但是当我尝试保存时,我得到了白色背景 有什么办法可以得到透明的输出吗?

这是我保存图片的代码

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();
    }

【问题讨论】:

标签: c# .net wpf canvas


【解决方案1】:

您正在编码为 Jpeg。

Jpeg 没有透明度信息(也称为 Alpha 通道)。

您应该使用PngBitmapEncoder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 2011-06-08
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 2023-03-08
    • 2019-03-18
    • 2014-02-20
    相关资源
    最近更新 更多