2012-02-22 5 views
0

이미지를 자르려면 내 작업 환경에서 사용할 작은 응용 프로그램을 작성했습니다. 이미지가 포함 된 창 폼 (.NET 3.5)에는 이미지의 섹션 위로 드래그하고 사각형 뒤에있는 버튼을 가져 오는 데 사용하는 투명 사각형이 있습니다.사각형 뒤의 이미지 캡처

현재 아래 코드를 사용하고 있는데, 캡처하는 영역이 꽤 많은 픽셀만큼 떨어져 있기 때문에 문제가됩니다. 내 CopyFromScreen 기능과 관련이 있다고 생각합니다.

//Pass in a rectangle 
    private void SnapshotImage(Rectangle rect) 
    { 
     Point ptPosition = new Point(rect.X, rect.Y); 
     Point ptRelativePosition; 

     //Get me the screen coordinates, so that I get the correct area 
     ptRelativePosition = PointToScreen(ptPosition); 

     //Create a new bitmap 
     Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 

     //Sort out getting the image 
     Graphics g = Graphics.FromImage(bmp); 

     //Copy the image from screen 
     g.CopyFromScreen(this.Location.X + ptPosition.X, this.Location.Y + ptPosition.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 
     //Change the image to be the selected image area 
     imageControl1.Image.ChangeImage(bmp); 
    } 

이미지가 상당히을 다시 그릴 때, 나는이 시점에서 영원히 감사하게 될 거라고 왜 누군가가 발견 할 수 있습니다. 또한, ChangeImage 함수는 괜찮습니다 - 선택 영역으로 양식을 사용하지만 직사각형을 사용하면 약간의 재즈가 발생합니다.

답변

0

를 흥미롭게도,이 때문에 기본 폼 이미지가 있다는 제어 폼 분리 상단의 모음 사이의 공간이었다 컨트롤과 메인 폼의 상단.

g.CopyFromScreen(relativePosition.X + 2, relativePosition.Y+48, Point.Empty.X, Point.Empty.Y, bmp.Size); 

건배 어떤 이유로

1

화면에 대한 상대 위치를 ptRelativePosition으로 가져 왔지만 실제로는 사용하지 않습니다. 양식의 테두리를 고려하지 않고 양식의 위치에 사각형의 위치를 ​​추가합니다. 여기

는 약간의 최적화를 고정 같습니다

// Pass in a rectangle 
private void SnapshotImage(Rectangle rect) 
{ 
    // Get me the screen coordinates, so that I get the correct area 
    Point relativePosition = this.PointToScreen(rect.Location); 

    // Create a new bitmap 
    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 

    // Copy the image from screen 
    using(Graphics g = Graphics.FromImage(bmp)) { 
     g.CopyFromScreen(relativePosition, Point.Empty, bmp.Size); 
    } 

    // Change the image to be the selected image area 
    imageControl1.Image.ChangeImage(bmp); 
} 
+0

흠, 그 여전히 잡는 잘못된 영역, Y 좌표 + 50 : 아래 그림과 같이이 문제를 얻으려면 단순히 그 픽셀을 고려하여 캡처 화면에서 한 줄을 수정했습니다. –

+0

아, 코드의 Point 이름이 'relativePosition'이지만 ptRelativePosition으로 참조합니다. 알다시피. –

+0

@AdLib : 프로젝트 등을 업로드 할 수 있습니까? (늦은 답변으로 죄송합니다. 어떤 이유로 든 알림을받지 못했습니다.) – Ryan