2009-11-10 230 views
8

當我關閉打印預覽窗口或移動打印預覽窗口時,我在下面的代碼中收到錯誤。我似乎無法理解爲什麼會發生這種情況。它發生在g.DrawString()行上。據我所知,也沒有處理任何東西。錯誤的C#參數無效錯誤

protected override void OnPaint(PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     Brush textBrush = new SolidBrush(this.ForeColor); 

     float width = TextRenderer.MeasureText(Text, this.Font).Width; 
     float height = TextRenderer.MeasureText(Text, this.Font).Height; 

     float radius = 0f; 

     if (ClientRectangle.Width < ClientRectangle.Height) 
      radius = ClientRectangle.Width * 0.9f/2; 
     else 
      radius = ClientRectangle.Height * 0.9f/2; 

     switch (orientation) 
     { 
      case Orientation.Rotate: 
       { 
        double angle = (_rotationAngle/180) * Math.PI; 
        g.TranslateTransform(
         (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle)))/2, 
         (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle)))/2); 
        g.RotateTransform((float)_rotationAngle); 
        g.DrawString(Text, this.Font, textBrush, 0, 0); 
        g.ResetTransform(); 
       } 
       break; 
     } 
    } 

第一部分:

at System.Drawing.Graphics.CheckErrorStatus(Int32 status) 
    at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format) 
    at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y) 
    at ScanPro.CustomControls.UserLabel.OnPaint(PaintEventArgs e) 

任何幫助,將不勝感激。

謝謝。

+0

什麼是例外? – jason 2009-11-10 14:57:38

+0

System.ArgumentException {「參數無效。」} – Nathan 2009-11-10 15:00:43

+0

「文本」中的內容是什麼? – 2009-11-10 16:41:45

回答

20

不久前我遇到了同樣的錯誤。原因是其中一個對象已經被處置...

也許字體被放置在其他地方,或圖形對象本身。我不認爲畫筆會造成問題,但因爲它是本地的方法,我們看到它沒有被處置。

編輯:

要知道一個圖形對象是否被佈置很簡單:它的所有性能拋出異常。對於字體來說並不容易,因爲所有的屬性都可以工作。我發現檢查字體是否處置的一種方法是嘗試克隆它(您可以在Watch窗口中添加font.Clone()以測試它)。如果克隆工作,字體不會丟棄。否則它會拋出異常。

+0

雖然我沒有看到任何東西。這只是令人困惑,因爲我繪製到Print Preview的所有內容都是從它自己的類中繪製的,所以我不明白爲什麼它甚至會觸及表單正在做的事情。 – Nathan 2009-11-10 17:29:17

+1

檢查我的編輯,處置的字體仍然顯示它的所有屬性,就好像它沒有被處置一樣。 – 2009-11-10 19:15:51

+0

我現在縮小了它在this.Font.GetHeight()拋出異常。有任何想法嗎? – Nathan 2009-11-10 19:30:05

1

您是否需要明確地將x/y座標設置爲float(即0.0f而不是0)?我期望編譯錯誤,而不是運行時錯誤,所以可能不會。

+0

我將其轉換爲您的建議,但我仍然收到錯誤消息。 – Nathan 2009-11-10 15:09:15

0

我還沒有用OnPaint做過那麼多...你展示的一切都是關於矩形的。你正在旋轉一個矩形或一個字符串?如果它是一個矩形不應該是.DrawRectangle而不是.DrawString?

0

如果有人有同樣的錯誤,我發現在「單獨的步驟」執行轉換解決了這個問題。

using (var graphics = Graphics.FromImage(destImage)) 
      { 
       using (var wrapMode = new ImageAttributes()) 
       { 


        wrapMode.SetWrapMode(WrapMode.TileFlipXY); 
        graphics.CompositingMode = CompositingMode.SourceCopy; 
        graphics.CompositingQuality = CompositingQuality.HighQuality; 
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
        graphics.SmoothingMode = SmoothingMode.HighQuality; 
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 
        graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); 

       }    
      } 

      using (var graphics = Graphics.FromImage(destImage)) 
      { 
       var font = new Font(new FontFamily("Arial"), 16, FontStyle.Regular, GraphicsUnit.Pixel); 
       var brush = new SolidBrush(Color.White); 
       graphics.DrawString("text to add", font, brush, 10F, 10F); 
       font.Dispose(); 
       brush.Dispose(); 
      }