user735647
user735647

Reputation: 395

C# : How to resize image proportionately with max height

I need to resize my image proportionately without changing aspect ratio.I have the code to resize with fixed hight and width but I need to resize image proportionately with max height(say 600 pixels). How can I modify the code to suit my requirement?

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;
}

Please provide your valuable thoughts.

Upvotes: 4

Views: 15874

Answers (4)

Zk Wave
Zk Wave

Reputation: 1

100% Worked

   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);

Upvotes: 0

army
army

Reputation: 545

Well, thinking through the process: if you have an image that 800 x 600 in size and want to resize it to newWidth x 400 height (plus whatever the respective newWidth will be), you get the ratio by dividing the newHeight (maxHeight in your case) with 600 and multiply 800 with this ratio, right?

So, in this case you need to change maxWidth and maxHeight to optional parameters (say 800 by 600) to give yourself some dynamism and get the following:

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;
}

I hope this helps. I didn't test it, but I've rewritten my VB code, so allegedly it should be okay...

There's also a ResizeStream method here: http://forums.asp.net/t/1576697.aspx/1 that you might find useful. If you want to keep image quality, you can use the CompositingQuality and SmoothingMode, etc. variables.

Upvotes: 1

gwiazdorrr
gwiazdorrr

Reputation: 6329

This almost feels to easy and I feel I'm missing something. Anyway, won't that do the trick?

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; 
} 

Upvotes: 4

Imran Rizvi
Imran Rizvi

Reputation: 7438

Use the following function

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;
}

Upvotes: 1

Related Questions