小弟做了一个小小的屏幕截图程序,程序基本运行正常。其基本思路是:在按下notifyIcon的弹出菜单的截图后,截取当前屏幕,并显示在Form中的PictureBox中,拖动鼠标选择截图区域,然后保存,然后隐藏窗体。
但是在拖动鼠标,显示选择区域时(想实时更新选择框),矩形框有时只有在松开鼠标后才显示,有时又可正常显示,不知问题所在。
源码如下,求高手相助!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} bool flag = false;
Point firstpoint;
Point secondpoint;
Image cachImage = null; //加载窗体时
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
} //按下鼠标时
void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (flag)
{
flag = false; int x1 = Math.Min(firstpoint.X, secondpoint.X);
int x2 = Math.Max(firstpoint.X, secondpoint.X);
int y1 = Math.Min(firstpoint.Y, secondpoint.Y);
int y2 = Math.Max(firstpoint.Y, secondpoint.Y); if (x1 != x2 && y1 != y2)
{
//方法一,有红边
//Image img = new Bitmap(x2 - x1, y2 - y1);
//Graphics g = Graphics.FromImage(img);
//g.CopyFromScreen(x1, y1, 0, 0, new Size(x2 - x1, y2 - y1));
//img.Save("C:\\test.jpg"); //方法二,这个方法没有红边
Image img = new Bitmap(x2 - x1, y2 - y1);
Graphics g = Graphics.FromImage(img);
g.DrawImage(cachImage, 0, 0, new Rectangle(x1, y1, x2 - x1, y2 - y1), GraphicsUnit.Pixel);
img.Save("C:\\test.jpg"); //存到剪贴板, 将 System.Windows.Forms.DataFormats.Bitmap 格式的 System.Drawing.Image 添加到剪贴板中
Clipboard.SetImage(img); MessageBox.Show("保存在C:\\test.jpg\r\n" + "宽:" + (x2 - x1).ToString() + "\r\n" + "高:" + (y2 - y1).ToString(), "截图成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Visible = false;
this.WindowState = FormWindowState.Minimized;
}
}
} //拖动鼠标时
void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (flag)
{
secondpoint = new Point(e.X, e.Y); int x1 = Math.Min(firstpoint.X, secondpoint.X);
int x2 = Math.Max(firstpoint.X, secondpoint.X);
int y1 = Math.Min(firstpoint.Y, secondpoint.Y);
int y2 = Math.Max(firstpoint.Y, secondpoint.Y); Image img = new Bitmap(cachImage);
Graphics g = Graphics.FromImage(img);
//g.Clear(Color.Beige);
g.DrawRectangle(Pens.Red, new Rectangle(x1, y1, x2 - x1, y2 - y1));
pictureBox1.BackgroundImage = img;
}
} //松开鼠标时
void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
flag = true;
firstpoint= new Point(e.X, e.Y);
} //退出
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} //截图
private void 截图ToolStripMenuItem_Click(object sender, EventArgs e)
{
//Rectangle r = Screen.PrimaryScreen.Bounds; //只截取工作区域
Rectangle r = Screen.PrimaryScreen.WorkingArea;
Image img = new Bitmap(r.Width, r.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), r.Size); this.Visible = true;
this.WindowState = FormWindowState.Normal;
//窗体最大化,及相关处理
this.Width = r.Width;
this.Height = r.Height;
this.Left = 0;
this.Top = 0; pictureBox1.Width = r.Width;
pictureBox1.Height = r.Height;
pictureBox1.BackgroundImage = img; //将当前截得的图保存到缓存
cachImage = img; } private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
}
}
但是在拖动鼠标,显示选择区域时(想实时更新选择框),矩形框有时只有在松开鼠标后才显示,有时又可正常显示,不知问题所在。
源码如下,求高手相助!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} bool flag = false;
Point firstpoint;
Point secondpoint;
Image cachImage = null; //加载窗体时
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
} //按下鼠标时
void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (flag)
{
flag = false; int x1 = Math.Min(firstpoint.X, secondpoint.X);
int x2 = Math.Max(firstpoint.X, secondpoint.X);
int y1 = Math.Min(firstpoint.Y, secondpoint.Y);
int y2 = Math.Max(firstpoint.Y, secondpoint.Y); if (x1 != x2 && y1 != y2)
{
//方法一,有红边
//Image img = new Bitmap(x2 - x1, y2 - y1);
//Graphics g = Graphics.FromImage(img);
//g.CopyFromScreen(x1, y1, 0, 0, new Size(x2 - x1, y2 - y1));
//img.Save("C:\\test.jpg"); //方法二,这个方法没有红边
Image img = new Bitmap(x2 - x1, y2 - y1);
Graphics g = Graphics.FromImage(img);
g.DrawImage(cachImage, 0, 0, new Rectangle(x1, y1, x2 - x1, y2 - y1), GraphicsUnit.Pixel);
img.Save("C:\\test.jpg"); //存到剪贴板, 将 System.Windows.Forms.DataFormats.Bitmap 格式的 System.Drawing.Image 添加到剪贴板中
Clipboard.SetImage(img); MessageBox.Show("保存在C:\\test.jpg\r\n" + "宽:" + (x2 - x1).ToString() + "\r\n" + "高:" + (y2 - y1).ToString(), "截图成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Visible = false;
this.WindowState = FormWindowState.Minimized;
}
}
} //拖动鼠标时
void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (flag)
{
secondpoint = new Point(e.X, e.Y); int x1 = Math.Min(firstpoint.X, secondpoint.X);
int x2 = Math.Max(firstpoint.X, secondpoint.X);
int y1 = Math.Min(firstpoint.Y, secondpoint.Y);
int y2 = Math.Max(firstpoint.Y, secondpoint.Y); Image img = new Bitmap(cachImage);
Graphics g = Graphics.FromImage(img);
//g.Clear(Color.Beige);
g.DrawRectangle(Pens.Red, new Rectangle(x1, y1, x2 - x1, y2 - y1));
pictureBox1.BackgroundImage = img;
}
} //松开鼠标时
void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
flag = true;
firstpoint= new Point(e.X, e.Y);
} //退出
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} //截图
private void 截图ToolStripMenuItem_Click(object sender, EventArgs e)
{
//Rectangle r = Screen.PrimaryScreen.Bounds; //只截取工作区域
Rectangle r = Screen.PrimaryScreen.WorkingArea;
Image img = new Bitmap(r.Width, r.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), r.Size); this.Visible = true;
this.WindowState = FormWindowState.Normal;
//窗体最大化,及相关处理
this.Width = r.Width;
this.Height = r.Height;
this.Left = 0;
this.Top = 0; pictureBox1.Width = r.Width;
pictureBox1.Height = r.Height;
pictureBox1.BackgroundImage = img; //将当前截得的图保存到缓存
cachImage = img; } private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货