C#: Why does this not zoom using the mouse as center?

I'm trying to make a canvas where you can zoom and pan. The panning works, but zooming at the mouse position won't work.

EDIT: This almost works. The center for zoom is slightly offset however...

This is the code for the zooming:

public partial class Form1 : Form { PointF mouseDown; float newX; float newY; float zoomFactor = 1.0F; public Form1() { InitializeComponent(); mouseDown = new PointF(0F, 0F); this.panel1.Paint += new PaintEventHandler(panel1_Paint); this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown); this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove); } private void panel1_Paint(object sender, PaintEventArgs e) { base.OnPaint(e); Graphics dc = e.Graphics; dc.SmoothingMode = SmoothingMode.AntiAlias; dc.TranslateTransform(newX, newY); dc.ScaleTransform(zoomFactor, zoomFactor); Color lineColor = Color.FromArgb(200, 200, 200); Pen linePen = new Pen(lineColor,1*zoomFactor); dc.DrawLine(linePen, 100, 100, 200, 200); textBox1.Text = newX.ToString(); textBox2.Text = newY.ToString(); } private void panel1_MouseDown(object sender, EventArgs e) { MouseEventArgs mouse = e as MouseEventArgs; if (mouse.Button == MouseButtons.Right) { mouseDown = mouse.Location; mouseDown.X = mouseDown.X - newX; mouseDown.Y = mouseDown.Y - newY; } } private void panel1_MouseMove(object sender, EventArgs e) { MouseEventArgs mouse = e as MouseEventArgs; if (mouse.Button == MouseButtons.Right) { Point mousePosNow = mouse.Location; float deltaX = mousePosNow.X - mouseDown.X; float deltaY = mousePosNow.Y - mouseDown.Y; newX = deltaX; newY = deltaY; panel1.Invalidate(); } } protected override void OnMouseWheel(MouseEventArgs e) { MouseEventArgs mouse = e as MouseEventArgs; if (e.Delta > 0) { if (zoomFactor + 0.2 < 10) { zoomFactor += 0.2F; } } else if (e.Delta < 0) { if (zoomFactor - 0.2 > 0.2) { zoomFactor -= 0.2F; } } float x = (mouse.Location.X - newX) * zoomFactor; float y = (mouse.Location.Y - newY) * zoomFactor; newX = mouse.Location.X - x; newY = mouse.Location.Y - y; panel1.Invalidate(); } }

-------------Problems Reply------------

I finally figured it out, so here's the solution.

public partial class Form1 : Form
{

PointF mouseDown;

float newX;
float newY;
float zoomFactor = 1.0F;

public Form1()
{
InitializeComponent();

mouseDown = new PointF(0F, 0F);

this.panel1.Paint += new PaintEventHandler(panel1_Paint);
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

}

private void panel1_Paint(object sender, PaintEventArgs e)
{

base.OnPaint(e);

Graphics dc = e.Graphics;

dc.SmoothingMode = SmoothingMode.AntiAlias;

dc.TranslateTransform(newX, newY);

dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);

Color lineColor = Color.FromArgb(200, 200, 200);
Pen linePen = new Pen(lineColor,1*zoomFactor);

dc.DrawLine(linePen, 100, 100, 200, 100);
dc.DrawLine(linePen, 200, 100, 200, 200);
dc.DrawLine(linePen, 200, 200, 100, 200);
dc.DrawLine(linePen, 100, 200, 100, 100);
dc.DrawLine(linePen, 100, 100, 200, 200);
dc.DrawLine(linePen, 200, 100, 100, 200);

}

private void panel1_MouseDown(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;

if (mouse.Button == MouseButtons.Right)
{
mouseDown = mouse.Location;

mouseDown.X = mouseDown.X - newX;
mouseDown.Y = mouseDown.Y - newY;

}

}

private void panel1_MouseMove(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;

if (mouse.Button == MouseButtons.Right)
{
PointF mousePosNow = mouse.Location;

float deltaX = mousePosNow.X - mouseDown.X;
float deltaY = mousePosNow.Y - mouseDown.Y;

newX = deltaX;
newY = deltaY;

panel1.Invalidate();

}

}

protected override void OnMouseWheel(MouseEventArgs e)
{

MouseEventArgs mouse = e as MouseEventArgs;

PointF mP = mouse.Location;

if (e.Delta > 0)
{
if (zoomFactor >= 1 && zoomFactor <= 10)
{
zoomFactor += 1F;

newX = newX - ((mP.X - newX) / (zoomFactor - 1));
newY = newY - ((mP.Y - newY) / (zoomFactor - 1));
}
else if (zoomFactor == 0.5)
{
zoomFactor = zoomFactor * 2;
newX = newX - ((mP.X - newX) );
newY = newY - ((mP.Y - newY) );
}
else if (zoomFactor < 0.5)
{
zoomFactor = zoomFactor * 2;
newX = newX - ((mP.X - newX) );
newY = newY - ((mP.Y - newY) );
}
}

else if (e.Delta < 0)
{
if (zoomFactor >2)
{
zoomFactor -= 1F;
newX = newX + (((mP.X - newX)) / (zoomFactor+1 ));
newY = newY + (((mP.Y - newY)) / (zoomFactor+1));
}
else if (zoomFactor == 2) {
zoomFactor -= 1F;

newX = newX + ((mP.X - newX)/2);
newY = newY + ((mP.Y - newY)/2);
}else if(zoomFactor <= 1 && zoomFactor > 0.2)
{
zoomFactor = zoomFactor / 2;

newX = newX + ((mP.X - newX) / 2);
newY = newY + ((mP.Y - newY) / 2);

}

}

panel1.Invalidate();

}
}

Maybe your MousePosition is different from PanelPosition? I mean on is relative to the control and one relative to the screen?

Control.PointToScreen is your friend!

Category:c# Views:2 Time:2010-11-25
Tags: c# mouse paint zoom

Related post

  • Zooming into the mouse position with a translation? 2010-07-01

    To zoom into the mouse position I was using: glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0); glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor); glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0); so basically I

  • how to zoom in an image and center it 2010-01-08

    Any Idea how to zoom in a image on particular point using javascript css ? I am using webkit based browser. I can zoom by specifying zoom property , like elem.style.zoom="150%", Main problem is I cannot centre the image where I want to zoom. I can ge

  • Zooming into the mouse, factoring in a camera translation? (OpenGL) 2010-07-20

    Here is my issue, I have a scale point, which is the unprojected mouse position. I also have a "camera which basically translates all objects by X and Y. What I want to do is achieve zooming into mouse position. I'v tried this: 1. Find the mouse's x

  • Zooming in on mouse position in OpenGl 2010-11-02

    I wish to implement a zoom-to-mouse-position-with-scrollwheel-algorithm for a CAD application I work on. Similar question have been asked (This one for example), but all the solutions i found used the Translate to origin Scale to zoom Translate back

  • How to retrieve the zoomed location on mouse click, in ArcGIS? 2011-01-26

    I am using ArcGIS map to display the location and allow the user to zoom and drag to their destination. When they click the Save button, I want to save the co-ordinates and in my postback I want to show them the same zoomed location. I am able to sav

  • JavaScript code to zoom maps using mouse scroll 2011-02-10

    Please help me to write JavaScript code to zoom-in, zoom-out Google maps using mouse scroll. --------------Solutions------------- Your question is incomplete. I'm guessing you're referring to Google Maps. If this is the case you need to call enableSc

  • Zoom Canvas to Mouse Cursor 2011-03-04

    I'm programming a HTML5 < canvas > project that involves zooming in and out of images using the scroll wheel. I want to zoom towards the cursor like google maps does but I'm completely lost on how to calculate the movements. What I have: image

  • as3 I have coding for zoom but i want zoom to follow mouse 2011-03-14

    Here is coding I found that creates a box and zooms in on point of mouseclick. what I need is for the zoom to then follow the mouse. Deperately need help plz. Thanks import fl.transitions.Tween; import fl.transitions.TweenEvent; import fl.transitions

  • Programmatically zooming a UIWebView without gestures - Center Point not calculated correctly 2011-06-27

    The Problem I have a UIWebView inside my iPad application which I need to zoom programmatically, but without the use of gestures/taps. The app. has a "+" and a "-" button for zooming in-and-out in user-defined increments (it is an app. for the visual

  • How do I disable, automatic Pinch and Zoom without a mouse Device Tab 2012-09-19

    I am having a problem with an over sensitive Pinch and Zoom, on my Aspire|V laptop, (Windows 8.1), the screen regularly grows to the size of an elephant or reduces to a postage stamp of it's own accord, I am currently using a touch pad to navigate, b

  • How to disable zoom when Ctrl + Mouse roll in Excel 2013? 2012-12-18

    Excel 2013 and some earlier versions have an option: "Advanced | Zoom on roll with IntelliMouse". I've unchecked that option because I don't want such zooming. However, I often have the Control key depressed as I select rows to delete, a process that

  • How do i disable zoom feature on mouse 2013-02-26

    How do i disable this feature. Mouse is zooming in & out when I a just moving the curser around. I don't even click ctrl+. Help! --------------Solutions------------- Hi, 1. Which version of Windows operating system is installed on your computer?

  • Placing Zoom Controls in a MapView 2008-11-04

    I'm trying to get the zoom controls to show up in a mapview, the following code almost works, but the zoom controls appear in the top left of the mapview, not the bottom center like I'm specifiying via setGravity(). Can someone enlighten me as to wha

  • Getting lat/lng/zoom/yaw/pitch from Google Street View 2009-04-08

    I have a Google map and street view based on provided lat/lng coordinates, zoom, yaw and pitch. I need to invoke a javascript to update a hidden field for each of these values whenever any of the details change from their default or when a button is

  • Eclipse IDE: How to zoom in on text? 2009-04-26

    I want the same behaviour Firefox has, when you use the scroll button in the mouse to zoom in and out on the current view. Is there something like it for eclipse? --------------Solutions------------- There is a project from a guy called 'tarlog' that

  • UIScrollView odd behavior: zoom in, pan, zoom out leaves content view uncentered 2010-02-05

    I have a custom UIViewController subclass controlling a UISCrollView containing an UIImageView, set up like so: UIViewController + UIScrollView (delegate: UIViewController, viewForZoomingInView: UIImageView) + UIImageView I can use the UIScrollView t

  • Implementing zoom on a fixed point, javascript/canvas 2010-05-23

    Possible Duplicate: Zoom in on a point (using scale and translate) I want to implement zooming on the mouse pointer with the mouse wheel. That is scaling the image while the point under the mouse pointer stays fixed. Here is my code, which doesn't wo

  • Zoom in on a point (using scale and translate) 2010-05-26

    I want to be able to zoom in on the point under the mouse in canvas with the mouse whell, like when zooming on maps.google. I'd like straight code as I've been working on this for 5h+ 15h Something to start with: After 2 weeks break I did it in 3 min

  • Auto-detect a screen resolution and change browser zoom with Javascript? 2010-07-06

    How do I auto-detect a screen resolution and change browser zoom with Javascript? I was thinking of something more like this: I've got the following code: #warp with width: 3300% and a mask with width: 100%; and then, each .item has width: 3.030303%

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.246 (s). 11 q(s)