【原创】Winform通用组件——自制的图片缩放与截取组件[含源码]
2011-04-13 20:04 刺客之家 阅读(3468) 评论(16) 编辑 收藏 举报前言
最近做一个项目,需要在Winform中做一个类似于现在很多网站的用户头像上传功能,需要对图像进行局部剪切和缩放功能。在网上搜了一下没找什么好的例子,只好自己动手试试写一个。
内容
一、开发环境
VS2008+.net2.0(考虑用户机器framework版本不高)
二、使用技术
GDI+,Winform基础
三、核心代码[源码重构中,完成后会发出]
图片缩放处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary>
/// 获取把图片按比例缩放到规定的大小以内的大小
/// </summary>
/// <param name="Image">目标图片</param>
/// <param name="maxSize">最大大小</param>
public static Size GetPictureLimitSize(Image img, Size maxSize)
{
Size size = new Size();
//如果需要缩放
if (img.Width > maxSize.Width ||
img.Height > maxSize.Height)
{
//获取高宽比
double hwRateNow = (Convert.ToDouble(img.Height) / img.Width);
double hwRateMax = (Convert.ToDouble(maxSize.Height) / maxSize.Width);
//如果高度要缩小
if (hwRateNow > hwRateMax)
{
size.Height = maxSize.Height;
size.Width = Convert.ToInt32(size.Height / hwRateNow);
}
//缩小宽度
else
{
size.Width = maxSize.Width;
size.Height = Convert.ToInt32(size.Width * hwRateNow);
}
}
//否则图片大小不变
else
{
size = img.Size;
}
return size;
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
{
try
{
//如果大小没变,则原样返回
if (bmp.Size.Width == newH && bmp.Height == newH)
{
return bmp;
}
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
// ===============================
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public static Image KiCut(Image b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}
if (StartX == 0 && StartY == 0 && iWidth == w && iHeight == h)
{
return b;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
鼠标移动处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 鼠标
/// <summary>
/// 鼠标移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImageCutAndZoom_MouseMove(object sender, MouseEventArgs e)
{
//首先更新鼠标状态
if (NowMainImageCanseeRegion.Contains(e.X, e.Y))
{
if (MouseStatus!= MouseState.Draging)
{
MouseStatus = MouseState.CanDrag;
}
}
else
{
MouseStatus = MouseState.None;
}
//鼠标在移动的时候,根据当前的鼠标状态进行逻辑处理
switch (MouseStatus)
{
case MouseState.Draging:
//计算鼠标移动的距离
Point offset = new Point(e.Location.X - MousePositonOld.X,e.Location.Y - MousePositonOld.Y);
MainImageRegion.Offset(offset);
this.Invalidate();
break;
default:
break;
}
MousePositonOld = e.Location;
}
/// <summary>
/// 鼠标按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImageCutAndZoom_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && (MouseStatus == MouseState.CanDrag || MouseStatus == MouseState.Draging))
{
MouseStatus = MouseState.Draging;
}
}
/// <summary>
/// 鼠标抬起
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImageCutAndZoom_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && MouseStatus == MouseState.Draging)
{
MouseStatus = MouseState.CanDrag;
}
}
#endregion
/// <summary>
/// 窗体载入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImageCutAndZoom_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 图片放大按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonBigger_Click(object sender, EventArgs e)
{
ResizeCount *= StepPerClick;
ResizeMainImageRegion();
}
/// <summary>
/// 图片缩小按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSmaller_Click(object sender, EventArgs e)
{
ResizeCount /= StepPerClick;
ResizeMainImageRegion();
}
/// <summary>
/// 确认按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOK_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 取消按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, EventArgs e)
{
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WinformParam.WM_NCLBUTTONDBLCLK)
{
return;
}
base.WndProc(ref m);
}
#endregion
四、Demo下载
五、效果预览
选择文件
图片中间是截取区域,左键点击图片可拖动
放大图片
缩小图片
截取图片