How can I capture a screen with a white background or a transparent screen with C# Winforms?

151 views Asked by At

I am developing a program that selects a certain range (adjusts the size) and captures it through Winforms. However, a transparent background or white color is recognized as transparent.

I searched for 'CopyFromScreen' on stackoverflow and found a similar phenomenon, but it did not show the same symptoms as mine.

I also tried 'BitBlt' of the Win32 API, but the result was the same.

Below is the code I wrote.

            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;

enter image description here

1

There are 1 answers

1
Node defender On BEST ANSWER

Next to your question, how to make the entire form transparent.

You can use TransparencyKey, but you need to set the form's BackColor property to the same color as the TransparencyKey

Here is a sample code:

 this.BackColor = Color.Magenta;

 this.TransparencyKey = Color.Magenta;

The effect is as follows:

enter image description here