我寫了一個小應用程序,將在我的工作環境中用於裁剪圖像。包含圖像的窗體(.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
函數是好的 - 它的工作原理是,如果我使用表單作爲選擇區域,但使用矩形會使爵士音樂有點過分。
嗯,它仍然抓住了錯誤的區域,Y座標+ 50的某些原因。 – 2012-02-23 07:58:20
哦,你的代碼中的Point被命名爲'relativePosition',但是你可以將它作爲ptRelativePosition來引用 - 只是你知道的。 – 2012-02-23 08:03:07
@AdLib:你可以上傳一個項目什麼的? (對於遲到的回覆,由於某種原因,我沒有收到評論通知。) – Ryan 2012-02-23 14:35:48