Scaling Picturebox does not change image at all

为君一笑 提交于 2019-12-20 03:53:33

问题


I'm using a picturebox to create a visual of an instance of my truss class. I'm creating the visual by drawing directly onto the picture box in the paint event. The method looks like this

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (isDraw)
    {
        //Preparing to draw
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.Bicubic;
        RunEntry entry = this.passedHistory.SelectedItem as RunEntry;
        AnsFile objToDraw = entry.FileRead;
        Pen pen = new Pen(Color.Black);

        //Getting size of bitmap
        int maxWidth = 0, maxHeight = 0;
        foreach (AnsJoint joint in objToDraw.AnsJoints)
        {
            if (joint.Location.X.Length > maxWidth)
            {
                maxWidth = (int)joint.Location.X.Length;
            }
            if (joint.Location.Y.Length > maxHeight)
            {
                maxHeight = (int)joint.Location.Y.Length;
            }
        }

        //Drawing joints
        foreach (AnsJoint joint in objToDraw.AnsJoints)
        {
            PointF jointPoint = this.ToCartesian(new PointF((float)joint.Location.X.Length - 4f, (float)joint.Location.Y.Length + 10f), maxHeight);
            e.Graphics.DrawString(joint.JointID.ToString(), new Font(FontFamily.GenericMonospace, 6f, FontStyle.Regular, GraphicsUnit.Point, 1, false), Brushes.Black, jointPoint);
        }

        //Draw the panels and links
        foreach (AnsMember member in objToDraw.AnsMembers)
        {
            List<AnsPanel> panels = member.Panels; //Drawing the panels

            foreach (AnsPanel pan in panels)
            {
                pen.Color = Color.Red;
                PointF p1 = this.ToCartesian(new PointF((float)pan.I.Location.X.Length, (float)pan.I.Location.Y.Length), maxHeight);
                PointF p2 = this.ToCartesian(new PointF((float)pan.J.Location.X.Length, (float)pan.J.Location.Y.Length), maxHeight);

                g.DrawEllipse(pen, p1.X - 2.5f, p1.Y - 2.5f, 5, 5);
                g.DrawEllipse(pen, p2.X - 2.5f, p2.Y - 2.5f, 5, 5);

                g.DrawEllipse(pen, p1.X - 3, p1.Y - 3.3f, 5, 5);
                g.DrawEllipse(pen, p2.X - 3, p2.Y - 3.3f, 5, 5);
                pen.Color = Color.Black;
                g.DrawLine(pen, p1, p2);
            }
            List<AnsLink> links = member.Links; //Drawing the links
            foreach (AnsLink link in links)
            {
                PointF p1 = this.ToCartesian(new PointF((float)link.I.Location.X.Length, (float)link.I.Location.Y.Length), maxHeight);
                PointF p2 = this.ToCartesian(new PointF((float)link.J.Location.X.Length, (float)link.J.Location.Y.Length), maxHeight);
                g.FillEllipse(Brushes.Green, p1.X - 1.5f, p1.Y - 1.5f, 3, 3);
                g.FillEllipse(Brushes.Green, p2.X - 1.5f, p2.Y - 1.5f, 3, 3);
                g.DrawLine(pen, p1, p2);
            }
        }
        g.ScaleTransform(.5f, .5f);
        pictureBox1.Tag = entry.FileName;
    }
}

Which yields the result I'm expecting

except that I want the truss image to scale to fill the picturebox more. I added the ScaleTransform call to the end of the paint event method but that doesn't seem to have any impact as the picture is the same size with or without the call. How can I scale what I draw on the picturebox to the size of the picturebox?


回答1:


Call ScaleTransform before you do your drawing. You can calculate the desired scale factor like this:

// place this code after the calculation of maxWidth and maxHeight
// but before the drawing code
PictureBox p = (PictureBox)sender;
float scaleFactor = Math.Min(
    ((float)p.Width) / maxWidth,
    ((float)p.Height) / maxHeight
);

g.ScaleTransform(scaleFactor, scaleFactor);


来源:https://stackoverflow.com/questions/36628305/scaling-picturebox-does-not-change-image-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!