2016-10-06 1 views
-2

가능한 업로드 된 이미지를 확인할 수있는 asp.net 응용 프로그램을 구축 흑백 영역 %?Asp.net 업로드 된 이미지를 확인 흑백 영역 %

예 : 사진을 업로드 enter image description here

사용자 후, 응용 프로그램이 검은 색과 흰색 영역을 계산합니다.

출력 :

** 화이트 : ** 32 %

블랙 ** : ** 68 %

+2

확실히 가능합니다. 정확히 어디에서 문제가 있습니까? –

답변

1

당신은 Bitmap 클래스를 사용할 수 있습니다. 속성이 WidthHeight 인 경우 총 픽셀 수를 계산할 수 있습니다. GetPixel 메서드를 사용하면 특정 픽셀의 색상을 얻을 수 있습니다. Color.White과 같은 알려진 색상을 다른 색상과 비교하려면 ToArgb 메소드를 사용할 수 있습니다.

Image yourImage = ... 

Bitmap bitmap = new Bitmap(yourImage); 
int whiteColorCount = 0; 
int blackColorCount = 0; 
for (int i = 0; i < bitmap.Width; i++) 
{ 
    for (int c = 0; c < bitmap.Height; c++) 
    { 
     int pixelHexColor = bitmap.GetPixel(i, c).ToArgb(); 
     if (pixelHexColor == Color.White.ToArgb()) 
     { 
      whiteColorCount++; 
     } 
     else if (pixelHexColor == Color.Black.ToArgb()) 
     { 
      blackColorCount++; 
     } 
    } 
} 

long totalPixelCount = bitmap.Width * bitmap.Height; 
double whitePixelPercent = whiteColorCount/(totalPixelCount/100.0); 
double blackPixelPercent = blackColorCount/(totalPixelCount/100.0); 
double otherPixelPercent = 100.0 - whitePixelPercent - blackPixelPercent; 
+0

이미지 yourImage = Image.FromFile (FileUploadControl.FileName); 이 오류가 발생하는 경우 System.IO.FileNotFoundException : test.png – KyLim

+2

@KyLim 예외의 이름 자체는 자명합니다. 파일의 올바른 경로를 가리키고 있습니까? 어쩌면 당신은'MapPath'를 놓치고 있기 때문에 코드는 어디에 있는지 알 수 있습니다. –

0

예, 이미지를 비트 맵으로 변환하여이 방법을 사용할 수 있습니다. 설정 한 픽셀 수를 반환합니다. 흑백 픽셀 수를 가져 와서 흑백 비율을 계산합니다.

// Return the number of matching pixels. 
private int CountPxl(Bitmap bmap, Color yourColor) 
{ 
// Loop through the pixels. 
int matches = 0; 
for (int y = 0; y < bmap.Height; y++) 
{ 
for (int x = 0; x < bmap.Width; x++) 
{ 
if (bmap.GetPixel(x, y) == yourColor) matches++; 
} 
} 
return matches; 
} 
관련 문제