面板图放大C# [英] Panel Drawing zoom in C#

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

问题描述

我有包含面板的形式,并在此面板I绘制形状,如矩形和圆形的,我需要放大到这个造型,我看到几个选项,但他们大多使用图片框。我应该使用位图创建面板区域为位图,改变缩放系数?这会帮助我也在进一步,如果我想有平移,而不是绘制图像不进适合在面板尺寸。

I have a Form that contain a panel, and in this panel I draw shapes, like rectangles and circles, I need to zoom into this shapes, I saw couple options but most of them using PictureBox. Should I use Bitmap creating the panel area as a bitmap and change the zooming factor ?? would this help me also further if I want to have Panning and not draw images not into the fit in the panel size.

下面是我的代码的快照

  private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        SolidBrush myBrush = new SolidBrush(Color.Black);
        Pen p = new Pen(Color.Black);
        int RecScale = 1;
        foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor)
        {
            RectangleF rec = new RectangleF((float)(resistorRow.CenterX - resistorRow.Length / 2), (float)(resistorRow.CenterY - resistorRow.Width/ 2), (float)resistorRow.Length, (float)resistorRow.Width);
            float orientation = 360 - (float)resistorRow.Orientation;
            PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY);
            PointF[] points = CreatePolygon(rec, center, orientation);
            if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY))
            {
                g.FillEllipse(myBrush, (float)resistorRow.HiX - 5 , (float)resistorRow.HiY - 5, 10, 10);
                g.DrawLine(p, new PointF((float)resistorRow.HiX, (float)resistorRow.HiY), center);
            }
            g.FillPolygon(myBrush, points);
        }
    }



能否示例代码来提供。
非常感谢

Can a sample code be provided. Many Thanks

太平绅士

推荐答案

下面是一个的方式来扩展由缩放图形目标:

Here is a way to scale the drawing by scaling the Graphics object:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.ScaleTransform(zoom, zoom);

    // some demo drawing:
    Rectangle rect = panel1.ClientRectangle;
    g.DrawEllipse(Pens.Firebrick, rect);
    using (Pen pen = new Pen(Color.DarkBlue, 4f)) g.DrawLine(pen, 22, 22, 88, 88);

}

下面我们存储缩放级别:

Here we store the zoom level:

float zoom = 1f;



在这里我们设置和更新面板:

Here we set it and update the Panel:

private void trackBar1_Scroll(object sender, EventArgs e)
{
   // for zooming between, say 5% - 500%
   // let the value go from 50-50000, and initialize to 100 !
    zoom = trackBar1.Value / 100f;  
    panel1.Invalidate();
}



两个例子截图:

Two example screenshots:

在这里输入的形象描述 href=\"http://i.stack.imgur.com/U0mUh.png\" rel=\"nofollow\">

请注意是如何很好地这个尺度钢笔的宽度,以及。打开抗锯齿将是一个不错的主意..: g.SmoothingMode = SmoothingMode.AntiAlias;

Note how nicely this scales the Pen widths as well. Turning on antialiasing would be a good idea..: g.SmoothingMode = SmoothingMode.AntiAlias;

这篇关于面板图放大C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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