mantaplatte mantaplatte - 1 year ago 97 Question

WPF RenderTargetBitmap black bars on right side and bottom

I want to save an image from my WPF user control. It works but I got black bars on the right side and the bottom. If I change the dpiX (96) and dpiY (96) it works but when I maximize the window, it's wrong again (then a bit of the usercontrol is missing ).

This is how I save the image as bitmap:

Dim parentWindow As Window = Window.GetWindow(_Map)
Dim rtb As New RenderTargetBitmap(parentWindow.ActualWidth, parentWindow.ActualHeight, 96, 96, PixelFormats.Pbgra32)
Dim ms As New MemoryStream()
Dim bp As New BmpBitmapEncoder()
Dim saveMap As New Bitmap(ms)

Answer Source

The width and height of the window are a bit larger than your UserControl because it includes the titlebar and borders. if you use the dimensions of your control instead it works fine:

Dim rtb As New RenderTargetBitmap(_Map.ActualWidth, _Map.ActualHeight, 96, 96, PixelFormats.Pbgra32)

I recommend you also make sure the control is layouted properly by calling Measure and Arrange with the current or desired size before rendering.

Additional size calculations may be neccessary if you are using the LayoutTransform or RenderTransform properties of your control.

Btw: WPF always uses 96 DPI for size calculations so you shouldn't change that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download