이미지 행 또는 열을 검사하고 모든 흰색 픽셀이 포함 된 경우 해당 행 또는 열을 잘라내려고합니다. 이유를 모르겠지만이 코드 조각을 실행할 때 TrimLeft의 img.Width는 1이며, 그 중 하나에서 1을 뺍니다. 실제 이미지 너비는 175입니다. 결국 ArgumentException이 생깁니다. 나는 오른쪽면을 잘라내 기와 비슷한 방법을 사용하고 있습니다. 이 현저하게 비효율적 일 것 같은C# 비트 맵 어려움
class ImageHandler
{
public Bitmap img;
public List<int[]> pixels = new List<int[]>();
public ImageHandler(String path)
{
img = new Bitmap(path);
GetPixels();
}
public void TrimLeft()
{
while (CheckColIfWhite(0, 0))
{
Rectangle cropBox = new Rectangle(1, 0, (img.Width-1), img.Height);
Bitmap cropdImg = CropImage(img, cropBox);
img = cropdImg;
}
}
public bool CheckColIfWhite(int colx, int starty)
{
bool allPixelsWhite = false;
int whitePixels = 0;
for (int y = starty; y < img.Height; y++)
{
if (pixels[y][colx] >= 200) { whitePixels++; }
else { return false; }
if (whitePixels == img.Height) { allPixelsWhite = true; }
}
return allPixelsWhite;
}
public void GetPixels()
{
for (int y = 0; y < img.Height; y++)
{
int[] line = new int[img.Width];
for (int x = 0; x < img.Width; x++)
{
line[x] = (int) img.GetPixel(x, y).R;
}
pixels.Add(line);
}
}
public Bitmap CropImage(Bitmap tImg, Rectangle area)
{
Bitmap bmp = new Bitmap(tImg);
Bitmap bmpCrop = bmp.Clone(area, bmp.PixelFormat);
return bmpCrop;
}
}
이 V이었다 에리 도움이 감사합니다. 내 방법이 비효율적이라는 것을 알았습니다. 더 좋은 방법을 알지 못했습니다. 원래 이미지의 모든 픽셀을 가져 와서 파일을 여러 번 읽지 않고 저장했습니다. – sjensen85