首页 > 解决方案 > 使用不同 DPI 捕获控件的屏幕截图

问题描述

我可以捕获正常 100% DPI 的控制屏幕截图。但是当 DPI 更改为 125% 时,屏幕截图不正确。请建议方法,以便可以使用任何 DPI 捕获屏幕截图。以下是代码

// 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();

标签: c#wpf

解决方案


推荐阅读