[英]How to reset value of captured pixel
我正在嘗試制作一個C#函數,該函數從屏幕快照位圖中返回每四個像素的R,G和B值。 這是我的代碼的一部分。
for (int ix = 4; ix < 1366; ix = ix + 4)
{
x = x + 4;
for (int iy = 3; iy < 768; iy = iy + 4)
{
y = y + 4;
System.Drawing.Color pixelcolor = screenshot.GetPixel(x,y);
red = kolorpiksela.R;
green = kolorpiksela.G;
blue = kolorpiksela.B;
sumR = sumR + red;
sumG = sumG + green;
sumB = sumB + blue;
}
}
screenshot
是位圖。 最后,“和”值除以像素數。 我的問題是它不起作用。 我得到的錯誤是:
System.Drawing.dll中發生了類型為'System.ArgumentOutOfRangeException'的未處理異常
附加信息:參數必須為正且<高度。
我認為該錯誤的原因是應該在每一步都重置pixelcolor
,但是我嘗試在最近的兩天內這樣做。 沒有成功
我只是不知道這是怎么回事:
for (int ix = 4; ix < 1366; ix = ix +4 )
{
x = x + 4;
for (int iy = 3; iy < 768; iy = iy + 4)
{
y = y + 4;
我的猜測是它應該看起來像:
for (int x = 0; x < screenshot.Width; x += 4 )
{
for (int y = 0; y < screenshot.Height; y += 4)
{
System.Drawing.Color pixelcolor = screenshot.GetPixel(x,y);
盡量不要在不必時鍵入值。 我什至可能會將“ 4”轉換為const或某個位置的屬性。
我假設您的位圖是1366 x 768。
在您的ix循環中,在第一個迭代ix = 4上。在第431次發信ix = 1364,因此它進入了循環。
問題是您然后將4加到x(所以x = 1368),這超出了您的1366邊界。
對位圖尺寸進行硬編碼不是最好的方法,但是如果要檢查x = x + 4是否超出1366邊界,則可以快速輸入。
768的iy循環幾乎相同。
嘗試這個:
for (int x = 0; x < screenshot.Width; x += 4)
{
for (int y = 0; y < screenshot.Height; y += 4)
{
Color pixelcolor = screenshot.GetPixel(x, y);
sumR += pixelcolor.R;
sumG += pixelcolor.G;
sumB += pixelcolor.B;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.