Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

winforms - Panel Drawing zoom in C#

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.

Here is a snapshot of my code

  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

Jp

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description hereenter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...