(转载)winform图片标尺控件

最近要做个winform的东西,要在里面集成一个类似Windows自带画图的标尺功能,还要能在图片上画矩形框。在网上找了好久也没找到写好的控件,无奈自己做了一个。

目前还有些bug,这里先做个分享。(Ps:很少做winform的东西,做的不好,轻喷。另外欢迎指点。)

(转载)winform图片标尺控件

由于最后要做的东西的背景是黑的,标尺就画成白的了,有需要的自己改吧。。

直接上代码

using Helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms; namespace UserCtrl
{
public partial class RulerControl : UserControl
{
private double _monitorDPI = ;
/// <summary>
/// 用于控制放大倍数
/// </summary>
private double _multiple = ; /// <summary>
/// 每多少个像素1格
/// </summary>
public double MonitorDPI
{
get
{
return _monitorDPI;
}
set
{
_monitorDPI = value;
}
}
/// <summary>
/// X轴偏移位置
/// </summary>
private float offSetX=; /// <summary>
/// Y轴偏移位置
/// </summary>
private float offSetY = ; /// <summary>
/// 开始位置X
/// </summary>
public double XStart
{
get;
set;
} /// <summary>
/// 开始位置Y
/// </summary>
public double YStart
{
get;
set;
} /// <summary>
/// 用户设置的原始图
/// </summary>
private Image _initImg = null; /// <summary>
/// 图片
/// </summary>
public Image Image
{
get
{
return Pic.Image;
}
set
{
Pic.Image = value;
if (_initImg == null&&value!=null)
{
_initImg = PicHelper.GetNewPic(value, value.Width , value.Height );
}
}
}
private Font font = new Font("宋体", ); //刻度值显示字体
public RulerControl()
{
InitializeComponent();
} private void Pic_MouseWheel(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
return;
}
Image img = Pic.Image;
if (img != null)
{
if (e.Delta >= )
{
if (_multiple * > )
return;
_multiple *= ;
}
else
{
if (Pic.Width < this.Width - )
return; _multiple *= 0.5;
if (Pic.Width <= (this.Width - ))
{
XStart = ;
YStart = ;
}
}
DrawRect();
AdapterDpi();
ReDrawX();
ReDrawY();
}
} private void RulerControl_Paint(object sender, PaintEventArgs e)
{
P1.Height = ;
P1.Width = this.Width - ;
P2.Height = this.Height - ;
P2.Width = ;
P1.Location = new Point(, );
P2.Location = new Point(, );
PContainer.Location = new Point(, );
} private void RulerControl_Resize(object sender, EventArgs e)
{
P1.Height = ;
P1.Width = this.Width - ;
P2.Height = this.Height - ;
P2.Width = ;
P1.Location = new Point(, );
P2.Location = new Point(, );
PContainer.Location = new Point(, );
} /// <summary>
/// 重画Y轴
/// </summary>
private void ReDrawY()
{
if (P2.BackgroundImage != null)
{
P2.BackgroundImage.Dispose();
}
Bitmap bmpY = new Bitmap(P2.Width, P2.Height);
using (Graphics g = Graphics.FromImage(bmpY))
{
int originLocation = bmpY.Width - ;
int startY = (int)Math.Ceiling(YStart);
offSetY = (float)(MonitorDPI * _multiple * (startY - YStart));
for (int i = startY; i <= Math.Ceiling(P2.Height / (MonitorDPI * _multiple) + YStart); i++)
{
float y = (float)(MonitorDPI * _multiple * (i - YStart)) + offSetY;
if (y >= )
{
PointF start = new PointF(originLocation, y);
PointF end = new PointF(originLocation - , y);
if (i % == )
{
end = new PointF(originLocation - , y);
}
if (i % == && i != )
{
end = new PointF(originLocation - , y);
g.DrawString((i*MonitorDPI).ToString(), font, Brushes.White, new PointF(originLocation - , y - ));
}
g.DrawLine(Pens.White, start, end);
}
}
g.DrawLine(Pens.White, new PointF(originLocation, ), new PointF(originLocation, bmpY.Height));
P2.BackgroundImage = bmpY;
}
} /// <summary>
/// 重画X轴
/// </summary>
private void ReDrawX()
{
if (P1.BackgroundImage != null)
{
P1.BackgroundImage.Dispose();
}
Bitmap bmpX = new Bitmap(P1.Width, P1.Height);
using (Graphics g = Graphics.FromImage(bmpX))
{
int originLocation = bmpX.Height - ;
int startX = (int)Math.Ceiling(XStart);
offSetX = (float)(MonitorDPI * _multiple * (startX - XStart));
for (int i = startX; i <= Math.Ceiling(P1.Width / (MonitorDPI * _multiple) + XStart); i++)
{
float x = (float)(MonitorDPI * _multiple * (i - XStart)) + offSetX;
if (x >= )
{
PointF start = new PointF(x, originLocation);
PointF end = new PointF(x, originLocation - );
if (i % == )
{
end = new PointF(x, originLocation - );
}
if (i % == && i != )
{
end = new PointF(x, originLocation - );
g.DrawString((i*MonitorDPI).ToString(), font, Brushes.White, new PointF(x - , originLocation - ));
}
g.DrawLine(Pens.White, start, end);
}
}
g.DrawLine(Pens.White, new PointF(, originLocation), new PointF(bmpX.Width, originLocation));
P1.BackgroundImage = bmpX;
}
} private void RulerControl_Load(object sender, EventArgs e)
{
P1.Height = ;
P1.Width = this.Width - ;
P2.Height = this.Height - ;
P2.Width = ;
P1.Location = new Point(, );
P2.Location = new Point(, );
PContainer.Location = new Point(, );
ReDrawX();
ReDrawY();
Pic.MouseWheel += new MouseEventHandler(Pic_MouseWheel);
} private void Pic_MouseEnter(object sender, EventArgs e)
{
Pic.Focus();
} private void PContainer_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
XStart = e.NewValue / (MonitorDPI * _multiple);
ReDrawX();
}
else if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
YStart = e.NewValue / (MonitorDPI * _multiple);
ReDrawY();
}
} #region 画图片选定区域
bool MouseIsDown = false;
Rectangle MouseRect = Rectangle.Empty; private void Pic_MouseDown(object sender, MouseEventArgs e)
{
MouseIsDown = true;
MouseRect = new Rectangle(e.X, e.Y, , );
Rectangle rec = new Rectangle(, ,
Math.Min(PContainer.ClientRectangle.Width, Pic.ClientRectangle.Width),
Math.Min(PContainer.ClientRectangle.Height, Pic.ClientRectangle.Height));
Cursor.Clip = PContainer.RectangleToScreen(rec);
} private void Pic_MouseMove(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
MouseRect.Width = e.X - MouseRect.Left;
MouseRect.Height = e.Y - MouseRect.Top;
if (MouseRect.Width > && MouseRect.Height > )
{
Rectangle rect = Pic.RectangleToScreen(MouseRect);
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
}
Pic.Refresh();
} } private void Pic_MouseUp(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
MouseRect.Width = e.X - MouseRect.Left;
MouseRect.Height = e.Y - MouseRect.Top;
using (Graphics g = Graphics.FromImage(Pic.Image))
{
g.DrawRectangle(new Pen(Color.Red), MouseRect);
}
Pic.Refresh();
MouseIsDown = false;
if ((int)(MouseRect.Width * _multiple) > && (int)(MouseRect.Height * _multiple) > )
{
list.Add(new Rectangle((int)(MouseRect.X / _multiple),
(int)(MouseRect.Y / _multiple),
(int)(MouseRect.Width / _multiple),
(int)(MouseRect.Height / _multiple)
));
}
MouseRect = Rectangle.Empty;
Cursor.Clip = Rectangle.Empty;
}
}
#endregion
public List<Rectangle> list = new List<Rectangle>(); private void Pic_DoubleClick(object sender, EventArgs e)
{
MouseEventArgs ev = e as MouseEventArgs;
List<Rectangle> temp = new List<Rectangle>();
foreach (Rectangle item in list)
{
if (ev.X > item.X * _multiple && ev.X < item.X * _multiple + item.Width * _multiple &&
ev.Y > item.Y * _multiple && ev.Y < item.Y * _multiple + item.Height * _multiple)
{
temp.Add(item);
}
}
foreach (Rectangle item in temp)
{
list.Remove(item);
} DrawRect();
} /// <summary>
/// 把list中的框画到图片中
/// </summary>
private void DrawRect()
{
if (Pic.Image != _initImg)
Pic.Image.Dispose();
Pic.Image = PicHelper.GetNewPic(_initImg, (int)(_initImg.Width * _multiple), (int)(_initImg.Height * _multiple));
using (Graphics g = Graphics.FromImage(Image))
{
foreach (Rectangle item in list)
{
g.DrawRectangle(new Pen(Color.Red), new Rectangle((int)(item.X * _multiple),
(int)(item.Y * _multiple),
(int)(item.Width * _multiple),
(int)(item.Height * _multiple)
));
}
}
Pic.Refresh();
} private void AdapterDpi()
{
if (MonitorDPI * _multiple < )
{
MonitorDPI *= ;
}
if (MonitorDPI * _multiple > )
{
MonitorDPI /= ;
}
} }
}

下载链接:http://download.csdn.net/download/p690075426/10142184

---------------------
作者:ydp1991
来源:博客园
原文:https://www.cnblogs.com/ydp1991/p/7955382.html

上一篇:Winform 中panel的mousewheel鼠标滚轮事件触发


下一篇:2016.02.14 总结JS事件