2013-09-27 3 views
1

아무도 C#에서 메서드간에 문자열을 공유하는 방법을 알고 있습니까? 필요에 여기에서 얻는 '캡처'문자열C에서 메서드 사이에 문자열 공유

private void timer1_Tick(object sender, EventArgs e) 
    { 
     // The screenshot will be stored in this bitmap. 
     Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height); 

     // The code below takes the screenshot and 
     // saves it in "capture" bitmap. 
     g = Graphics.FromImage(capture); 
     g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds); 

     // This code assigns the screenshot 
     // to the Picturebox so that we can view it 
     pictureBox1.Image = capture; 

    } 

:

 Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height); 

그리고 장소 '캡처'에서를

내가 내 문자열을 얻을 필요가 코드의 조각 이 방법 : 여기에서

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (paint) 
     { 
      using (Graphics g = Graphics.FromImage(!!!Put Capture in Here!!!)) 
      { 
       color = new SolidBrush(Color.Black); 
       g.FillEllipse(color, e.X, e.Y, 5, 5); 
      } 
     } 

    } 

:

 using (Graphics g = Graphics.FromImage(!!!Put Capture in Here!!!)) 

누군가 도와 드릴 수 있기를 바랍니다.

추신 : 모든 것을 이해하지 못한다면 저는 14 세이고 네덜란드 출신이기 때문에 나는 최고의 영어 작가가 아닙니다 .--).

+2

'class' 내에 (메소드가 아닌)'private 비트 맵 캡쳐;를두고'this.capture' /'capture'를 사용하여 각 함수 내에서 참조하십시오. –

+1

당신은 이미'paint'를 클래스 필드로 사용합니다. 'capture '와 동일하게 수행 –

답변

3

변수의 범위을보고 있습니다.

변수 capture은 메서드 수준에서 정의되어 있으므로 해당 메서드 만 사용할 수 있습니다.

클래스 수준 (메서드 외부)에서 변수를 정의 할 수 있으며 클래스의 모든 메서드에서 변수에 액세스 할 수 있습니다.

Bitmap capture; 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    // The screenshot will be stored in this bitmap. 
    capture = new Bitmap(screenBounds.Width, screenBounds.Height); 

    // The code below takes the screenshot and 
    // saves it in "capture" bitmap. 
    g = Graphics.FromImage(capture); 
    g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds); 

    // This code assigns the screenshot 
    // to the Picturebox so that we can view it 
    pictureBox1.Image = capture; 

} 

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (paint) 
    { 
     using (Graphics g = Graphics.FromImage(capture)) 
     { 
      color = new SolidBrush(Color.Black); 
      g.FillEllipse(color, e.X, e.Y, 5, 5); 
     } 
    } 

} 
+2

작업을 마친 후에는 반드시'Bitmap'을 처리해야합니다. 그렇지 않으면 GDI 자원이 부족할 때 오류가 발생할 수 있습니다. –