我正在尝试创建屏幕截图/位图.我写了这个函数:
public static Bitmap CreateScreenshot(Rectangle bounds)
{
var bmpScreenshot = new Bitmap(bounds.Width, bounds.Height,
PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(bounds.X, bounds.Y,
0, 0,
new Size(bounds.Size.Width, bounds.Size.Height),
CopyPixelOperation.SourceCopy);
return bmpScreenshot;
}
Run Code Online (Sandbox Code Playgroud)
在我的叠加形式中调用此函数,该窗体应将位图绘制到自身上.我目前正在使用GDI +进行整个过程.
private void ScreenshotOverlay_Load(object sender, EventArgs e)
{
foreach (Screen screen in Screen.AllScreens)
Size += screen.Bounds.Size;
Location = Screen.PrimaryScreen.Bounds.Location;
_screenshot = BitmapHelper.CreateScreenshot(new Rectangle(new Point(0, 0), Size));
Invalidate(); // The screenshot/bitmap is drawn here
}
Run Code Online (Sandbox Code Playgroud)
是的,我稍后处理了位图,所以不用担心.;)在我的笔记本电脑和台式电脑上工作正常.我用不同的分辨率测试了它,计算是正确的.我可以在表单上看到屏幕的图像.
问题始于Surface 3.所有元素的缩放比例均为1.5(150%).这意味着DPI会发生变化.如果我尝试在那里截取屏幕截图,它只会捕获屏幕左上角但不是整个屏幕截图.我已经通过谷歌和StackOverflow,尝试了不同的东西:
第一种方式没有带来预期的结果.第二个做了,但整个应用程序必须进行调整,这在Windows窗体中非常复杂.
现在我的问题是:有没有办法捕获整个屏幕的截图,即使它的scalation因子高于1(更高的DPI)?必须有办法做到这一点,以使其在任何地方工作.
但在这一点上,我没有真正的搜索结果可以帮助我.提前致谢.
试试这个,它可以在 SharpAVI 的库中找到。无论分辨率如何,它都可以在设备上正常运行。我在 Surface 3 上测试过,电量为 150%。
System.Windows.Media.Matrix toDevice;
using (var source = new HwndSource(new HwndSourceParameters()))
{
toDevice = source.CompositionTarget.TransformToDevice;
}
screenWidth = (int)Math.Round(SystemParameters.PrimaryScreenWidth * toDevice.M11);
screenHeight = (int)Math.Round(SystemParameters.PrimaryScreenHeight * toDevice.M22);
Run Code Online (Sandbox Code Playgroud)
SharpAVI 可以在这里找到: https: //github.com/baSSiLL/SharpAvi 它用于视频,但在获取每一帧时使用类似的 copyFromScreen 方法:
graphics.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(screenWidth, screenHeight));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2145 次 |
| 最近记录: |