2012-04-26 116 views
3

我需要調整我的圖像的比例,而不改變縱橫比。我有代碼來調整高度和寬度的固定大小,但我需要調整圖像的比例與最大高度(比如600像素)。我如何修改代碼以符合我的要求?C#:如何按比例調整圖像的最大高度

public static void Main() 
{ 
    var image = Image.FromFile(@"c:\logo.png"); 
    var newImage = ScaleImage(image, 300, 400); 
    newImage.Save(@"c:\test.png", ImageFormat.Png); 
} 

public static Image ScaleImage(Image image, int maxWidth, int maxHeight) 
{ 
    var ratioX = (double)maxWidth/image.Width; 
    var ratioY = (double)maxHeight/image.Height; 
    var ratio = Math.Min(ratioX, ratioY); 

    var newWidth = (int)(image.Width * ratio); 
    var newHeight = (int)(image.Height * ratio); 

    var newImage = new Bitmap(newWidth, newHeight); 
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 
    return newImage; 
} 

請提供您的寶貴意見。

+0

什麼是與此代碼的問題? – Likurg 2012-04-26 07:33:52

+0

尊重,請按照我的要求。此代碼使用最大高度和最大寬度,而我需要按比例更改圖像尺寸,使高度保持最大值(例如600)。 – user735647 2012-04-26 07:38:57

+0

我不知道問題是什麼,但如果我沒有弄錯,代碼會在不改變寬高比的情況下調整圖像大小。你可以添加一個你期望發生的例子嗎? – 2012-04-26 07:40:12

回答

4

這幾乎感覺易,我覺得我失去了一些東西。無論如何,這不是訣竅嗎?

public static Image ScaleImage(Image image, int maxHeight) 
{ 
    var ratio = (double)maxHeight/image.Height; 

    var newWidth = (int)(image.Width * ratio); 
    var newHeight = (int)(image.Height * ratio); 

    var newImage = new Bitmap(newWidth, newHeight); 
    using (var g = Graphics.FromImage(newImage)) 
    { 
     g.DrawImage(image, 0, 0, newWidth, newHeight); 
    } 
    return newImage; 
} 
+0

非常感謝。讓我試試看。 – user735647 2012-04-26 07:58:49

+0

我需要添加的內容是爲了防止縮放適合最大高度或最大寬度的圖像,並且其中一個邊更大,只需將大小縮小至適合所需區域,同時保持色調。 – 2016-07-26 21:27:10

1

使用下面的函數

public Bitmap ProportionallyResizeBitmapByHeight(Bitmap imgToResize, int height) 
{ 
    int sourceWidth = imgToResize.Width; 
    int sourceHeight = imgToResize.Height; 

    float scale = 0; 

    scale = (height/(float)sourceHeight); 

    int destWidth = (int)(sourceWidth * scale); 
    int destHeight = (int)(sourceHeight * scale); 

    Bitmap result = new Bitmap(destWidth, destHeight); 
    result.SetResolution(imgToResize.HorizontalResolution, imgToResize.VerticalResolution); 
    Graphics g = Graphics.FromImage(result); 
    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); 
    g.Dispose(); 

    return result; 
} 
+1

他得到了相同的代碼 – Likurg 2012-04-26 07:43:58

+0

@Imran非常感謝。讓我試試看。 – user735647 2012-04-26 07:58:18

+0

我可以使用給定的代碼來調整圖像大小 – 2012-04-26 08:41:22

1

好了,整個過程在想: ,如果你有一個形象,800大小x 600,並希望將其調整到newWidth×400高(加上任何相應的newWidth會),你得到的比通過將newHeight(在你的情況下是maxHeight)除以600,然後用這個比率乘以800,對吧?

因此,在這種情況下,你需要爲maxWidth和maxHeight改爲可選參數(比如600 800),以給自己一些動力,並得到如下:

public static Image ScaleImage(Image image, int maxWidth = 800, int maxHeight = 600) 
{ 

    int newWidth; 
    int newHeight; 
    double ratio = image.Height/image.Width; 

    if(maxHeight != 600) { 

    newWidth = image.Width * ratio; 
    newHeight = maxHeight; 

    } 

    Bitmap newImage = new Bitmap(newWidth, newHeight); 
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 
    return newImage; 
} 

我希望這有助於。我沒有測試它,但我已經重寫了我的VB代碼,所以據說它應該沒問題...

這裏還有一個ResizeStream方法:http://forums.asp.net/t/1576697.aspx/1,你可能會覺得有用。 如果你想保持圖像質量,你可以使用CompositingQuality和SmoothingMode等變量。

+0

非常感謝。讓我試試看。 – user735647 2012-04-26 08:07:51

+1

沒問題。我希望它有幫助。讓我知道你是否觸礁。另一件事:這是基於最大高度,所以你可能想要檢查一個else if(maxWidth!= 800),並相應地更改newWidth和newHeight。但是,我所包含的鏈接非常方便,無論如何。 – army 2012-04-26 08:16:52

0

100%曾爲

private static BitmapFrame CreateResizedImage(ImageSource source, int Max_width, int Max_height, int margin) 
{ 
    float scaleHeight = (float)Max_width/(float)source.Height; 
    float scaleWidth = (float)Max_height/(float)source.Width; 
    float scale = Math.Min(scaleHeight, scaleWidth); 

    int width = (int)(source.Width * scale); 
    int height = (int)(source.Height * scale); 


    var rect = new Rect(margin, margin, width - margin * 2, height - margin * 2); 

    var group = new DrawingGroup(); 
    RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality); 
    group.Children.Add(new ImageDrawing(source, rect)); 

    var drawingVisual = new DrawingVisual(); 
    using (var drawingContext = drawingVisual.RenderOpen()) 
     drawingContext.DrawDrawing(group); 

    var resizedImage = new RenderTargetBitmap(
     width, height,   // Resized dimensions 
     96, 96,    // Default DPI values 
     PixelFormats.Default); // Default pixel format 
    resizedImage.Render(drawingVisual); 

    return BitmapFrame.Create(resizedImage); 
} 


//--------Main------------// 

    BitmapImage imageSource = (BitmapImage)ImagePreview.Source; 
    var NewImage= CreateResizedImage(imageSource , 300, 300, 0); 
相關問題