未来の自分へ知識谷金

ゆーきのエンジニアブログ
C#Visual Studioプログラミング

【C#】画面の一部をスクリーンショットする

.NETBitmapC#Visual StudioVisual Studio 2022

■デスクトップ上の領域をスクリーンショットするコードを記載します。

public Bitmap CaptureScreen(Rectangle screenRect)
        {
            Bitmap bmp = new Bitmap(screenRect.Size.Width, screenRect.Size.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(screenRect.Location, new Point(0, 0), bmp.Size);
            }
            return bmp;
        }

前回の記事と組み合わせるとデスクトップ上のある領域をユーザーが選択し、

その画像を取得できるようになります。

以上!