C#截图错误?
我使用以下代码截图:C#截图错误?,c#,screenshot,C#,Screenshot,我使用以下代码截图: 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); public partial class SnippingT
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);
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 = bmp;
tl;博士,我正在尝试截图,一些像素被替换为白色,并扭曲了结果
事先非常感谢
编辑:位图使该区域透明(白色来自表单的背景色,感谢您发现了这个挥霍者!)
但很明显,正如你在第一张图片中清楚地看到的那样;我不想捕捉任何透明的内容。为什么会这样
编辑2:
这是我用来选择截图的整个类:
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);
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);
}
}
对于任何遇到这个问题的人;这是最终对我来说运行良好的配置:
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的显式像素格式实例化位图:
Bitmap screenBmp = new Bitmap(width, height, PixelFormat.Format32bppRgb)
这涉及到视频播放器吗?我很清楚,你正在尝试拍摄一张黑屏截图,但生成时它是白色的?这看起来像是拍摄的图像具有透明度。假设
pictureBox1
的背景色设置为黑色?在我看来像是一个视频驱动程序错误,请查找更新。CopyFromScreen()也不完全干净,您可以试试。但我怀疑这就是问题所在。@John:试试新位图(rc.Width,rc.Height,PixelFormat.Format24bppRgb)代码>注意助手方法。