使用不同的DPI捕获控件的屏幕截图 [英] Capture screenshot of control with different DPI
本文介绍了使用不同的DPI捕获控件的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以捕获正常100%DPI的控件截图。但是当DPI变为125%时,屏幕截图不正确。
I can capture screenshot of control for normal 100% DPI. But when the DPI changed to 125% then the screenshot is not proper.
// Get absolute location on screen of upper left corner of button
System.Windows.Point locationFromScreen = this.sv.PointToScreen(new System.Windows.Point(0, 0));
// Transform screen point to WPF device independent point
PresentationSource source = PresentationSource.FromVisual(this);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
var bmpScreenshot = new Bitmap((int)sv.ActualWidth,
(int)sv.ActualHeight,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen((int)targetPoints.X,
(int)targetPoints.Y,
0,
0,
new System.Drawing.Size((int)sv.ActualWidth, (int)sv.ActualHeight),
CopyPixelOperation.SourceCopy);
byte[] data;
using (var stream = new System.IO.MemoryStream())
{
bmpScreenshot.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
data = stream.ToArray();
}
var result = Convert.ToBase64String(data);
winObj = new Window1();
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)bmpScreenshot).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
winObj.imageViewer.Source = image;
winObj.ShowDialog();
bmpScreenshot.Dispose();
请提示方法,以便可以使用任何DPI捕获屏幕截图。
Please suggest approach so that the screenshot can be captured with any DPI.
推荐答案
嗨 Vipul_Langalia,
>>请提示方法以便截图即可使用任何DPI捕获
您可以尝试以下方法:使用RenderTargetBitmap类和PngBitmapEncoder
Hi Vipul_Langalia,
>>Please suggest approach so that the screenshot can be captured with any DPI
You can try the following method : use RenderTargetBitmap class and PngBitmapEncoder
SaveControlImage(sv);
// Save a control's image.
private void SaveControlImage(FrameworkElement control )
{
// Get the size of the Visual and its descendants.
Rect rect = VisualTreeHelper.GetDescendantBounds(control);
// Make a DrawingVisual to make a screen
// representation of the control.
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush brush = new VisualBrush(control);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
// Make a bitmap and draw on it.
int width = (int)control.ActualWidth;
int height = (int)control.ActualHeight;
RenderTargetBitmap rtb = new RenderTargetBitmap(
width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
// Make a PNG encoder.
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
encoder.Save(stream);
BitmapImage image = new BitmapImage();
image.BeginInit();
stream.Seek(0, SeekOrigin.Begin);
image.StreamSource = stream;
image.EndInit();
imageViewerforms winObj = new imageViewerforms();
winObj.imageViewer.Source = image;
winObj.ShowDialog();
}
最好的问候,
Yong Lu
Best Regards,
Yong Lu
这篇关于使用不同的DPI捕获控件的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文