透明的WPF PNG图像- VB.NET



我在wpf中有一个透明的无边框窗口,只有来自其背景的。png图像是可见的,我试图完全按照原样导出表单,透明。

我有:

    Dim target = New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb)
    Dim graphics__1 = Graphics.FromImage(target)
    graphics__1.CompositingMode = CompositingMode.SourceOver
    graphics__1.DrawImage(target, 0, 0)
    target.Save("filename.png", ImageFormat.Png)

可以,但是输出的图像是空白的

您从未在此代码的任何地方设置目标图像为背景图像,因此当您保存时,没有图像数据。

请尝试以下代码。

Image image = new Image();
image.Source = (Background as ImageBrush).ImageSource;
Size sz = new Size(image.Source.Width, image.Source.Height);
image.Measure(sz);
image.Arrange(new Rect(sz));
RenderTargetBitmap rtb = new RenderTargetBitmap((int)image.Source.Width, (int)image.Source.Height, 96d, 96d, PixelFormats.Default);
rtb.Render(image);
PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(rtb));
using (Stream stream = File.Create("test.png"))
   pngBitmapEncoder.Save(stream);

最新更新