ImageBox Control with Zoom/Pan Capability

Introduction
This control extends the capability of an ImageBox by including scrollbars to pan the image and a method for zooming
the size of the image. My goal here is to show you two things:
- How to create your own controls that extend the
System.Windows.Formscontrols. - How to add zoom/pan capability to an image display in a simple fashion.
Creating the control
Using Microsoft Visual Studio .NET, the easiest way to create a control is to begin by right-clicking on the project and selecting "Add -> Add User Control".
The Zoom control was created by adding a GroupBox, TrackBar,
and three Labels for the minimum zoom (25%), center zoom (100%) and maximum zoom (300%). I set the Anchor property
of the GroupBox and TrackBar to
"Right, Top, Left" so that resizing the window will resize the width of these controls. To keep the 100% Label aligned
to the center of the GroupBox, I set the Anchor property
to "Top".
The Image control with automatic scroll bars was created by dropping a Panel onto
the control and sizing it to fill the remaining section of the control (below the Zoom control) and setting its Anchor property
to "Left, Top, Right, Bottom" so that it will resize with the control. Set the AutoScroll property to "true".
Finally, I dropped an ImageBox inside the panel with Location and
= 0,0SizeMode=StretchImage.
The properties must be set with AutoScroll=true and SizeMode=StretchImage in
order for the zoom and scroll bars to work properly.
Collapse | CopyCode
// zoom controls
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TrackBar scrollZoom;
private System.Windows.Forms.Label lblMax;
private System.Windows.Forms.Label lblMin;
private System.Windows.Forms.Label lblCenter;
// image controls
private System.Windows.Forms.Panel imagePanel;
private System.Windows.Forms.PictureBox imgBox;
Developing the code
At this point it becomes very simple. By placing the ImageBox inside a Panel with AutoScroll=true,
the Panel will automatically add scrollbars when the ImageBox size
exceeds the size of the Panel. So, all you have to do is to add code to get or set the image and a little bit of code
to control the zoom.
The image is set by adding a public property. In this case, I chose to make the property available at design
time by setting Browsable(true).
I also re-center the zoom scroll when a new image is loaded and disable the zoom scroll if the image is null.
Finally, I set the size of the ImageBox equal to the size of the Image for
a zoom factor of 100%.
As mentioned in the comments below by yfoulon, adding scrollZoom.Focus() should allow the use of mousewheel to zoom
the image. (I don't have a mouse so I was unable to test this.)
Collapse | CopyCode
[Browsable(true),
Description("Image loaded into the box.")]
public Image Image
{
get
{
return imgBox.Image;
}
set
{
// Set the image value
imgBox.Image = value; // enable the zoom control if this is not a null image
scrollZoom.Enabled = (value != null); if (scrollZoom.Enabled)
{
// reset zoom control
scrollZoom.Value = this.scrollZoom.Maximum/2; // Initially, the zoom factor is 100% so set the
// ImageBox size equal to the Image size.
imgBox.Size = value.Size;
}
else
{
// If null image, then reset the imgBox size
// to the size of the panel so that there are no
// scroll bars.
imgBox.Size = imagePanel.Size;
}
}
}
The zoom is handled with an EventHandler that calls a method when the user scrolls the zoom TrackBar.
The zoom factor is currently a hard-coded array with 11 elements which is the same as the number of positions on the TrackBar(min
= 0, center = 5, max = 10). The ImageBox is then resized by multiplying the Image size
by the new zoom factor. Because the ImageBox's SizeMode is
set to "StretchImage", the Image will
be scaled to fit the new size of theImageBox.
Collapse | CopyCode
private double[] zoomFactor =
{.25, .33, .50, .66, .80, 1, 1.25, 1.5, 2.0, 2.5, 3.0}; private void scrollZoom_Scroll(object sender,
System.EventArgs e)
{
setZoom();
} private void setZoom()
{
// The scrollZoom changed so reset the zoom factor
// based on the scrollZoom TrackBar position.
double newZoom = zoomFactor[scrollZoom.Value]; // Set the ImageBox width and height to the new zoom
// factor by multiplying the Image inside the Imagebox
// by the new zoom factor.
imgBox.Width =
Convert.ToInt32 ( imgBox.Image.Width * newZoom);
imgBox.Height =
Convert.ToInt32 ( imgBox.Image.Height * newZoom );
}
Additionally, I also added a KeyDown event handler and some code to allow the user to increase or decrease the zoom
factor using the Ctrl+ and Ctrl- keys.
Collapse | CopyCode
private void ImageBoxPanZoom_KeyDown(object sender, KeyEventArgs e)
{
// Was the key combination that was pressed Ctrl+ or Ctrl-?
// If so, then change the zoom level (but only if the zoom
// is enabled)
if (scrollZoom.Enabled)
{
// Note: The e.KeyData is the combination of all the
// keys currently pressed down. To find out if this is
// the Ctrl key *and* the + key, you "or" the Keys
// together. This is a bitwise "or" rather than the
// || symbol used for boolean logic. if((e.KeyData == (Keys.Oemplus | Keys.Control)) &&
(scrollZoom.Value != scrollZoom.Maximum))
{
scrollZoom.Value++;
setZoom();
}
else if ((e.KeyData == (Keys.OemMinus | Keys.Control)) &&
(scrollZoom.Value != scrollZoom.Minimum))
{
scrollZoom.Value--;
setZoom();
}
}
}
转自:http://www.codeproject.com/Articles/12331/ImageBox-Control-with-Zoom-Pan-Capability
ImageBox Control with Zoom/Pan Capability的更多相关文章
- iphone dev 入门实例6:How To Use UIScrollView to Scroll and Zoom and Page
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content Getting Starte ...
- Scroll Segmented Control(Swift)
今天用了一个github上一个比较好用的Segmented Control但是发现不是我要效果,我需要支持scrollView.当栏目数量超过一屏幕,需要能够滑动. 由于联系作者没有回复,我就自己在其 ...
- ZedGraph 柱状图、饼图、折线图演示源码
http://code1.okbase.net/codefile/ZedGraphControl.ContextMenu.cs_201211225626_97.htm // //This librar ...
- Pyhton开源框架(加强版)
info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...
- Python开源框架
info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...
- 【Leafletjs】4.L.Map 中文API
L.Map API各种类中的核心部分,用来在页面中创建地图并操纵地图. 使用 example // initialize the map on the "map" div with ...
- 一个优秀的C#开源绘图软件 DrawTools
1.Extensions to DrawTools Author Mark Miller I develop software for a leading healthcare system in N ...
- Devexpress VCL Build v2015 vol 15.1.2发布
2015年马上过半年了.终于第一个大版出来了. What's New in 15.1.2 (VCL Product Line) New Major Features in 15.1 What's ...
- zedgraph控件的一些比较有用的属性 转
(1)zedgraph控件属性具体解释: AxisChange()() ->> This performs an axis change command on the graphPane. ...
随机推荐
- HDU 多校1.11
- 10、Django实战第10天:找回密码
今天完成的功能是:用户忘记密码后,通过注册邮箱重置密码... 首先还是把前端页面准备好,把forgetpwd.html复制到templates目录下 编辑users.views.py,创建一个忘记密码 ...
- View Controller Basics学习记录
1.UIView,UIScreen ,UIWindow的区别? UIScreen是关于设备的尺寸大小.UIWindow是在UIScreen上作画的区域.UIView是在uiwindow上draw视图. ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [JOISC2014]JOIOJI
题目大意: 给你一串仅包含'J''O''I'的字符串,问满足三种字符出现次数相等的最大字串是多少? 思路: 用map存一下出现次数前缀和两两之差出现的最早位置,每次看一下当前的两两之差最早的出现位置是 ...
- Bootstrap-table使用footerFormatter做统计列
写在前面: 在做表格的时候,难免会碰到做统计的时候.由于在项目中涉及到做统计的功能比较简单,之后也就没有过多的去研究更复杂的,这里简单记录下. 这次就直接先上图:一个简单的例子 看到效果图还是很好的, ...
- java static代码段
1)java中还有个static代码块的形式,形式为 static {……}.static代码块是类定义的一部分,仅仅在类被初次加载的时候被调用一次,之后再调用不会再加载.那么类什么时候首次被加载呢? ...
- Word中插入带公式的Visio注意事项
有时候发现,有的公式显示的间距特别大,那么在word中右键打开Visio,改好后,保存了,word里还是那样. 因为你需要吧改好的另存为原来的visio文件(名字.位置要一样,就是说替换原来的文件), ...
- hibernate CascadeType属性
CascadeType.PERSIST 只有A类新增时,会级联B对象新增.若B对象在数据库存(跟新)在则抛异常(让B变为持久态) : 级联保存,当调用了Persist() 方法,会级联保存相应的数据 ...
- Array.apply 方法的使用
Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...