我正在开发一个程序,选择某个范围(调整大小)并通过Winforms捕获它。但是,透明背景或白色会被识别为透明。
我在stackoverflow上搜索“CopyFromScreen”,发现了类似的现象,但没有表现出与我相同的症状。
我也尝试过Win32 API的“BitBlt”,但结果是一样的。
下面是我写的代码。
capturePanel.Controls.Clear();
this.TopMost = true;
this.pictureBox = new PictureBox();
this.pictureBox.Size = capturePanel.Size;
this.pictureBox.Location = new Point(0, 0);
this.pictureBox.BackColor = Color.White;
capturePanel.Controls.Add(this.pictureBox);
// capturePanel의 스크린 좌표 구하기
Point screenPoint = capturePanel.PointToScreen(Point.Empty);
// 스크린 캡처 로직
Bitmap bmp = new Bitmap(capturePanel.Width, capturePanel.Height);
using (Graphics g = Graphics.FromImage(bmp as Image))
{
g.CopyFromScreen(screenPoint.X, screenPoint.Y, 0, 0, capturePanel.Size);
}
// PictureBox에 완성된 이미지를 설정합니다.
pictureBox.Image = bmp;