2014-01-08 6 views
0

C#에서 작성한 메서드에서 일부 비트 맵을 렌더링 중입니다.DrawText 메서드의 현재 컨텍스트 오류에서 이름 "e"가 존재하지 않습니다.

TextRenderer.DrawText() 메서드를 사용하여 비트 맵에 텍스트를 그리는 법을 배우고 있습니다. 다음과 같이 코드 조각은 다음과 같습니다

TextRenderer.DrawText(e.Graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText);

그러나, e.Graphics에서, 나는 The name "e" does not exist under the current context하는 오류를 제공하고 있습니다.

무엇이 문제 일 수 있는지 궁금합니다.

내 Render 메서드에서 코드, Rhino의 공통 라이브러리 사용 : 코드는 위의 메뚜기 캔버스에 히트 맵을 렌더링과 같이

protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel) { 
     base.Render(canvas, graphics, channel); 

     if (channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Wires) { 
      var comp = Owner as KT_HeatmapComponent; 
      if (comp == null) 
       return; 

      List<HeatMap> maps = comp.CachedHeatmaps; 
      if (maps == null) 
       return; 

      if (maps.Count == 0) 
       return; 

      int x = Convert.ToInt32(Bounds.X + Bounds.Width/2); 
      int y = Convert.ToInt32(Bounds.Bottom + 10); 

      for (int i = 0; i < maps.Count; i++) { 
       Bitmap image = maps[i].Image; 
       if (image == null) 
        continue; 

       Rectangle mapBounds = new Rectangle(x, y, maps[i].Width, maps[i].Height); 
       mapBounds.X -= mapBounds.Width/2; 

       Rectangle edgeBounds = mapBounds; 
       edgeBounds.Inflate(4, 4); 

       GH_Capsule capsule = GH_Capsule.CreateCapsule(edgeBounds, GH_Palette.Normal); 
       capsule.Render(graphics, Selected, false, false); 
       capsule.Dispose(); 

       graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; 
       graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 
       graphics.DrawImage(image, mapBounds); 
       graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default; 
       graphics.DrawRectangle(Pens.Black, mapBounds); 
       TextRenderer.DrawText(e.Graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText); 

       y = edgeBounds.Bottom - (mapBounds.Height) - 4; 
      } 
     } 
    } 

:

enter image description here

을 그러나 제목과 x/y 축 텍스트를 추가하는 데 관심이 있습니다.

+4

여기에서 * e *가 예상됩니까? 여기에는 컨텍스트가 없습니다. 이벤트 처리기에 있습니까? 다른 것? –

+0

아마도 스 니펫은 페인트 핸들러에서 가져온 것이고 'e'라는 이벤트 객체가 전달되었을 것입니다. –

+0

방금 ​​배운 이벤트 처리기 arg입니다. 아니요, 이것은 이벤트 핸들러가 아니므로 잘못된 방법을 사용하여 비트 맵에 텍스트를 그릴 수 있습니다. 나는 좀 더 구체적으로 편집 할 것이다. – theGreenCabbage

답변

3

당신이 게시 한 코드는 그것이 Paint 이벤트 핸들러 내에서 할 수 있도록 설계 것처럼 보인다 (e는 관용적 변수 이름은 이벤트 처리기에 대한 EventArgs 적절하게 입력에 대한 때문에, 그리고 GraphicsPaintEventArgs의 속성입니다).

당신이 Graphics 개체를 이미 전달되는 것 때문에, 당신은 단지 graphics 매개 변수를 사용하여 코드에 e.Graphics을 대체 할 수 있어야합니다 귀하의 Render 함수에 전달된다. 해당 함수에 다음 행을 추가 할 수 있습니다.

TextRenderer.DrawText(graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText); 
+0

'graphics'가 이미 매개 변수로 전달됩니다. CreateGraphics()를 사용할 필요가 없습니다. – LarsTech

+0

위의'graphics '를 이미 선언했다면 어떻게해야합니까? – theGreenCabbage

+0

@LarsTech : 이것은 함수 본문을 추가 한 질문 편집 전에있었습니다. 원래 게시 된 것은 모두 단일 'TextRenderer' 행이었습니다. –

관련 문제