簡體   English   中英

無法在C#中從我的畫布保存透明PNG

[英]Cannot save a Transparent PNG from my canvas in C#

我制作了一個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();
    }

您正在編碼為Jpeg。

Jpegs沒有透明度信息(也稱為Alpha通道)。

您應該使用PngBitmapEncoder

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM