2013-05-16 2 views
3

C#에서 자르기 이미지 공백을 요청했지만 포럼에서 일부 메소드를 검색하지만 요청을 만족시킬 수 없습니다.C#에서 이미지 자르기 만들기

enter image description here

내가 기대하는 결과 원래의 이미지가

,

enter image description here

어떤 도움을 주셔서 감사합니다.

+1

흰색 픽셀이 끊어지지 않도록 각면에서 얼마나 깊이 갈 수 있는지 측정하십시오. – spender

+0

이미지 크기 조정 및 자르기에 대한 링크를 확인하십시오. 도움이 될 수 있습니다 : http://jasonjano.wordpress.com/2010/02/13/image-resizing-and-cropping-in-c/ –

+0

위쪽, 아래쪽, 왼쪽 및 오른쪽 검은 점의 좌표를 찾고 자르기 http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c와 같은 사각형 – Alex

답변

6
입니다

첫 번째 이미지 d를 가져올 수 있습니다. ata (이미지가 있음)를 선택하고 새 이미지에 데이터를 그립니다. 이 방법을 사용해보십시오. 그것이 당신을 도울 수 있기를 바랍니다.

private static Bitmap ImageTrim(Bitmap img) 
{ 
    //get image data 
    BitmapData bd= img.LockBits(new Rectangle(Point.Empty, img.Size), 
    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 
    int[] rgbValues = new int[img.Height * img.Width]; 
    Marshal.Copy(bd.Scan0, rgbValues, 0, rgbValues.Length); 
    img.UnlockBits(bd); 


    #region determine bounds 
    int left = bd.Width; 
    int top = bd.Height; 
    int right = 0; 
    int bottom = 0; 

    //determine top 
    for (int i = 0; i < rgbValues.Length; i++) 
    { 
     int color = rgbValues[i] & 0xffffff; 
     if (color != 0xffffff) 
     { 
      int r = i/bd.Width; 
      int c = i % bd.Width; 

      if (left > c) 
      { 
       left = c; 
      } 
      if (right < c) 
      { 
       right = c; 
      } 
      bottom = r; 
      top = r; 
      break; 
     } 
    } 

    //determine bottom 
    for (int i = rgbValues.Length - 1; i >= 0; i--) 
    { 
     int color = rgbValues[i] & 0xffffff; 
     if (color != 0xffffff) 
     { 
      int r = i/bd.Width; 
      int c = i % bd.Width; 

      if (left > c) 
      { 
       left = c; 
      } 
      if (right < c) 
      { 
       right = c; 
      } 
      bottom = r; 
      break; 
     } 
    } 

    if (bottom > top) 
    { 
     for (int r = top + 1; r < bottom; r++) 
     { 
      //determine left 
      for (int c = 0; c < left; c++) 
      { 
       int color = rgbValues[r * bd.Width + c] & 0xffffff; 
       if (color != 0xffffff) 
       { 
        if (left > c) 
        { 
         left = c; 
         break; 
        } 
       } 
      } 

      //determine right 
      for (int c = bd.Width - 1; c > right; c--) 
      { 
       int color = rgbValues[r * bd.Width + c] & 0xffffff; 
       if (color != 0xffffff) 
       { 
        if (right < c) 
        { 
         right = c; 
         break; 
        } 
       } 
      } 
     } 
    } 

    int width = right - left + 1; 
    int height = bottom - top + 1; 
    #endregion 

    //copy image data 
    int[] imgData = new int[width * height]; 
    for (int r = top; r <= bottom; r++) 
    { 
     Array.Copy(rgbValues, r * bd.Width + left, imgData, (r - top) * width, width); 
    } 

    //create new image 
    Bitmap newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb); 
    BitmapData nbd 
     = newImage.LockBits(new Rectangle(0, 0, width, height), 
      ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); 
    Marshal.Copy(imgData, 0, nbd.Scan0, imgData.Length); 
    newImage.UnlockBits(nbd); 

    return newImage; 
}    
2

이미지에 2 가지 색상 (흰색과 검은 색) 만있는 경우 이미지를 반복하여 왼쪽 상단 픽셀 세트와 오른쪽 하단 픽셀 세트를 찾은 다음 잘라낼 수 있습니다. (의사 코드, 의존 당신이

int minX = int.MaxValue, maxX = 0, minY = int.MaxValue, maxY = 0; 
for (x = 0; x < image.Width, x++) 
{ 
    for (y = 0; y < image.Height; y++) 
    { 
     if (image[x, y] == 1) 
     { 
      if (x < minX) minX = x; 
      else if (x > maxX) maxX = x; 
      if (y < minY) minY = y; 
      else if (y > maxY) maxY = y; 
     } 
    } 
} 

다음 당신은 당신이 이미지

나는이 최적화 될 수있는 확신을자를 수있게된다 좌표를해야합니다) 이미지의 픽셀을 얻기 위해 사용하지만 일반적인 생각