C# 缩放Picturebox根本不会改变图像

C# 缩放Picturebox根本不会改变图像,c#,winforms,graphics,scaling,C#,Winforms,Graphics,Scaling,我正在使用picturebox创建truss类实例的可视化视图。在绘画活动中,我通过直接在图片框上绘制来创建视觉效果。方法如下所示 private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (isDraw) { //Preparing to draw Graphics g = e.Graphics; g.SmoothingMode = Smoothing

我正在使用picturebox创建truss类实例的可视化视图。在绘画活动中,我通过直接在图片框上绘制来创建视觉效果。方法如下所示

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;
    }
}
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
如果(isDraw)
{
//准备画画
图形g=e.图形;
g、 SmoothingMode=SmoothingMode.AntiAlias;
g、 插值模式=插值模式.双三次;
RunEntry entry=this.passedHistory.SelectedItem作为RunEntry;
AnsFile objToDraw=entry.FileRead;
钢笔=新钢笔(颜色为黑色);
//获取位图的大小
int maxWidth=0,maxHeight=0;
foreach(对象中的AnsJoint关节。AnsJoint)
{
如果(joint.Location.X.Length>maxWidth)
{
maxWidth=(int)joint.Location.X.Length;
}
如果(关节位置Y长度>最大高度)
{
maxHeight=(int)joint.Location.Y.Length;
}
}
//拉丝接头
foreach(对象中的AnsJoint关节。AnsJoint)
{
PointF jointPoint=this.ToCartesian(新的PointF((float)joint.Location.X.Length-4f,(float)joint.Location.Y.Length+10f),最大高度);
e、 Graphics.DrawString(joint.JointID.ToString(),新字体(FontFamily.GenericMonospace,6f,FontStyle.Regular,GraphicsUnit.Point,1,false),Brush.Black,jointPoint);
}
//绘制面板和链接
foreach(objToDraw.AnsMembers中的AnsMember成员)
{
名单


除了我希望缩放桁架图像以填充更多的picturebox之外。我在paint事件方法的末尾添加了ScaleTransform调用,但这似乎没有任何影响,因为调用或不调用时图片大小相同。如何将我在picturebox上绘制的内容缩放到picturebox的大小?

调用
ScaleTransform
在绘制之前。您可以按如下方式计算所需的比例因子:

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

我从未使用过ScaleTransform,但这个问题的答案表明,在绘图调用之前需要设置它:(而且,看起来你的变换将缩小图形,而不是向上)。这就解决了它!足够简单。添加它作为答案,我会接受它