2009-11-10 7 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

+0

System.ArgumentException { "매개 변수가 유효하지 않습니다."} – Nathan

+0

'텍스트'에 무엇이 있습니까? –

답변

20

오래 전 동일한 오류가 발생했습니다. 이유는 개체 중 하나가 이미 처리 된 것이기 때문입니다 ...

어쩌면 글꼴이 다른 곳이나 그래픽 개체 자체에 배치되고있는 것일 수 있습니다. 브러시가 문제의 원인이 될 것이라고 생각하지는 않습니다. 왜냐하면 브러시는 메소드에 국한되어 있고 처리되지 않았 음을 알기 때문입니다.

편집 :

는 그래픽 객체가 배치되어 있는지 여부를 알고는 간단합니다 : 그것의 모든 속성이 예외를 throw합니다. 글꼴은 쉽지 않습니다. 왜냐하면 모든 속성이 여전히 작동하기 때문입니다. 글꼴이 삭제되었는지 여부를 확인하는 한 가지 방법은 복제하려고 시도하는 것입니다 (글꼴을 테스트하려면 조사 식 창에서 font.Clone()을 추가 할 수 있음). 클론이 작동하면 글꼴이 삭제되지 않습니다. 그렇지 않으면 예외를 throw합니다.

+0

그래도 처분 된 것으로는 보이지 않습니다. 인쇄 미리보기에 그려지는 모든 것은 그 자체 클래스에서 가져 오므로 혼란 스럽습니다. 그래서 양식이 만지는 이유를 이해하지 못합니다. – Nathan

+1

내 편집을 확인하십시오. 처분 된 글꼴은 해당 속성이 처리되지 않은 것처럼 계속 표시됩니다. –

+0

나는 this.Font.GetHeight() 예외를 던지고 지금 그것을 좁혔습니다. 어떤 아이디어? – Nathan

1

명시 적으로 x/y 좌표를 부동화해야합니까 (예 : 0 대신 0.0f)? 나는 런타임 에러가 아닌 컴파일 에러를 기대한다.

+0

내가 제안한 내용으로 전환했는데 오류가 계속 발생합니다. – Nathan

0

나는 OnPaint로 그렇게 많이 해왔다 ... 네가 보여주는 것은 모두 직사각형에 관한 것이다. 사각형이나 문자열을 회전시키고 있습니까? 그것이 직사각형 인 경우 .DrawString 대신 .DrawRectangle이어야하지 않습니까?

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(); 
      } 
관련 문제