2014-06-13 14 views
0

아래쪽과 오른쪽에 많은 공백이 포함 된 일부 이미지가 있습니다. 사용자에게 표시하기 전에 공백을 자르고 싶습니다.C에서 이미지 오른쪽의 공백 자르기

지금까지는 흰색 픽셀을 맨 아래에서 감지하여 구현했습니다. 픽셀 형식은 형식 32BppArgb입니다.

 byte[] byteImage = Convert.FromBase64String(imageString); 

     MemoryStream ms = new MemoryStream(byteImage, 0, byteImage.Length); 
     ms.Write(byteImage, 0, byteImage.Length); 
     Image image = Image.FromStream(ms, true); 
     Bitmap bmpImage = new Bitmap(image); 
     int imageDataHeight = bmpImage.Height; 
     int imageWidth = bmpImage.Width; 
     int imageHeight = bmpImage.Height; 

     BitmapData data = bmpImage.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, bmpImage.PixelFormat); 
     try 
     { 
      unsafe 
      { 
       int width = data.Width/2; 
       for (int y = data.Height-1; y > 0 ; y--) 
       { 
        byte* row = (byte*)data.Scan0 + (y * data.Stride); 

        int blue = row[width * 3]; 
        int green = row[width * 2]; 
        int red = row[width * 1]; 

        if ((blue != 255) || (green != 255) || (red != 255)) 
        { 
         imageDataHeight = y + 50; 
         break; 
        } 
       } 
      } 
     } 
     finally 
     { 
      bmpImage.UnlockBits(data); 
     } 

     // cropping a rectangle based on imageDataHeight 
     // ... 

어떻게 제대로 왼쪽이 아닌 흰색 픽셀을 감지하는 오른쪽에서 시작하여 열을 반복?

+0

당신을 도움이 될 수 http://imageresizing.net/plugins/whitespacetrimmer –

답변

0

이렇게하면 대상 폭을 줄 것이다 : 그런 다음

unsafe int CropRight(BitmapData data) 
    { 
     int targetWidth = data.Width; 
     for (int x = data.Width - 1; x >= 0; x--) 
     { 
      bool isWhiteStripe = true; 
      for (int y = data.Height - 1; y > 0; y--) 
      { 
       if (!IsWhite(data, x, y)) 
       { 
        isWhiteStripe = false; 
        break; 
       } 
      } 

      if (!isWhiteStripe) 
       break; 
      targetWidth = x; 
     } 
     return targetWidth; 
    } 

    int bytesPerPixel = 4; //32BppArgb = 4bytes oer pixel 
    int redOffset = 1; // 32BppArgb -> red color is the byte at index 1, alpha is at index 0 

    unsafe bool IsWhite(BitmapData data, int x, int y) 
    { 
     byte* row = (byte*)data.Scan0 + (y * data.Stride) + (x * bytesPerPixel); 

     int blue = row[redOffset + 2]; 
     int green = row[redOffset + 1]; 
     int red = row[redOffset]; 

     // is not white? 
     if ((blue != 255) || (green != 255) || (red != 255)) 
     { 
      return false; 
     } 

     return true; 
    } 

, 이미지를자를 수 : How to crop an image using C#?