C#:如何使用最大高度按比例调整图像大小

时间:2012-04-26 07:29:39

标签: c# asp.net image-resizing aspect-ratio

我需要在不改变宽高比的情况下按比例调整图像大小。我有使用固定高度和宽度调整大小的代码,但我需要按照最大高度(比如600像素)按比例调整图像大小。如何修改代码以满足我的要求?

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

请提供宝贵的意见。

4 个答案:

答案 0 :(得分: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; 
} 

答案 1 :(得分: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;
}

答案 2 :(得分:1)

好吧,想一想这个过程: 如果您的图像尺寸为800 x 600并想要将其调整为newWidth x 400高度(再加上相应的newWidth),则通过将newHeight(在您的情况下为maxHeight)除以600并乘以800得到比率用这个比例,对吧?

因此,在这种情况下,您需要将maxWidth和maxHeight更改为可选参数(例如800乘600),以给自己一些动力,并获得以下内容:

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等变量。

答案 3 :(得分: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);