For example I have a WPF window:
public class MyWindow : Window
{
public MyWindow()
{
InitializeStyle();
InitializeContent();
InitializeSizePosition();
}
// ...
}
Now I want to render this window into a bitmap before showing it in foreground.
I tried the following things:
Using
RenderTargetBitmap
var window = new MyWindow(); RenderTargetBitmap bitmap = new((int)window.Width, (int)window.Height, 96, 96, PixelFormats.Pbgra32); bitmap.Render(window); // window.Show();
I get a full black bitmap. Same thing happens if I call these functions inside the window (
bitmap.Render(this)
), but it a just render a component of the window (e.g. aGrid
), it works fine.Ensuring window handle
As someone suggested, I tried
WindowInteropHelper
to ensure window handle, but it doesn't work.var window = new MyWindow(); var helper = new WindowInteropHelper(window); helper.EnsureHandle(); RenderTargetBitmap bitmap = new((int)window.Width, (int)window.Height, 96, 96, PixelFormats.Pbgra32); bitmap.Render(window); // window.Show();
Call
Measure()
,Arrange()
&UpdateLayout()
manuallyI called these methods both inside & outside the window class before getting bitmap, it doesn't work either, even with a fixed window size.
I want to know if there's a way to solve my problem or workaround?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744278692a4566462.html
评论列表(0条)