using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms; namespace Aping.Common.Windows.Forms
{
public partial class CutImageBox : System.Windows.Forms.PictureBox
{
public CutImageBox()
{
InitializeComponent();
} public CutImageBox(IContainer container)
{
container.Add(this); InitializeComponent();
} //自定义裁图控件
//线条颜色
#region 字段
private Color borderColor = Color.Red;//线条颜色
private bool canResize = true;//是否能够改变截图框的大小
private int side = ;//小框框的大小
private int alpha = ;//遮罩层透明度
private bool showCutArea = true;//是否展示截图框
private Rectangle CutImageArea = new Rectangle(, , , );
private Rectangle[] Lites = new Rectangle[];
private AreaAlign cutAreaAlign = AreaAlign.Default;
private int miniWidth = ;//最小宽
private int miniHeight = ;//最小高
private bool ownerDraw = false;
private bool autoHide = false;
private System.Drawing.Drawing2D.DashStyle dashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
#endregion #region 属性
[Description("设置截图框边框的颜色。")]
public Color AreaBoderColor { get { return borderColor; } set { borderColor = value; Invalidate(); } }
[Description("是否可以改变截图框的大小。")]
public bool EnableResize { get { return canResize; } set { canResize = value; } }
//小方框的大小
[Description("是否显示截图框,默认为显示。")]
public bool ShowCutArea { get { return showCutArea; } set { showCutArea = value; Invalidate(); } }
[Description("截图框的透明度,默认值:50。")]
public int AreaAlpha { get { return alpha; } set { alpha = value; Invalidate(); } }
[Description("截图框加载后显示的位置,默认为左上角。")]
public AreaAlign CutAreaAlign
{
get { return cutAreaAlign; }
set
{
cutAreaAlign = value;
switch (cutAreaAlign)
{
case AreaAlign.Default:
CutImageArea = new Rectangle(, , miniWidth, miniHeight);
break;
case AreaAlign.Center:
CutImageArea = new Rectangle((Width - miniWidth) / , (Height - miniHeight) / , miniWidth, miniHeight);
break;
case AreaAlign.RightCenter:
CutImageArea = new Rectangle(Width - CutImageArea.Width - , (Height - miniHeight) / , miniWidth, miniHeight);
break;
case AreaAlign.RightTop:
CutImageArea = new Rectangle(Width - CutImageArea.Width - , , miniWidth, miniHeight);
break;
case AreaAlign.LeftCenter:
CutImageArea = new Rectangle(, (Height - miniHeight) / , miniWidth, miniHeight);
break;
case AreaAlign.LeftBottom:
CutImageArea = new Rectangle(, Height - CutImageArea.Width - , miniWidth, miniHeight);
break;
case AreaAlign.RightBottom:
CutImageArea = new Rectangle(Width - CutImageArea.Width - , Height - CutImageArea.Width - , miniWidth, miniHeight);
break;
default:
break;
}
Invalidate();
}
}
[Description("截图框宽的最小值,默认值:120。")]
public int MiniWidth { get { return miniWidth; } set { miniWidth = value; } }
[Description("截图框高的最小值,默认值:160。")]
public int MiniHeight { get { return miniHeight; } set { miniHeight = value; } }
[Description("开启手动绘图,EnableResize 必须开启。")]
public bool EnableOwnerDraw { get { return ownerDraw && canResize; } set { ownerDraw = value; Invalidate(); } }
[Description("是否在截完图后自动隐藏。")]
public bool AutoHide { get { return autoHide; } set { autoHide = value; } }
[Description("绘图框边框的样式,不要使用customs。")]
public System.Drawing.Drawing2D.DashStyle AreaBorder { get { return dashStyle; } set { if (value != System.Drawing.Drawing2D.DashStyle.Custom)dashStyle = value; Invalidate(); } }
#endregion #region 参数
private Point MouseDownPoint;
private Point MouseDownPointToScreen;
private Point CutImageAreaLocationToScreen;
private Point CutImageOldLocation;
private Size OldCutImageAreaSize;
private MouseDirection mouseDirection = MouseDirection.NONE;
private System.Drawing.Point RectStartPoint;
private System.Drawing.Point RectEndPoint; #endregion #region 方法
/// <summary>
/// 判断某个点是否在指定的区域内
/// </summary>
/// <param name="leftup">左上角的点</param>
/// <param name="rightdown">右下角的点</param>
/// <param name="compare">需要判断的点</param>
/// <returns>
/// 是 :true 否 false
/// </returns>
private bool IsInArea(Point leftup, Point rightdown, Point compare)
{
if (compare.X > leftup.X && compare.Y > leftup.Y && compare.X < rightdown.X && compare.Y < rightdown.Y)
{
return true;
}
return false;
}
/// <summary>
/// 判断某个点是否在指定的区域内
/// </summary>
/// <param name="leftup">左上角的点</param>
/// <param name="width">区域宽</param>
/// <param name="height">区域高</param>
/// <param name="compare">需要判断的点</param>
/// <returns>
/// 是 :true 否 false
/// </returns>
private bool IsInArea(Point leftup, int width, int height, Point compare)
{
if (compare.X > leftup.X && compare.Y > leftup.Y && compare.X < leftup.X + width && compare.Y < leftup.Y + height)
{
return true;
}
return false;
}
//图像裁剪
public Bitmap GetPartOfImage(Bitmap sourceImg, int width, int height, int offsetX, int offsetY)
{
Bitmap sourceBitmap = sourceImg; Bitmap resultBitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
Rectangle resultRectangle = new Rectangle(, , width, height);
Rectangle sourceRectangle = new Rectangle( + offsetX, + offsetY, width, height); //万能的drawImage函数啊,七七八八的参数非常多 普通的图片拉伸 放大 等效果都可以用它来做到。
//第一个参数:原图(被裁剪的图)
//第二个参数:目标Image(裁剪后的图)
//第三个参数:原图被裁剪的区域
//第四个参数:单位(当然是像素啦)
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return resultBitmap;
}
bool CanMove()
{
bool flag = true;
if (CutImageArea.Width >= Width)
flag = false;
if (CutImageArea.Height >= Height)
flag = false;
if (CutImageArea.Width <= MiniWidth)
flag = false;
if (CutImageArea.Height <= MiniHeight)
flag = false;
return flag;
}
#endregion #region 重写的方法
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Image == null)
throw new ArgumentNullException("aping extends control throws exception error message : image is null");
//处理裁图
Bitmap cutted = GetPartOfImage((Bitmap)Image, CutImageArea.Width, CutImageArea.Height, CutImageArea.X, CutImageArea.Y);
Image = cutted;
if (autoHide)
{
Lites = new Rectangle[];
showCutArea = false;
Invalidate();
}
if (OnCuttedImage != null)
OnCuttedImage(Image);
}
protected override void CreateHandle()
{
base.CreateHandle();
}
protected override void OnLoadCompleted(AsyncCompletedEventArgs e)
{
base.OnLoadCompleted(e); }
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Up)
CutImageArea.Location = new Point(CutImageOldLocation.X, CutImageOldLocation.Y + );
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
mouseDirection = MouseDirection.NONE;
} protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e); if (mouseDirection != MouseDirection.NONE && mouseDirection != MouseDirection.ALL)
{
DoResponse(e);
return;
}
//
/* N
* W E
* S
* */
Point epoint = e.Location;
#region 改变鼠标样式 if (IsInArea(new Point(Lites[].X + side, Lites[].Y + side), Lites[].Location, epoint))
{ Cursor = Cursors.SizeAll; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNWSE; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNWSE; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNS; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNS; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNESW; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeNESW; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeWE; }
else if (IsInArea(Lites[].Location, side, side, epoint))
{ Cursor = Cursors.SizeWE; }
else
{ Cursor = Cursors.Default; } #endregion
//当鼠标左键按下去了
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//在可拖动的区域内
if (IsInArea(new Point(Lites[].X + side, Lites[].Y + side), Lites[].Location, epoint))
{ mouseDirection = MouseDirection.ALL; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↖
{ mouseDirection = MouseDirection.NW; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↘
{ mouseDirection = MouseDirection.SE; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↑
{ mouseDirection = MouseDirection.N; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↓
{ mouseDirection = MouseDirection.S; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↗
{ mouseDirection = MouseDirection.NE; }
else if (IsInArea(Lites[].Location, side, side, epoint))//↙
{ mouseDirection = MouseDirection.SW; }
else if (IsInArea(Lites[].Location, side, side, epoint))//→
{ mouseDirection = MouseDirection.E; }
else if (IsInArea(Lites[].Location, side, side, epoint))//←
{ mouseDirection = MouseDirection.W; }
else
{ mouseDirection = MouseDirection.NONE; } if (mouseDirection != MouseDirection.NONE && mouseDirection != MouseDirection.ALL)
DoResponse(e);
if (mouseDirection == MouseDirection.ALL)
{
Point l = CutImageOldLocation;
Point epointToScreen = PointToScreen(e.Location);
l.Offset(epointToScreen.X - MouseDownPointToScreen.X, epointToScreen.Y - MouseDownPointToScreen.Y);
CutImageArea.Location = l;
//
if (CutImageArea.Location.X <= (side / + ))
CutImageArea.Location = new Point((side / ) + , CutImageArea.Location.Y);
if (CutImageArea.Y <= (side / + ))
CutImageArea.Location = new Point(CutImageArea.Location.X, (side / ) + );
if (CutImageArea.Location.X >= Width - CutImageArea.Width - (side / + ))
CutImageArea.Location = new Point(Width - CutImageArea.Width - (side / + ), CutImageArea.Location.Y);
if (CutImageArea.Location.Y >= Height - CutImageArea.Height - (side / + ))
CutImageArea.Location = new Point(CutImageArea.Location.X, Height - CutImageArea.Height - (side / + ));
}
Invalidate();
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right && ownerDraw)
{
RectEndPoint = e.Location;
CutImageArea = new System.Drawing.Rectangle(RectStartPoint, new System.Drawing.Size(e.X - RectStartPoint.X, e.Y - RectStartPoint.Y));
Invalidate();
}
}
void DoResponse(MouseEventArgs e)
{
if (!canResize)
return;
Point epointToScreen = PointToScreen(e.Location);
Point l = CutImageOldLocation; l.Offset(epointToScreen.X - this.MouseDownPointToScreen.X, epointToScreen.Y - this.MouseDownPointToScreen.Y); switch (mouseDirection)
{
case MouseDirection.N:
if (CanMove())
CutImageArea.Location = new Point(CutImageOldLocation.X, l.Y);
CutImageArea.Height = OldCutImageAreaSize.Height - (epointToScreen.Y - MouseDownPointToScreen.Y); break;
case MouseDirection.S:
CutImageArea.Height = OldCutImageAreaSize.Height + (epointToScreen.Y - MouseDownPointToScreen.Y);
break;
case MouseDirection.W:
CutImageArea.Width = OldCutImageAreaSize.Width - (epointToScreen.X - MouseDownPointToScreen.X);
if (CanMove())
CutImageArea.Location = new Point(l.X, CutImageOldLocation.Y);
break;
case MouseDirection.E:
CutImageArea.Width = OldCutImageAreaSize.Width + (epointToScreen.X - MouseDownPointToScreen.X);
break;
case MouseDirection.NE:
CutImageArea.Width = OldCutImageAreaSize.Width + (epointToScreen.X - MouseDownPointToScreen.X);
CutImageArea.Height = OldCutImageAreaSize.Height - (epointToScreen.Y - MouseDownPointToScreen.Y);
if (CanMove())
CutImageArea.Location = new Point(CutImageOldLocation.X, l.Y);
break;
case MouseDirection.SW:
CutImageArea.Width = OldCutImageAreaSize.Width - (epointToScreen.X - MouseDownPointToScreen.X);
CutImageArea.Height = OldCutImageAreaSize.Height + (epointToScreen.Y - MouseDownPointToScreen.Y);
if (CanMove())
CutImageArea.Location = new Point(l.X, CutImageOldLocation.Y);
break;
case MouseDirection.NW:
CutImageArea.Width = OldCutImageAreaSize.Width - (epointToScreen.X - MouseDownPointToScreen.X);
CutImageArea.Height = OldCutImageAreaSize.Height - (epointToScreen.Y - MouseDownPointToScreen.Y);
if (CanMove())
CutImageArea.Location = new Point(l.X, l.Y);
break;
case MouseDirection.SE:
CutImageArea.Width = OldCutImageAreaSize.Width + (epointToScreen.X - MouseDownPointToScreen.X);
CutImageArea.Height = OldCutImageAreaSize.Height + (epointToScreen.Y - MouseDownPointToScreen.Y);
CutImageArea.Location = new Point(CutImageOldLocation.X, CutImageOldLocation.Y);
break;
case MouseDirection.ALL:
break;
case MouseDirection.NONE:
break;
default:
break;
} if (CutImageArea.Width >= Width)
CutImageArea.Width = Width - side - ;
if (CutImageArea.Height >= Height)
CutImageArea.Height = Height - side - ;
if (CutImageArea.Width <= MiniWidth)
CutImageArea.Width = MiniWidth;
if (CutImageArea.Height <= MiniHeight)
CutImageArea.Height = MiniHeight;
Invalidate();
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
CutImageOldLocation = CutImageArea.Location;
CutImageAreaLocationToScreen = PointToScreen(CutImageArea.Location);
MouseDownPoint = e.Location;
MouseDownPointToScreen = PointToScreen(e.Location);
OldCutImageAreaSize = CutImageArea.Size;
if (ownerDraw && e.Button == System.Windows.Forms.MouseButtons.Right)
{
//右键自己绘制截图框
RectStartPoint = e.Location;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (showCutArea)
{
/* 0 1 2
* 7 3
* 6 5 4
*/
//左上角 0
Lites[] = new Rectangle(CutImageArea.Left - side / , CutImageArea.Top - side / , side, side);
//上中 1
Lites[] = new Rectangle(CutImageArea.Left + (CutImageArea.Width - side) / , CutImageArea.Top - side / , side, side);
//右上角 2
Lites[] = new Rectangle(CutImageArea.Width + CutImageArea.Left - side / , CutImageArea.Top - side / , side, side);
//右中 3
Lites[] = new Rectangle(CutImageArea.Width + CutImageArea.Left - side / , CutImageArea.Top + (CutImageArea.Height - side) / , side, side);
//右下角 4
Lites[] = new Rectangle(CutImageArea.Width + CutImageArea.Left - side / , CutImageArea.Top + CutImageArea.Height - side / , side, side);
//下中 5
Lites[] = new Rectangle(CutImageArea.Left + (CutImageArea.Width - side) / , CutImageArea.Top + CutImageArea.Height - side / , side, side);
//下中 6
Lites[] = new Rectangle(CutImageArea.Left - side / , CutImageArea.Top + CutImageArea.Height - side / , side, side);
//下中 7
Lites[] = new Rectangle(CutImageArea.Left - side / , CutImageArea.Top + (CutImageArea.Height - side) / , side, side);
Pen pen = new Pen(new SolidBrush(borderColor));
pen.DashStyle = dashStyle;
pe.Graphics.DrawRectangle(pen, CutImageArea);
pe.Graphics.DrawRectangles(pen, Lites); pe.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
System.Drawing.Color cor = System.Drawing.Color.FromArgb(alpha, borderColor);
System.Drawing.SolidBrush bsh = new System.Drawing.SolidBrush(cor);
pe.Graphics.FillRectangle(bsh, CutImageArea);
}
}
#endregion #region 自定义事件
public delegate void CuttedImage(Image image);
[Description("双击截图后触发的事件")]
public event CuttedImage OnCuttedImage;
#endregion
enum MouseDirection
{
N, S, W, E, NE, SW, NW, SE, ALL, NONE
}
public enum AreaAlign
{
Default, Center, RightCenter, RightTop, LeftCenter, LeftBottom, RightBottom
}
}
}

winform的扩展的带有截图功能picturebox的更多相关文章

  1. 记一次"截图"功能的项目调研过程!

    目录 项目需求 功能调研 AWT Swing Html2Image PhantomJS Headless Chrome 实现方案 结论 项目需求 最近,项目接到了一个新需求,要求对指定URL进行后端模 ...

  2. Chrome扩展开发之四——核心功能的实现思路

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  3. C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

    在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...

  4. canvas与html5实现视频截图功能

    这段时间一直在研究canvas,突发奇想想做一个可以截屏视频的功能,然后把图片拉去做表情包,哈哈哈哈哈哈~~ 制作方法: 1.在页面中加载视频 在使用canvas制作这个截图功能时,首先必须保证页面上 ...

  5. 如何编写Vault插件扩展Vault Explorer的功能

    今天练习了一下Vault Explorer的扩展程序,基本上是Vault SDK中的HelloWord示例程序.如果你刚刚开始接触Vault的二次开发,希望对你有帮助. 开始之前,你需要安装Vault ...

  6. Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能

    之前的博文 020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小) 简述了通过 JavaScript 获取浏览器显示区域大小,此文将简述另一种获取浏览器显 ...

  7. ajax中网页传输(一)TEXT——带有删除功能的数据库表格显示练习

    网页之间传输的三种方式:TEXT.JSON.XML. 本章将讲解带有TEXT形势的ajax网页传输 第一:body部分代码 <title>ajax中TEXT讲解并且带有删除功能的表格< ...

  8. mac qq截图功能失效后,如何重启截图功能?

    在finder中打开应用程序目录,找到QQ,右键单击QQ,选择显示包内容,此时会打开一个文件夹. 进入以下路径Library/LoginItems然后双击ScreenCapture这个进程,截图功能即 ...

  9. C#软件开发实例.个人定制自己的屏幕抓图工具(八)加入了截图功能键盘

    章文件夹 (一)功能概览 (二)创建项目.注冊热键.显示截图主窗体 (三)托盘图标及菜单的实现 (四)基本截图功能实现 (五)针对拖拽时闪烁卡顿现象的优化 (六)加入配置管理功能 (七)加入放大镜的功 ...

随机推荐

  1. 【BZOJ 2843】极地旅行社

    复习一下$LinkCutTree$的模板. #include<cstdio> #include<cstring> #include<algorithm> #defi ...

  2. 网络流 POJ2112

    题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用:并告诉了产奶机.奶牛之间的两两距离Dij(0<=i,j<K+C). 问题:如何安排使得在任何一头奶牛都有自己产奶机的条件下,奶牛 ...

  3. js-处理回车事件

    /**回车 */ function enterkey() { //兼容IE或其它其它浏览器 var event = arguments[0] || window.event; //兼容IE或其它浏览器 ...

  4. Html-Css标签lable中定义宽度需要其他的支持

    lable的标签如果定义了width,如果要使起生效,则需要定义display width: 130px; display: inline-block;

  5. HashMap Hashtable区别

    http://blog.csdn.net/java2000_net/archive/2008/06/05/2512510.aspx 我们先看2个类的定义 public class Hashtable ...

  6. 【USACO 1.4】Arithmetic Progressions

    /* TASK: ariprog LANG:C++ URL:http://train.usaco.org/usacoprob2?a=PA9lOcZrdWq&S=ariprog SOLVE:平方 ...

  7. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  8. CSS基础知识真难啊

    CSS层叠样式表Cascading Style Sheets CSS派生选择器(上下文选择器): 后代选择器:h1  strong {color:red;}第一个参数和第二个参数之间的代数是可以无限的 ...

  9. RabbitMQ配置文件

    配置文件Config 在Web的可视化管理界面中可以看到一些文件的路径 比如 Config文件的地址 数据库存放的文件夹 log文件的地址 进入到这个文件夹会发现有这些文件,其中example是con ...

  10. Guava集合-BiMap

    在本篇文章中我们将介绍Guava集合中的BiMap这个接口. com.google.common.collect Interface BiMap<K,V> BiMap接口的父接口是Map& ...