using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Reflection;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string strFilename = "";
OpenFileDialog dlgOpen = null;
try
{
dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "All Image Files(*.*)|*.*";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
if (dlgOpen.FileName == "") return;
strFilename = dlgOpen.FileName;
pictureBox1.Image = Image.FromFile(strFilename);
this.SetImageSizeMode(pictureBox1);
}
}
catch (Exception ex)
{
MessageBox.Show("无法识别的图片格式(" + strFilename + ")\r\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (dlgOpen != null)
dlgOpen.Dispose();
dlgOpen = null;
}
}
/********************************************************************************************************/
private void SetImageSizeMode(PictureBox pImage)
{
if (pImage == null) return;
Image imgResult = pImage.Image;
if (imgResult != null)
{
//控制图片显示方式
if (imgResult.Width > pImage.Width || imgResult.Height > pImage.Height)
{
pImage.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
pImage.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
}
/*****************************************************************************************************/
#region 绘制及剪切
Point fPtStart; //开始位置矩形的位置
bool fHaveImage = false; //是否有图片
Point fPtFirst; //鼠标按下去的位置
Point fPtSecond; //鼠标当前位置
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
fPtFirst = new Point(e.X, e.Y);
fPtStart = new Point(e.X, e.Y);
if ((sender as PictureBox).Image != null)
{
this.fHaveImage = true;
}
else
{
this.fHaveImage = false;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Cursor == Cursors.Cross)
{
fPtSecond = new Point(e.X, e.Y);
if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的右下角
fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
}
else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
{
//鼠标当前位置在,按下去位置的右上角
fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
}
else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的左下角
fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
}
else
{
//鼠标当前位置在,按下去位置的左上角
fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
}
pictureBox1.Invalidate();
}
}
/*******************************************************************************************************/
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (this.fHaveImage == false) return;
try
{
fPtSecond = new Point(e.X, e.Y);
if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的右下角
fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
}
else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
{
//鼠标当前位置在,按下去位置的右上角
fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
}
else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的左下角
fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
}
else
{
//鼠标当前位置在,按下去位置的左上角
fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
}
Rectangle rectDisplay = this.GetPictureDisplaySize(pictureBox1); //图片实际显示的大小
int Width = Math.Abs((fPtSecond.X - fPtFirst.X));
int Height = Math.Abs((fPtSecond.Y - fPtFirst.Y));
double dWRate = pictureBox1.Image.Width / (double)rectDisplay.Width; //缩放比例:宽
double dHRate = pictureBox1.Image.Height / (double)rectDisplay.Height; //缩放比例:高
int intRealWidth = (int)(dWRate * Width); //实际需要截取的图片宽度
int intRealHeight = (int)(dHRate * Height); //实际需要截取的图片高度
int intRealX = (int)((fPtStart.X - rectDisplay.X) * dWRate); //实际的X坐标
int intRealY = (int)((fPtStart.Y - rectDisplay.Y) * dHRate); //实际的Y坐标
Bitmap bmpDest = new Bitmap(intRealWidth, intRealHeight, PixelFormat.Format32bppRgb); //目标图片大小
Graphics g = Graphics.FromImage(bmpDest); //创建GDI
Rectangle rectDest = new Rectangle(0, 0, intRealWidth, intRealHeight);
Rectangle rectSource = new Rectangle(intRealX, intRealY, intRealWidth, intRealHeight);
g.DrawImage(pictureBox1.Image, rectDest, rectSource, GraphicsUnit.Pixel); //绘图
pictureBox2.Image = (Image)bmpDest;
g.Dispose();
SetImageSizeMode(pictureBox2);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{ this.Cursor = Cursors.Default; }
}
private Rectangle GetPictureDisplaySize(PictureBox pbxImage)
{
if (pbxImage != null)
{
PropertyInfo ppiImageRect = pbxImage.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
return (Rectangle)ppiImageRect.GetValue(pbxImage, null);
}
return new Rectangle(0, 0, 1, 1);
}
/***********************************************************************************/
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int intWidth = 0;
int intHeight = 0;
if (this.fHaveImage == true)
{
Pen p = new Pen(Color.Black, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
intWidth = Math.Abs(fPtFirst.X - fPtSecond.X);
intHeight = Math.Abs(fPtFirst.Y - fPtSecond.Y);
Rectangle rectDraw = new Rectangle(fPtStart, new Size(intWidth, intHeight));
e.Graphics.DrawRectangle(p, rectDraw);
}
}
/***************************************************************************************************/
#endregion
/// <summary>
/// 保存剪切后的图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();
if (dlgSave.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image.Save(dlgSave.FileName);
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
}
}
c# 图片截图
原创wx604f04a92c6fd 博主文章分类:opencv进化之旅 ©著作权
©著作权归作者所有:来自51CTO博客作者wx604f04a92c6fd的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:插入排序之算法研究
下一篇:人脸检测or五官检测
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# 屏幕截图
C# 屏幕截图
C# 屏幕截图 System Windows Linq -
C# 桌面截图工具 demo
自动截屏 并保存在 某个目录下。截图:工具下载地址:http://pan.baid
下载地址 .net 源代码下载 -
C# 简单实现QQ截图功能
接上一篇写的截取电脑屏幕,我们在原来的基础上加一个选择区域的功能,实现自定义选择截图。个人比较懒,上一篇的代
sed desktop 缩放比例 -
C# 图片缩略
/// <summary> /// 无损压缩图片 /// </summary> /// <para
C# 图片缩略 图片压缩 html 缩放 hive -
C# 剪裁图片
【代码】C# 剪裁图片。
c# System Image