Nana Nana - 1 month ago 19 Question

Transparent WPF to PNG Image - VB.NET

I have a transparent borderless window in wpf and just the .png image from its background is visible and I'm trying to export the form exactly as it is, transparent.

Here is what I have:

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)

It works, but the output image is blank.


You are never setting the targets image to the background image anywhere in this code so when you save out, there is no image data.

Please try the following code. It worked for me:

     Image image = new Image();
     image.Source = (Background as ImageBrush).ImageSource;
     Size sz = new Size(image.Source.Width, image.Source.Height);
     image.Arrange(new Rect(sz));

     RenderTargetBitmap rtb = new RenderTargetBitmap((int)image.Source.Width, (int)image.Source.Height, 96d, 96d, PixelFormats.Default);

     PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
     using (Stream stream = File.Create("test.png"))