Увеличьте масштаб, чтобы указать матрицу C # - PullRequest
0 голосов
/ 25 июня 2019

Я хотел бы сделать простое масштабирование, используя матрицы.Я пробовал несколько способов, таких как масштабирование матрицы с помощью коэффициента масштабирования, но он только увеличивает верхний левый угол (0,0).
Как мне добиться увеличения в любой заданной позиции мышью?

Это код, который у меня есть.

 private void DrawToBitmap()
    {
        Brush b = new SolidBrush(Color.White);
        graphics.Clear(pic.BackColor);
        graphics.SmoothingMode = SmoothingMode.HighQuality;

        float width = maxX - minX; //8000 units wide
        float height = maxY - minY;//8000 units high

        transform = new Matrix();
        transform.Scale(bmp.Width / width, bmp.Height / height);
        transform.Translate(-minX, -minY, MatrixOrder.Prepend);
        graphics.Transform = transform; 

//Works fine until now

    }


// using this method to draw the image of the bmp to the picturebox

 private void Pic_Paint(object sender, PaintEventArgs e)
    {
        DrawToBitmap();
        Graphics g = e.Graphics;
        g.DrawImage(bmp, 0, 0);
    }




 //that's what I've already gathered from the internet...it works fine 
 when i define a rectangle inside the Pic_Paint Method(just like the code 
 example shows below) and call draw 
 rectangle. It zooms in at the desired point just like i want it, but it 
 doesn't work with the DrawToBitmap method... to be totally honest I'm 
 pretty overtaxed when it comes to matrices since i've never used them



 private void Pic_Paint(object sender, PaintEventArgs e)
    {
        //Graphics g = e.Graphics;
        //g.Transform = transform;

        //Pen mypen = new Pen(Color.Red, 5);
        //Rectangle rect = new Rectangle(10, 10, 30, 30);
        //e.Graphics.DrawRectangle(mypen, rect);

    }

 protected override void OnMouseWheel(MouseEventArgs mea) //changed
    {
        pic.Focus();
        if (pic.Focused == true && mea.Delta !=0 )
        {
            Point picPoint = 
            pic.PointToClient(this.PointToScreen(mea.Location));
            ZoomScroll(picPoint, mea.Delta > 0);
        }


 private void ZoomScroll(Point location, bool zoomIn)
    {

        float newScale = Math.Min(Math.Max(zoomingfactor + (zoomIn ? 
        scrollvalue : -scrollvalue), 0.1f), 10);

        if (newScale != zoomingfactor)
        {
            float adjust = newScale / zoomingfactor;
            zoomingfactor = newScale;


            transform.Translate(-location.X, -location.Y, 
            MatrixOrder.Append);


            transform.Scale(adjust, adjust, MatrixOrder.Append);


            transform.Translate(location.X, location.Y, 
            MatrixOrder.Append);

            pic.Invalidate();
        }
    }

Что, вероятно, также важно знать, я использую таймер для постоянного вызова DrawBitmap (), где положение объекта рисуется с помощью graphics.FillEllipse (...)

Буду очень признателен, если вы мне поможете, пожалуйста, не стесняйтесь спрашивать больше информации в любое время!

...