Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 994395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:34:09+00:00 2026-05-16T06:34:09+00:00

I’m trying to implement a simple zoom to point functionality in WinForms. When the

  • 0

I’m trying to implement a simple “zoom to point” functionality in WinForms. When the mouse is at the same point and the mouse wheel is scrolled in/out, it works great. When the mouse position is changed between scrolls, it jumps to that position and is very wonky. Here’s the code for the control that you can add to a Form for testing:

public class Canvas : Control
{

    private Bitmap Image;
    private TextureBrush ImageBrush;
    private Point Origin;
    private Matrix TransformMatrix;

    public float ZoomScale
    {
        get;
        set;
    }

    public Canvas()
    {
        this.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);

        this.Image = // Load your picture here.
        this.ImageBrush = new TextureBrush(this.Image, WrapMode.Clamp);
        this.ZoomScale = 1.0f;

        this.TransformMatrix = new Matrix();

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        float zs = this.ZoomScale;

        var matrix = this.TransformMatrix.Clone();
        e.Graphics.Transform = matrix;

        var c = e.ClipRectangle;

        // Transform the clip rectangle, is this even right?
        var x = (int)Math.Round(c.X / zs - matrix.OffsetX / zs);
        var y = (int)Math.Round(c.Y / zs - matrix.OffsetY / zs);
        var w = (int)Math.Round(c.Width / zs);
        var h = (int)Math.Round(c.Height / zs);

        // Draw the image scaled and translated.
        e.Graphics.FillRectangle(this.ImageBrush, x, y, w - 1, h - 1);

    }

    protected override void OnMouseWheel(MouseEventArgs e)
    {
        if (e.Delta > 0)
        {
            this.ZoomScale += 0.1f;
        }
        else
        {
            this.ZoomScale -= 0.1f;
        }

        this.ZoomToPoint(e.Location);
        this.Refresh();
    }

    private void ZoomToPoint(Point origin)
    {
        this.Origin = origin;
        float zoomScale = this.ZoomScale;

        // The important part.
        var matrix = new Matrix(1, 0, 0, 1, 0, 0);
        matrix.Reset();
        matrix.Translate(-origin.X, -origin.Y, MatrixOrder.Append);
        matrix.Scale(zoomScale, zoomScale, MatrixOrder.Append);
        matrix.Translate(origin.X, origin.Y, MatrixOrder.Append);
        this.TransformMatrix = matrix;
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        this.ZoomToPoint(this.Origin);
    }

}

So the important part is obviously the affine transformation. I know it’s in Adobe Flex, but I got the idea of Translate, Scale, Translate from this website (great example of what I’m trying to do). I didn’t realize that MatrixOrder.Append was the key in .NET until I tried. But the problem still remains. Try loading the control in a WinForms application and you’ll see what I mean. Does anyone know what the issue is with this?

EDIT: The reason I need to manually calculate the rectangle is because I’m going to be drawing more than just an image. It’s a canvas that performs like Visio. I’ll also need a method for transforming a specific Point.

EDIT 2: I understand that I can use Invert() on the Matrix then TransformPoints() to get a proper transformed point but it still doesn’t solve the problem of the wonky mouse movement. I was thinking of calculating the Origin from the location of the top corner of the image but it didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:34:10+00:00Added an answer on May 16, 2026 at 6:34 am

    Ah-ha! I found it! After analyzing the link that I posted, I realized “Hey, he’s not setting a “zoom-scale” anywhere. Then I found the problem. I was creating a new Matrix every time. This isn’t the right way to do it. It should scale/translate the current transformation:

    private void ZoomToPoint(float scale, Point origin)
    {
        var matrix = this.TransformMatrix;
        matrix.Translate(-origin.X, -origin.Y, MatrixOrder.Append);
        matrix.Scale(scale, scale, MatrixOrder.Append);
        matrix.Translate(origin.X, origin.Y, MatrixOrder.Append);
    
        this.TransformMatrix = matrix;
    }
    

    And the call to that would be like:

    // Zoom in
    this.ZoomToPoint(6 / 5.0f, e.Location);
    

    and

    // Zoom out
    this.ZoomToPoint(5 / 6.0f, e.Location);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.