解决方案
- 通过创建一个透明的Form窗体
- 检测鼠标按下(MouseDown)、抬起(MouseMove)、移动(MouseUp)事件
- 添加窗体右键事件(截图完成后方便显示可操作的功能)
- 准备一个图片展示控件(PictureBox)
- 具体做法
1.鼠标按下时将PictureBox定位到鼠标按下的坐标(Cursor.Position)
2.鼠标按下移动时计算PictureBox的坐标与鼠标的坐标从而改变PictureBox的大小(Size)
3.鼠标抬起时绘制图片 并 显示右键菜单
效果展示
解决问题
- ScreenshotForm是创建的Form窗体,没有添加任何控件
- ScaleMoveControl通过右侧超链接查看控制控件的缩放、移动 的类
- ScreenHelper过右侧超链接查看屏幕帮助类
- 代码需要自行做一些相应修改才可使用,诸位拿到代码后问题想做修改问题应该大不
public partial class ScreenshotForm : Form
{
public ScreenshotForm()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.Opacity = 0.5;
this.BackColor = Color.Black;
this.WindowState = FormWindowState.Maximized;
AddContextMenuStrip();
AddMouseScreenshotClick();
}
#region 自定义属性、事件
public System.Drawing.Bitmap Bitmap { get; private set; }
public System.Drawing.Point UpperLeftSource { get; private set; }
public event Action ClickSuccessEvent;
public event Action ClickExitEvent;
#endregion
#region 添加右键菜单
private void AddContextMenuStrip()
{
this.ContextMenuStrip = new ContextMenuStrip();
this.ContextMenuStrip.Items.Add(new ToolStripMenuItem("操作图片(双击图片完成操作)", null, (sender, e) =>
{
ClearImage();
pictureBox.BorderStyle = BorderStyle.FixedSingle;
scaleMoveControl.IsCanOperate = true;
}));
this.ContextMenuStrip.Items.Add(new ToolStripMenuItem("保存", null, (sender, e) =>
{
var bitmap = GetBitmap();
if (bitmap == null)
{
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "图片|*.jpg;*.jpeg;*.png;";
saveFileDialog.DefaultExt = ".jpg";
saveFileDialog.AddExtension = true;
var Result = saveFileDialog.ShowDialog();
if (!Result.Equals(DialogResult.OK))
{
return;
}
var fileName = saveFileDialog.FileName;
var path = System.IO.Path.GetDirectoryName(fileName);
System.IO.Directory.CreateDirectory(path);
var Extension = System.IO.Path.GetExtension(path);
if (Extension.Equals(".jpg", StringComparison.CurrentCultureIgnoreCase) || Extension.Equals(".jpeg", StringComparison.CurrentCultureIgnoreCase))
{
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (Extension.Equals(".png", StringComparison.CurrentCultureIgnoreCase))
{
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
else
{
bitmap.Save(fileName);
}
}));
this.ContextMenuStrip.Items.Add(new ToolStripMenuItem("完成(图片复制到剪切板)", null, (sender, e) =>
{
var bitmap = GetBitmap();
if (bitmap != null)
{
Clipboard.Clear();
Clipboard.SetImage(bitmap);
}
if (ClickSuccessEvent != null)
{
ClickSuccessEvent();
}
}));
this.ContextMenuStrip.Items.Add(new ToolStripMenuItem("退出", null, (sender, e) =>
{
this.Close();
if (ClickExitEvent != null)
{
ClickExitEvent();
}
}));
}
#endregion
#region 设置鼠标点击事件
PictureBox pictureBox = new PictureBox();
ScaleMoveControl scaleMoveControl { get; set; }
private void AddMouseScreenshotClick()
{
#region 截图代码(原始版本已注释)
#endregion
pictureBox.Visible = true;
this.Controls.Add(pictureBox);
this.MouseDown += ScreenshotForm_MouseDown;
pictureBox.MouseDown += ScreenshotForm_MouseDown;
this.MouseMove += (sender, e) =>
{
if (scaleMoveControl.IsCanOperate)
{
return;
}
if (!e.Button.Equals(MouseButtons.Left))
{
return;
}
var MouseMovePosition = Cursor.Position;
var size = new System.Drawing.Size(Math.Abs(MouseMovePosition.X - pictureBox.Location.X), Math.Abs(MouseMovePosition.Y - pictureBox.Location.Y));
if (size.Width <= 0 || size.Height <= 0)
{
return;
}
pictureBox.Size = size;
};
this.MouseUp += (sender, e) =>
{
if (scaleMoveControl.IsCanOperate)
{
return;
}
if (!e.Button.Equals(MouseButtons.Left))
{
return;
}
var MouseUpPosition = Cursor.Position;
RefreshImage();
this.ContextMenuStrip.Show(MouseUpPosition);
};
scaleMoveControl = new ScaleMoveControl(pictureBox);
scaleMoveControl.IsCanOperate = false;
pictureBox.DoubleClick += (sender, e) =>
{
scaleMoveControl.IsCanOperate = false;
RefreshImage();
this.ContextMenuStrip.Show(Cursor.Position);
};
}
private void ScreenshotForm_MouseDown(object sender, MouseEventArgs e)
{
if (scaleMoveControl.IsCanOperate)
{
return;
}
if (!e.Button.Equals(MouseButtons.Left))
{
return;
}
ClearImage();
pictureBox.Size = System.Drawing.Size.Empty;
pictureBox.Location = Cursor.Position;
pictureBox.BorderStyle = BorderStyle.FixedSingle;
}
private void RefreshImage()
{
if (pictureBox.Width <= 0 || pictureBox.Height <= 0)
{
ClearImage();
return;
}
pictureBox.Refresh();
var Size = pictureBox.Size;
pictureBox.Image = ScreenHelper.CopyScreenToImage(pictureBox.Location, Size);
pictureBox.BorderStyle = BorderStyle.None;
}
private void ClearImage()
{
if (pictureBox.Image != null)
{
pictureBox.Image.Dispose();
pictureBox.Image = null;
}
}
#endregion
#region 拿到当前截取的图片
protected System.Drawing.Bitmap GetBitmap()
{
this.Close();
if (pictureBox.Image != null && !pictureBox.Size.Equals(System.Drawing.Size.Empty))
{
var bitmap = ScreenHelper.CopyScreenToImage(pictureBox.Location, pictureBox.Size);
this.Bitmap = bitmap;
this.UpperLeftSource = pictureBox.Location;
this.DialogResult = DialogResult.OK;
return bitmap;
}
return null;
}
#endregion
public static ScreenshotForm ShowForm()
{
ScreenshotForm screenshotForm = new ScreenshotForm();
screenshotForm.Show();
return screenshotForm;
}
}