在 RenderTargetBitmap 捕获 wpf 控件的图像中有一个意外的透明区域
Has a unexpected transparent area in image that RenderTargetBitmap captured wpf control
我试图通过 RenderTargetBitmap class 将主 window wpf 应用程序捕获到 png 图像。除了图像的底部和左侧有一个小的透明区域外,它运行良好。这是代码和捕获的图像:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
string folder = "screenshot";
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string filename = "ScreenCapture_" + DateTime.Now.ToString("ddMM_hhmmss") + ".png";
string filePath = folder + "//" + filename;
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(this);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(filePath))
{
pngImage.Save(fileStream);
}
}
Captured image has transparent part at bottom
A Window 的 ActualWidth
和 ActualHeight
包括边框和标题栏的大小。
而不是 Window,您应该呈现其顶级 child 元素,即其 Content
:
var contentElement = (FrameworkElement)Content;
var renderTargetBitmap = new RenderTargetBitmap(
(int)contentElement.ActualWidth, (int)contentElement.ActualHeight,
96, 96, PixelFormats.Default);
renderTargetBitmap.Render(contentElement);
我试图通过 RenderTargetBitmap class 将主 window wpf 应用程序捕获到 png 图像。除了图像的底部和左侧有一个小的透明区域外,它运行良好。这是代码和捕获的图像:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
string folder = "screenshot";
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string filename = "ScreenCapture_" + DateTime.Now.ToString("ddMM_hhmmss") + ".png";
string filePath = folder + "//" + filename;
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(this);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(filePath))
{
pngImage.Save(fileStream);
}
}
Captured image has transparent part at bottom
A Window 的 ActualWidth
和 ActualHeight
包括边框和标题栏的大小。
而不是 Window,您应该呈现其顶级 child 元素,即其 Content
:
var contentElement = (FrameworkElement)Content;
var renderTargetBitmap = new RenderTargetBitmap(
(int)contentElement.ActualWidth, (int)contentElement.ActualHeight,
96, 96, PixelFormats.Default);
renderTargetBitmap.Render(contentElement);