如何放大在PictureBox的一个点 [英] How to zoom at a point in picturebox
问题描述
这是我的代码。结果
我能缩放图片框而不是在一个点上。
如何缩放到鼠标指向当我转动鼠标滚轮?
的变量是: -
私人双m_dZoomscale = 1.0; //这是ZOOM规模,其中的每个对象
//被放大在画布
公共静态双s_dScrollValue = 0.01;
//比例因子鼠标滚动缩放
值
油漆代码: -
私人无效m_Picturebox_Canvas_Paint(对象发件人,PaintEventArgs的E)
{
图形G = e.Graphics;
g.ScaleTransform((浮点)m_dZoomscale,(浮点)m_dZoomscale);
}
的主要代码: -
保护覆盖无效OnMouseWheel(MouseEventArgs MEA)
{
m_Picturebox_Canvas.Focus();
如果(m_Picturebox_Canvas.Focused ==真)
{
如果(mea.Delta大于0)
{
ZoomInScroll();
}
,否则如果(mea.Delta℃,)
{
ZoomOutScroll();
}
}
}
子功能如下 -
//函数鼠标SCROL ZOOM-IN
私人无效ZoomInScroll()
$ { b $ b m_dZoomscale = m_dZoomscale + s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
//函数鼠标SCROL ZOOM-IN
私人无效ZoomOutScroll()
{
m_dZoomscale = m_dZoomscale - s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
下面是你如何做到这一点(说明下面的代码):
变量
//这个跟踪应用到图片框的Graphics
私人矩阵变换变换=新的Matrix();
公共静态双s_dScrollValue = 0.01; //什么新...
油漆代码
私人无效m_Picturebox_Canvas_Paint(对象发件人,PaintEventArgs的E)
{
图形G = e.Graphics;
g.Transform =变换;
}
滚动-事件代码
保护覆盖无效OnMouseWheel(MouseEventArgs MEA)
{
m_Picturebox_Canvas.Focus();
如果(m_Picturebox_Canvas.Focused ==真放大器;&放大器;!mea.Delta = 0)
{
ZoomScroll(mea.Location,mea.Delta大于0);
}
}
变焦功能
//函数鼠标SCROL ZOOM-IN
私人无效ZoomScroll(点位置,布尔zoomIn)
{
//使变焦点(光标位置)我出身
transform.Translate(-location.X,-location.Y);
//进行变焦(在原点)
如果(zoomIn)
transform.Scale(s_dScrollValue,s_dScrollValue);
,否则
transform.Scale(-s_dScrollValue,-s_dScrollValue);
//翻译出身回光标
transform.Translate(location.X,location.Y);
m_Picturebox_Canvas.Invalidate();
}
说明
所有的
首先,你可以看到我结合你的两个缩放方法一种方法: ZoomScroll
不然的话,我们就复制了很多逻辑...
那么,什么是这里做什么?我想这是明确的,我们需要一个翻译也适用于图形
对象。我们积累在矩阵
字段应用于图片框
所有转换。
您成功放大你的形象,但总是与原产地(的左上角的图片框
)作为缩放操作的locical中心 - 即是多么缩放
/ ScaleTransform
作品!因此,为了在不同以刻度点需要以下步骤:
- 翻译世界,让您点希望以大规模的新的原点(例如,您的光标位于
12 | 34
,所以我们通过翻译-12 | -34
) - 现在,所期望的点是我们的新原点,比例
- 翻译世界回来了,所以原来的点你的光标下再次结束了
This is my code.
I am able to zoom the picturebox but not at a point.
How to zoom to a mouse point when i rotate the mouse wheel?
The variables are:-
private double m_dZoomscale = 1.0; //THIS IS THE ZOOM SCALE TO WHICH EACH OBJECT
//ARE ZOOMED IN THE CANVAS
public static double s_dScrollValue = .01;
//scale factor value for mouse scroll zooming
The paint code is:-
private void m_Picturebox_Canvas_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.ScaleTransform((float)m_dZoomscale, (float)m_dZoomscale);
}
The main code is:-
protected override void OnMouseWheel(MouseEventArgs mea)
{
m_Picturebox_Canvas.Focus();
if (m_Picturebox_Canvas.Focused == true)
{
if (mea.Delta > 0)
{
ZoomInScroll();
}
else if (mea.Delta < 0)
{
ZoomOutScroll();
}
}
}
The sub functions are as follows:-
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomInScroll()
{
m_dZoomscale = m_dZoomscale + s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomOutScroll()
{
m_dZoomscale = m_dZoomscale - s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
Here is how you achieve this (description below code):
Variables
// this tracks the transformation applied to the PictureBox's Graphics
private Matrix transform = new Matrix();
public static double s_dScrollValue = .01; // nothing new...
Paint code
private void m_Picturebox_Canvas_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Transform = transform;
}
Scroll-Event code
protected override void OnMouseWheel(MouseEventArgs mea)
{
m_Picturebox_Canvas.Focus();
if (m_Picturebox_Canvas.Focused == true && mea.Delta != 0)
{
ZoomScroll(mea.Location, mea.Delta > 0);
}
}
Zoom function
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomScroll(Point location, bool zoomIn)
{
// make zoom-point (cursor location) our origin
transform.Translate(-location.X, -location.Y);
// perform zoom (at origin)
if(zoomIn)
transform.Scale(s_dScrollValue, s_dScrollValue);
else
transform.Scale(-s_dScrollValue, -s_dScrollValue);
// translate origin back to cursor
transform.Translate(location.X, location.Y);
m_Picturebox_Canvas.Invalidate();
}
Description
First of all, as you can see I combined your two zoom methods to one method: ZoomScroll
Otherwise we would duplicate a lot of logic...
So what is done here? I guess it is clear that we need to also apply a translation to the Graphics
object. We "accumulate" all transformation applied to the PictureBox
in a Matrix
field.
You successfully scaled your image, but always with the origin (upper-left corner of the PictureBox
) as the locical center of your scaling-operation - that is just how Scale
/ScaleTransform
works! So in order to scale at a different point the following steps are needed:
- translate the world, so that the point you want to scale at is the new origin (e.g. your cursor is at
12|34
, so we translate by-12|-34
) - now that the desired spot is our new origin, scale
- translate the world back, so the original point ends up under your cursor again
这篇关于如何放大在PictureBox的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!