不用滚动缩放图形 [英] Zooming graphics without scrolling

查看:156
本文介绍了不用滚动缩放图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,我使用 ScaleTransform()来实现缩放。

为了在变焦之后将中心内容保持在中心,还需要滚动。例如,如果我放大(使事物变大),X和Y原点应该增加,以便大部分内容不会向下移动并向右移动。 (也就是说,当我放大时,其中一些内容应该消失在左侧和上方。)

有没有人计算过要在X和Y方向响应变焦?

例如:

 <$ c $ e.Graphics.ScaleTransform(2.0F,2.0F); 
e.Graphics.TranslateTransform(?,?);

我的论点是 TranslateTransform()以便内容的中心部分保持在中心位置?



注意:我没有显示图片。我正在将图形内容绘制到用户控件的表面上。

div class =h2_lin>解决方案

这应该工作,我无法想象任何更简单的方法;它假定您已决定放大的中心。我选择了以面板为中心绘制:

  float zoom = 1f; 

private void drawPanel1_Paint(object sender,PaintEventArgs e)
{
Point c = new Point(drawPanel1.ClientSize.Width / 2,drawPanel1.ClientSize.Height / 2);

//一个用于测试
的蓝色智能检查e.Graphics.FillEllipse(Brushes.DodgerBlue,c.X - 3,c.Y - 3,6,6);

//你正在寻找的偏移量:
float ox = c.X *(zoom - 1f);
float oy = c.Y *(zoom - 1f);

//先移动然后缩放
e.Graphics.TranslateTransform(-ox,-oy);
e.Graphics.ScaleTransform(zoom,zoom);

//现在我们可以围绕我们的要点绘制c
尺寸sz = new尺寸(300,400);
int count = 10;
int wx = sz.Width / count;
int wy = sz.Height / count;

for(int i = 0; i {
Rectangle r = new Rectangle(cX - i * wx / 2,cY - i * wy / 2,i * wx,i * wy);
e.Graphics.DrawRectangle(Pens.Red,r);
}
}



请注意移动和缩放的顺序!


I have a user control and I'm using ScaleTransform() to implement zoom.

However, in order to keep the center content in the center after the zoom, it is also necessary to scroll. For example, if I zoom in (make things bigger), the X and Y origin should increase so that most of the content does not move down and to the right. (That is, as I zoom in, some of the content should disappear to the left and top.)

Has anyone worked out the calculations of how much to scroll in the X and Y direction in response to a zoom?

For example:

e.Graphics.ScaleTransform(2.0F, 2.0F);
e.Graphics.TranslateTransform(?, ?);

What would be my arguments to TranslateTransform() be so that the center part of the content remains at the center?

Note: I am not displaying an image. I am drawing the graphic content to the surface of my user control.

Or perhaps there's an even easier way?

解决方案

This should work and I can't imagine any easier way; it assumes you have decided on the center of the zooming. I have chosen to draw centered on the panel:

float zoom = 1f;

private void drawPanel1_Paint(object sender, PaintEventArgs e)
{
    Point c = new Point(drawPanel1.ClientSize.Width / 2, drawPanel1.ClientSize.Height / 2);

    // a blue sanity check for testing
    e.Graphics.FillEllipse(Brushes.DodgerBlue, c.X - 3, c.Y - 3, 6, 6);

    // the offsets you were looking for:
    float ox = c.X * ( zoom - 1f);
    float oy = c.Y * ( zoom - 1f);

    // first move and then scale
    e.Graphics.TranslateTransform(-ox, -oy);
    e.Graphics.ScaleTransform(zoom, zoom);

     // now we can draw centered around our point c
    Size sz = new Size(300, 400);
    int count = 10;
    int wx = sz.Width  / count;
    int wy = sz.Height / count;

    for (int i = 0; i < count; i++)
    {
        Rectangle r = new Rectangle(c.X - i * wx / 2 , c.Y - i * wy / 2, i * wx, i * wy );
        e.Graphics.DrawRectangle(Pens.Red, r );
    }
}

Note the order of moving and scaling!

这篇关于不用滚动缩放图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆