2016-04-14 72 views
1

我正在使用圖片框來創建我的桁架類的實例的視覺效果。我通過在繪畫事件中直接繪製到圖片框來創建視覺效果。該方法是這樣的縮放Picturebox不會改變圖像

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

其中產量我期待

enter image description here

,只是我想桁架圖像縮放,填補了PictureBox的更多結果。我將ScaleTransform調用添加到繪製事件方法的末尾,但這看起來沒有任何影響,因爲圖片的大小是相同的,無論是否有調用。我怎樣才能將我在畫框上畫的東西縮放到畫框的大小?

+2

我從來沒有使用ScaleTransform,但這個問題的答案表明,它需要繪圖調用之前設置:http://stackoverflow.com/questions/22615138/apply-scaletransform-to-graphics-gdi(此外,它看起來像你的變換將縮小圖形,而不是up。) – adv12

+0

修復它!夠簡單。添加它作爲答案,我會接受它 –

回答

1

在您繪製之前請致電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);