我使用以下代码截取屏幕截图:
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);
现在这种方法效果非常好,并且很容易使用,但是我总是在一些屏幕截图中遇到小的“白点”。这可能非常烦人,并且当图像大量出现时会使图像失真。
我设法缩小了问题的范围,当我尝试截取以下图片时,会发生错误:
屏幕截图的输出如下所示:
你怎么解决这个问题? 出于好奇,这是如何解释的?
在我的测试环境中,屏幕截图根本没有保存。我直接使用它与以下代码:
pictureBox1.Image = bmp;
tl; dr我正在尝试截取屏幕截图,部分像素被白色替换并扭曲结果。
非常感谢你。
编辑:事实证明,位图使区域透明(白色来自表格的背景颜色,感谢发现这个花钱!)
但显然你可以在第一张照片中清楚地看到;我不是想捕捉任何透明的内容。为什么这样做?
EDIT2:
这是我用来选择屏幕截图的全班:
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();
答案 0 :(得分: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;
}
答案 1 :(得分:0)
问题是我们在ARGB中捕获屏幕,捕获的图像会带有透明度。
然后我们将其保存为JPG,透明度将变为白色。
要解决这个问题,我们只需要使用不包含alpha的显式PixelFormat来实例化Bitmap:
Bitmap screenBmp = new Bitmap(width, height, PixelFormat.Format32bppRgb)