2009-12-30 7 views
7

이미지 크기를 조정하고 싶지만 이미지가 비뚤어져 보이지 않게하고 싶습니다.이미지 크기를 조절하면서 비율을 유지합니다.

이미지는 115x115 (길이 x 너비) 여야합니다.

이미지의 가로 길이는 115 픽셀을 초과 할 수 없지만 필요한 경우 너비는 115보다 작을 수 있지만 그 이상은 될 수 없습니다.

까다로운가요?

+0

이 http://stackoverflow.com/questions/2823200/과 비슷한 것 같다 브리지 (Brij)의 답변에 따라 –

답변

2

이미지를 확장하고 화면 비율 유지하기 위해 찾고 :

float MaxRatio = MaxWidth/(float) MaxHeight; 
float ImgRatio = source.Width/(float) source.Height; 

if (source.Width > MaxWidth) 
return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth/
ImgRatio, 0))); 

if (source.Height > MaxHeight) 
return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio, 
0), MaxHeight)); 

return source; 

당신을 도움이 될 것입니다, 그리고 당신이 아이디어에 관심이 있다면 : Wikpedia article on Image Aspect Ratio

+0

이 코드를 사용해 보았을 때 (같은 기본 문제를 검색하는 동안 다른 곳에서 찾았습니다.) 너비와 높이가 최대 값보다 큰 이미지로 시작하면 실제로 작업을 수행하지 않습니다. 원본 이미지의 어느 차원이 가장 큰지에 따라 첫 번째 또는 두 번째 배율을 적용합니다. 그렇지 않으면 두 배 중 하나가 배율 조정에서 허용되는 최대 배율보다 커집니다. – Moo

0

Bertrands blog post에서보세요 GDI 및 WPF를 사용하여 이미지를 크기 조정하는 방법에 대해 설명합니다.

5

당신은 가로 세로 비율을 유지해야합니다 코드에서

float scale = 0.0; 

    if (newWidth > maxWidth || newHeight > maxHeight) 
    { 
     if (maxWidth/newWidth < maxHeight/newHeight) 
     { 
      scale = maxWidth/newWidth; 
     } 
     else 
     { 
      scale = maxHeight/newHeight; 
     } 
     newWidth = newWidth*scale; 
     newHeight = newHeight*scale; 

    } 

, 처음 newWidth/newHeight 이미지의 너비/높이 있습니다.

/// <summary> 
/// Resize image to max dimensions 
/// </summary> 
/// <param name="img">Current Image</param> 
/// <param name="maxWidth">Max width</param> 
/// <param name="maxHeight">Max height</param> 
/// <returns>Scaled image</returns> 
public static Image Scale(this Image img, int maxWidth, int maxHeight) 
{ 
    double scale = 1; 

    if (img.Width > maxWidth || img.Height > maxHeight) 
    { 
     double scaleW, scaleH; 

     scaleW = maxWidth/(double)img.Width; 
     scaleH = maxHeight/(double)img.Height; 

     scale = scaleW < scaleH ? scaleW : scaleH; 
    } 

    return img.Resize((int)(img.Width * scale), (int)(img.Height * scale)); 
} 

/// <summary> 
/// Resize image to max dimensions 
/// </summary> 
/// <param name="img">Current Image</param> 
/// <param name="maxDimensions">Max image size</param> 
/// <returns>Scaled image</returns> 
public static Image Scale(this Image img, Size maxDimensions) 
{ 
    return img.Scale(maxDimensions.Width, maxDimensions.Height); 
} 

크기 조정 방법 :

/// <summary> 
/// Resize the image to the given Size 
/// </summary> 
/// <param name="img">Current Image</param> 
/// <param name="width">Width size</param> 
/// <param name="height">Height size</param> 
/// <returns>Resized Image</returns> 
public static Image Resize(this Image img, int width, int height) 
{ 
    return img.GetThumbnailImage(width, height, null, IntPtr.Zero); 
} 
+0

newHeight 또는 newWidth가 정수인 경우 제대로 작동하지 않으므로'float'으로 캐스트해야합니다. – BrunoLM

4

나는이 확장 방법을했다. 그 질문에 대한 나의 대답을 보라.
+0

대신 img.Height, img.Source.Height가 작동했습니다 (VS 2010 .net 4). 또한 소스가 너비로 사용되었습니다. – mnemonic

관련 문제