2012-04-20 5 views
3

400x300px 이미지가 있다고 가정하고 200x200px로 자르고 서버 측 (C#, .NET 4.0)으로 자르고 싶습니다.이미지 중심을 자르십시오

어떻게하면됩니까? 일종의 캔버스를 사용하고 그것을 이동? 튜토리얼/코드 예제/제안?

+0

I는이 질문에 대한 비슷한 생각 : http://stackoverflow.com/questions/18014365/c-sharp-crop-image -from-center/27164374 # 27164374 – h3n

답변

-1

대상 크기로 새로운 비트 맵 개체를 만듭니다.

비트 맵 주위에 그래픽 개체를 만듭니다.

해당 Graphcs 개체에서 적절한 매개 변수를 사용하여 DrawImage()를 호출하면 첫 번째 그림에서 적절한 세그먼트를 잘라냅니다.

뭔가 같은 코드가 될 것이다 : 나는, 방법에 대한 문자 매개 변수를 포함을 파악하는 인텔리 수동을 사용하지 않은

Bitmap dstBitmap=new Bitmap(200, 200); 
using (Graphics g=Graphics.FromImage(dstBitmap)) 
{ 
    srcBitmap.DrawImage(dstBitmap, /* cropping parameters here */); 
} 
// at the end you'll have your bitmap in dstBitmap, ... 

. 이 같은

+0

"자르기 매개 변수"는 무엇을 의미합니까? – markzzz

+0

자신을 알아 내려고 노력하십시오. DrawImage() 메서드에는 여러 가지 오버로드가 있으며 사용자에게 적합한 오버로드를 찾아야합니다. 자르려는 이미지 부분의 좌표가있는 Rect를 정의해야합니다. –

5

시도 뭔가 : 소스 이미지가 대상 이미지 크기보다 작은

 Bitmap sourceImage = ...; 

     int targetWidth = 200; 
     int targetHeight = 200; 

     int x = sourceImage.Width/2 - targetWidth/2; 
     int y = sourceImage.Height/2 - targetHeight/2; 

     Rectangle cropArea = 
      new Rectangle(x, y, targetWidth, targetHeight); 

     Bitmap targetImage = 
      sourceImage.Clone(cropArea, sourceImage.PixelFormat); 

경우이 분명히 실패 할 것이다,하지만 당신은 아이디어를 얻을. 필요한 경우

1

이미지를 저장합니다이 방법은 중앙에 잘립니다 :

bool SaveCroppedImage(Image image, int targetWidth, int targetHeight, string filePath) 
{ 
    ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/jpeg").First(); 
    Image finalImage = image; 
    System.Drawing.Bitmap bitmap = null; 
    try 
    { 
     int left = 0; 
     int top = 0; 
     int srcWidth = targetWidth; 
     int srcHeight = targetHeight; 
     bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight); 
     double croppedHeightToWidth = (double)targetHeight/targetWidth; 
     double croppedWidthToHeight = (double)targetWidth/targetHeight; 

     if (image.Width > image.Height) 
     { 
      srcWidth = (int)(Math.Round(image.Height * croppedWidthToHeight)); 
      if (srcWidth < image.Width) 
      { 
       srcHeight = image.Height; 
       left = (image.Width - srcWidth)/2; 
      } 
      else 
      { 
       srcHeight = (int)Math.Round(image.Height * ((double)image.Width/srcWidth)); 
       srcWidth = image.Width; 
       top = (image.Height - srcHeight)/2; 
      } 
     } 
     else 
     { 
      srcHeight = (int)(Math.Round(image.Width * croppedHeightToWidth)); 
      if (srcHeight < image.Height) 
      { 
       srcWidth = image.Width; 
       top = (image.Height - srcHeight)/2; 
      } 
      else 
      { 
       srcWidth = (int)Math.Round(image.Width * ((double)image.Height/srcHeight)); 
       srcHeight = image.Height; 
       left = (image.Width - srcWidth)/2; 
      } 
     } 
     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      g.SmoothingMode = SmoothingMode.HighQuality; 
      g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      g.CompositingQuality = CompositingQuality.HighQuality; 
      g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(left, top, srcWidth, srcHeight), GraphicsUnit.Pixel); 
     } 
     finalImage = bitmap; 
    } 
    catch { } 
    try 
    { 
     using (EncoderParameters encParams = new EncoderParameters(1)) 
     { 
      encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)100); 
      //quality should be in the range [0..100] .. 100 for max, 0 for min (0 best compression) 
      finalImage.Save(filePath, jpgInfo, encParams); 
      return true; 
     } 
    } 
    catch { } 
    if (bitmap != null) 
    { 
     bitmap.Dispose(); 
    } 
    return false; 
} 
관련 문제