1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-29 09:10:09 +00:00
msfs-popout-panel-manager/Provider/ImageOperation.cs

50 lines
1.8 KiB
C#
Raw Normal View History

2022-02-07 03:05:05 +00:00
using MSFSPopoutPanelManager.Shared;
using System;
2021-12-14 05:40:07 +00:00
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
2022-01-27 13:40:04 +00:00
namespace MSFSPopoutPanelManager.Provider
2021-12-14 05:40:07 +00:00
{
public class ImageOperation
{
public static Bitmap TakeScreenShot(IntPtr windowHandle)
{
2022-02-07 03:05:05 +00:00
if (!PInvoke.IsWindow(windowHandle))
throw new PopoutManagerException("Pop out windows were closed unexpectedly.");
2021-12-14 05:40:07 +00:00
// Set window to foreground so nothing can hide the window
PInvoke.SetForegroundWindow(windowHandle);
Thread.Sleep(300);
Rectangle rectangle;
PInvoke.GetWindowRect(windowHandle, out rectangle);
2022-02-07 03:05:05 +00:00
Rectangle clientRectangle;
PInvoke.GetClientRect(windowHandle, out clientRectangle);
2021-12-14 05:40:07 +00:00
2022-02-07 03:05:05 +00:00
// Take a screen shot by removing the titlebar of the window
var left = rectangle.Left;
var top = rectangle.Top + (rectangle.Height - clientRectangle.Height) - 8; // 8 pixels adjustments
var bmp = new Bitmap(clientRectangle.Width, clientRectangle.Height, PixelFormat.Format24bppRgb);
2021-12-14 05:40:07 +00:00
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(left, top), Point.Empty, rectangle.Size);
}
2022-02-07 03:05:05 +00:00
// Place the above image in the same canvas size as before
Bitmap backingImage = new Bitmap(rectangle.Width, rectangle.Height);
using (Graphics gfx = Graphics.FromImage(backingImage))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 0, 0)))
{
gfx.FillRectangle(brush, 0, 0, rectangle.Width, rectangle.Height);
gfx.DrawImage(bmp, new Point(0, top));
}
return backingImage;
2021-12-14 05:40:07 +00:00
}
}
}