C#屏幕截图问题?

14

我使用以下代码来截屏:

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);
现在这个功能非常好用且易于使用,但是我经常会在一些截屏中遇到小的“白点”。当这种现象出现在大量屏幕截图中时,这可能非常烦人并且会扭曲图像。
我成功地缩小了问题,并发现当我尝试截取以下图像时,该错误会发生: bug-causing image 截屏的输出结果如下: bug 你如何解决这个问题?另外,你能解释一下这是怎么回事吗?
在我的测试环境中,截屏根本没有被保存。 我直接使用以下代码:
pictureBox1.Image = bmp;

太长不看,我想要截取屏幕截图,但是有些像素被替换成白色并且扭曲了结果。

非常感谢你的帮助。

编辑:结果发现位图使区域透明(白色来自表单的背景颜色,感谢spender指出!)

bug with different background

但是很明显,就像你可以在第一张图片中清晰地看到的那样,我并没有尝试捕捉任何透明内容。为什么会这样呢?

编辑2:

这是我用来选择我的屏幕截图的整个类:

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();

6
这是否涉及视频播放器? - CodesInChaos
1
我想确认一下,您是试图对黑屏进行截屏,但生成的截屏却是白色的? - James
4
这似乎是捕获的图像具有透明度。假设 pictureBox1 的背景颜色设置为黑色? - spender
3
对我来说,这似乎是视频驱动程序的一个错误,请寻找更新。 CopyFromScreen() 也不完全干净,你可以尝试使用这段代码。但我怀疑这不是问题所在。 - Hans Passant
2
@John:尝试使用new Bitmap(rc.Width, rc.Height, PixelFormat.Format24bppRgb); - leppie
显示剩余20条评论
2个回答

3

对于遇到此问题的任何人; 这是最终为我正常工作的配置:

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;
}

请注意SnippingTool.Snip()辅助方法。 - Hans Passant

1
问题在于我们以ARGB格式捕获屏幕,所捕获的图像会带有透明度。

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

为了解决这个问题,我们只需要使用不包括alpha通道的显式PixelFormat来实例化Bitmap即可:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接