問題描述
我正在創(chuàng)建一個(gè)應(yīng)用程序(Windows 窗體),允許用戶根據(jù)他們選擇的位置(拖動(dòng)以選擇區(qū)域)截取屏幕截圖.我想添加一個(gè)放大的小預(yù)覽窗格",以便用戶可以更精確地選擇他們想要的區(qū)域(更大的像素).在 mousemove 事件中,我有以下代碼...
I'm creating an application (Windows Form) that allows the user to take a screenshot based on the locations they choose (drag to select area). I wanted to add a little "preview pane" thats zoomed in so the user can select the area they want more precisely (larger pixels). On a mousemove event i have a the following code...
private void falseDesktop_MouseMove(object sender, MouseEventArgs e)
{
zoomBox.Image = showZoomBox(e.Location);
zoomBox.Invalidate();
bmpCrop.Dispose();
}
private Image showZoomBox(Point curLocation)
{
Point start = new Point(curLocation.X - 50, curLocation.Y - 50);
Size size = new Size(100, 90);
Rectangle rect = new Rectangle(start, size);
Image selection = cropImage(falseDesktop.Image, rect);
return selection;
}
private static Bitmap bmpCrop;
private static Image cropImage(Image img, Rectangle cropArea)
{
if (cropArea.Width != 0 && cropArea.Height != 0)
{
Bitmap bmpImage = new Bitmap(img);
bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
bmpImage.Dispose();
return (Image)(bmpCrop);
}
return null;
}
失敗并出現(xiàn)內(nèi)存不足異常的行是:
The line that fails and has the Out of Memory exception is:
bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
基本上它的作用是在鼠標(biāo)指針周圍取一個(gè) 100x90 的矩形并將其拉入縮放框,這是一個(gè)圖片框控件.但是,在此過程中,我收到內(nèi)存不足錯(cuò)誤.我在這里做錯(cuò)了什么?
Basically what this does is it takes a 100x90 rectangle around the mouse pointer and pulls that into the zoomBox, which is a picturebox control. However, in the process, i get an Out Of Memory error. What is it that i am doing incorrectly here?
感謝您的幫助.
推薦答案
C# 成像中的內(nèi)存不足,通常是錯(cuò)誤的 rect 或 point 的標(biāo)志 - 有點(diǎn)紅鯡魚.我敢打賭 start
在發(fā)生錯(cuò)誤或 Size.Hight + Y 或 Size.Width + X 大于圖像的高度或?qū)挾葧r(shí)具有 負(fù) X 或 Y.
Out of memory in C# imaging, is usually sign of wrong rect or point - a bit of red herring. I bet start
has negative X or Y when error happens or the Size.Hight + Y or Size.Width + X is bigger than Hight or width of the image.
這篇關(guān)于C# 創(chuàng)建位圖時(shí)內(nèi)存不足的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!