【发布时间】:2015-01-26 12:47:25
【问题描述】:
我使用以下代码截图:
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();
【问题讨论】:
-
这是否涉及视频播放器?
-
我很清楚,您正在尝试截取黑屏的屏幕截图,但生成时它是白色的?
-
这看起来更像是捕获的图像具有透明度。假设
pictureBox1的背景颜色设置为黑色? -
在我看来像一个视频驱动程序错误,寻找更新。 CopyFromScreen() 也不是完全干净的,你可以试试this code。但我怀疑这就是这里的问题。
-
@John:试试
new Bitmap(rc.Width, rc.Height, PixelFormat.Format24bppRgb);
标签: c# screenshot