我知道標題是有點怪,但我不知道爲什麼這是給錯誤。for循環工作寬度X高度,但不是高度X寬度爲BMP圖像
我的任務
獲取從圖像的開始和結束的x,y的索引,所以我可以畫他們周圍的矩形。目前我正在努力獲取圖像中起始和結束像素的位置(x,y)。
這是我4個功能4位中
static int[] getStartX(Bitmap bmp) //run perfect
{
int[] arr = new int[2];
for (int x = 0; x < bmp.Width - 1; x++)
{
for (int y = 0; y < bmp.Height - 1; y++)
{
Color color = bmp.GetPixel(x, y);
if (color.ToArgb() == Color.Black.ToArgb())
{
arr[0] = x;
arr[1] = y;
return arr;
}
}
}
return arr;
}
static int[] getStartY(Bitmap bmp)
{
int[] arr = new int[2];
for (int x = 0; x < bmp.Height - 1; x++)
{
for (int y = 0; y < bmp.Width - 1; y++)
{
Color color = bmp.GetPixel(x, y); //Exception out of range
if (color.ToArgb() == Color.Black.ToArgb())
{
arr[0] = x;
arr[1] = y;
return arr;
}
}
}
return arr;
}
static int[] getEndX(Bitmap bmp)
{
int[] arr = new int[2];
for (int x = bmp.Width-1 ; x >= 0; x--)
{
for (int y = bmp.Height - 1; y >=0 ; y--)
{
Color color = bmp.GetPixel(x, y);//Exception out of range
if (color.ToArgb() == Color.Black.ToArgb())
{
arr[0] = x;
arr[1] = y;
return arr;
}
}
}
return arr;
}
static int[] getENdY(Bitmap bmp)
{
int[] arr = new int[2];
for (int x = bmp.Height - 1; x >= 0; x--)
{
for (int y = bmp.Width - 1; y >= 0; y--)
{
Color color = bmp.GetPixel(x, y); //Exception out of range
if (color.ToArgb() == Color.Black.ToArgb())
{
arr[0] = x;
arr[1] = y;
return arr;
}
}
}
return arr;
}
只有我的第一個功能正常工作和所有其他給予異常超出範圍的錯誤就行代碼指出。
這是我Image
預計輸出像素爲紅色顯示:Output 主要
static void Main(string[] args)
{
Bitmap bmp = new Bitmap("C:\\Users\\AV\\5.bmp");
int[] arr = new int[2];
arr = getStartX(bmp);
bmp.SetPixel(arr[0], arr[1], Color.Blue);
arr = getStartY(bmp);
bmp.SetPixel(arr[0], arr[1], Color.Blue);
arr = getEndX(bmp);
bmp.SetPixel(arr[0], arr[1], Color.Blue);
arr = getENdY(bmp);
bmp.SetPixel(arr[0], arr[1], Color.Blue);
bmp.Save("1.bmp");
}
上,我想每一個位置繪製藍色的點,來表示。 我想只得到4個位置,所以我可以在它周圍繪製一個矩形。 如果任何人都可以提前幫助我。
那麼,你的目標周圍繪製圖像中的線條的長方形?如果不是,那麼「x,y索引的開始和結束」是什麼意思。 –
我的目標是圍繞這條線繪製一個矩形,但是它的區域的矩形,通過開始和結束x,y軸,我是指來自每一側的起始部分線。 –
對不起,還是有點困惑。你可以張貼預期輸出的圖片嗎? –