C#屏幕截图错误?

C# screenshot bug?

我使用以下代码进行截图:

var rc = SystemInformation.VirtualScreen;
Bitmap bmp = new Bitmap(rc.Width, rc.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rc.Left, rc.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

现在这很好用而且很容易使用,但是我总是在一些屏幕截图中遇到很少的 'white dots'。当它大量出现时,这可能会非常烦人并且会扭曲图像。

我设法缩小了问题的范围,当我尝试截取下图时,错误出现了:

屏幕截图的输出如下所示:

如何解决这个问题? 出于好奇,这是如何解释的?

在我的测试环境中,屏幕截图根本没有保存。我直接用下面的代码使用它:

pictureBox1.Image = bmp;

tl;dr 我正在尝试截取屏幕截图,但一些像素被替换为白色并扭曲了结果。

非常感谢您。

编辑:事实证明位图使区域透明(白色来自表单的背景色,感谢您发现这个花钱的人!)

但很明显,正如您在第一张图片中可以清楚地看到的那样;我不是要捕获任何透明内容。为什么要这样做?

编辑 2:

这是整个 class 我正在使用 select 我的屏幕截图:

public partial class SnippingTool : Form
{
    public static Image Snip()
    {
        var rc = SystemInformation.VirtualScreen;

        Bitmap bmp = new Bitmap(rc.Width, rc.Height);
        Graphics g = Graphics.FromImage(bmp);
        g.CopyFromScreen(rc.Left, rc.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

        var snipper = new SnippingTool(bmp);

        if (snipper.ShowDialog() == DialogResult.OK)
        {
            return snipper.Image;
        }

        return null;
    }

    public SnippingTool(Image screenShot)
    {
        InitializeComponent();
        this.BackgroundImage = screenShot;
        this.ShowInTaskbar = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.StartPosition = FormStartPosition.Manual;

        int screenLeft = SystemInformation.VirtualScreen.Left;
        int screenTop = SystemInformation.VirtualScreen.Top;
        int screenWidth = SystemInformation.VirtualScreen.Width;
        int screenHeight = SystemInformation.VirtualScreen.Height;

        this.Size = new System.Drawing.Size(screenWidth, screenHeight);
        this.Location = new System.Drawing.Point(screenLeft, screenTop);


        this.DoubleBuffered = true;
    }

    public Image Image { get; set; }

    private Rectangle rcSelect = new Rectangle();
    private Point pntStart;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        pntStart = e.Location;
        rcSelect = new Rectangle(e.Location, new Size(0, 0));
        this.Invalidate();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        int x1 = Math.Min(e.X, pntStart.X);
        int y1 = Math.Min(e.Y, pntStart.Y);
        int x2 = Math.Max(e.X, pntStart.X);
        int y2 = Math.Max(e.Y, pntStart.Y);
        rcSelect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
        this.Invalidate();
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (rcSelect.Width <= 0 || rcSelect.Height <= 0) return;
        Image = new Bitmap(rcSelect.Width, rcSelect.Height);
        using (Graphics gr = Graphics.FromImage(Image))
        {
            gr.DrawImage(this.BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height),
                rcSelect, GraphicsUnit.Pixel);
        }
        DialogResult = DialogResult.OK;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (Brush br = new SolidBrush(Color.FromArgb(120, Color.Black)))
        {
            int x1 = rcSelect.X; int x2 = rcSelect.X + rcSelect.Width;
            int y1 = rcSelect.Y; int y2 = rcSelect.Y + rcSelect.Height;
            e.Graphics.FillRectangle(br, new Rectangle(0, 0, x1, this.Height));
            e.Graphics.FillRectangle(br, new Rectangle(x2, 0, this.Width - x2, this.Height));
            e.Graphics.FillRectangle(br, new Rectangle(x1, 0, x2 - x1, y1));
            e.Graphics.FillRectangle(br, new Rectangle(x1, y2, x2 - x1, this.Height - y2));
        }
        using (Pen pen = new Pen(Color.Red, 3))
        {
            e.Graphics.DrawRectangle(pen, rcSelect);
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Escape) this.DialogResult = DialogResult.Cancel;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

然后在我的表格上:

pictureBox1.Image = SnippingTool.Snip();

对于遇到此问题的任何人;这是最终适合我的配置:

var rc = SystemInformation.VirtualScreen;

using (Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppRgb))
{
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.CopyFromScreen(rc.Left, rc.Top, 0, 0, bmp.Size);
    }
    using (var snipper = new SnippingTool(bmp))
    {
        if (snipper.ShowDialog() == DialogResult.OK)
        {
            return snipper.Image;
        }
    }
    return null;
}

问题是我们在 ARGB 中捕获屏幕,捕获的图像会带有透明度。

然后我们将其保存为JPG,透明度将变为白色。

要解决这个问题,我们只需要使用不包含 alpha 的显式 PixelFormat 实例化位图:

Bitmap screenBmp = new Bitmap(width, height, PixelFormat.Format32bppRgb)