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 3451482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:08:58+00:00 2026-05-18T09:08:58+00:00

I’m trying to create a canvas for drawing different objects. I’ve created zoom and

  • 0

I’m trying to create a canvas for drawing different objects. I’ve created zoom and pan functions using graphics.scaleTransform and graphics.translateTransform, but i wan’t the canvas background (a grid) to allways fill out the entire window, however it does not, using the following code:

EDIT: I’ve tried using the coordinates in the transformed graphics object, but it seems it won’t accept negative numbers?!?

EDIT: This picture explains my problem:

alt text

 public partial class Form1 : Form
        {

            PointF mouseDown;

            float newX;
            float newY;
            float zoomFactor = 1F;

            Region _rgn;
            Graphics _dc;
            PointF zoomPoint = new PointF(150, 150);

            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 bg = 

                Graphics dc = e.Graphics;
                _dc = dc;

                dc.SmoothingMode = SmoothingMode.AntiAlias;

Color gridColor = Color.FromArgb(230, 230, 230);
            Pen gridPen = new Pen(gridColor, 1);

            for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
            {
                dc.DrawLine(gridPen, 0, i, this.Width * (zoomFactor), i);
            }
            for (float i = 0; i < this.Width * (zoomFactor); i = i + 30*zoomFactor)
            {
                dc.DrawLine(gridPen, i, 0, i, this.Height * (zoomFactor));
            }

                dc.TranslateTransform(newX, newY);
                dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);



                float XPosition = 10;
                float YPosition = 10;
                float CornerRadius = 5;
                float Width = 50;
                float Height = 50;

                Color BoxColor = Color.FromArgb(0, 0, 0);
                Pen BoxPen = new Pen(BoxColor, 2);

                GraphicsPath Path = new GraphicsPath();

                Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition);
                Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90);
                Path.AddLine(XPosition + Width, YPosition + CornerRadius, XPosition + Width, YPosition + Height - (CornerRadius * 2));
                Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
                Path.AddLine(XPosition + Width - (CornerRadius * 2), YPosition + Height, XPosition + CornerRadius, YPosition + Height);
                Path.AddArc(XPosition, YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
                Path.AddLine(XPosition, YPosition + Height - (CornerRadius * 2), XPosition, YPosition + CornerRadius);
                Path.AddArc(XPosition, YPosition, CornerRadius * 2, CornerRadius * 2, 180, 90);

                Path.CloseFigure();



                LinearGradientBrush lgb = new LinearGradientBrush(new PointF(XPosition+(Width/2),YPosition), new PointF(XPosition+(Width/2),YPosition + Height), Color.RosyBrown, Color.Red);

                dc.FillPath(lgb, Path);


                dc.DrawPath(BoxPen, Path);

                Matrix transformMatrix = new Matrix();
                transformMatrix.Translate(newX, newY);
                transformMatrix.Scale(zoomFactor, zoomFactor);

                _rgn = new Region(Path);

                _rgn.Transform(transformMatrix);

            }

            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;

                }

                else if (mouse.Button == MouseButtons.Left)
                {

                    if (_rgn.IsVisible(mouse.Location, _dc))
                    {
                        MessageBox.Show("tada");
                    }


                }

            }

            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 = 2 * newX - mP.X ;
                        newY = 2 * newY - mP.Y ;
                    }
                    else if (zoomFactor < 0.5)
                    {
                        zoomFactor = zoomFactor * 2;
                        newX = 2 * newX - mP.X;
                        newY = 2 * newY - mP.Y;
                    }
                }

                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();

            }
        }
  • 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-18T09:08:58+00:00Added an answer on May 18, 2026 at 9:08 am

    Draw your GRID BEFORE transforming the coordinate system.

    If you can’t, use GetTransform() and ResetTransform(), then draw grid, then SetTransform back (the one got in the first step).

    IMHO: It would be much better if you scale you image ‘manually’ instead of using coordinate transformation. You’ll have much greater control this way, and won’t hit the wall with anything.

    EDIT:

    Also: you have a bug in your grid drawing routine:

    for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
    

    try replacing with

    for (float i = 0; i < this.Height; i = i + 30*zoomFactor)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.