面板绘图放大C#

时间:2015-08-25 12:39:53

标签: c# winforms zoom panel

我有一个包含面板的Form,在这个面板中我绘制了形状,如矩形和圆形,我需要放大这些形状,我看到几个选项,但大多数使用PictureBox。我应该使用位图创建面板区域作为位图并更改缩放系数??如果我想要进行平移并且不能在面板尺寸中绘制图像,这对我的帮助也会更大。

以下是我的代码的快照

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

是否可以提供示例代码。 非常感谢

太平绅士

2 个答案:

答案 0 :(得分:5)

以下是一种按scaling the Graphics对象缩放绘图的方法:

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

}

这里我们存储缩放级别:

float zoom = 1f;

我们在此设置并更新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();
}

两个示例屏幕截图:

enter image description here enter image description here

请注意,这也很好地缩放了Pen宽度。打开抗锯齿是个好主意..:g.SmoothingMode = SmoothingMode.AntiAlias;

答案 1 :(得分:0)

由于你是从头开始画画,你不能根据缩放系数调整你的尺寸吗?

您可以将绘图尺寸乘以缩放系数。假设您的缩放系数为:

  • 0.5表示50%缩放(这会缩小图纸尺寸)
  • 1.0表示100%(实际尺寸)
  • 1.5 for 150%(更大尺寸),您可以通过这种方式计算宽度:
object.Width = originalWidth * zoomFactor;