C#中的面板绘图放大

Panel Drawing zoom in C#

我有一个包含面板的表单,我在这个面板中绘制形状,如矩形和圆形,我需要放大这些形状,我看到了几个选项,但其中大部分使用 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);
        }
    }

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

日本

既然你是从头开始画的,你不能根据缩放系数调整你画的大小吗?

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

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

这里有一种按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;

这里我们设置它并更新面板:

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

两个示例截图:

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