代码之家  ›  专栏  ›  技术社区  ›  Younes

用C#缩放图像的好方法是什么?

  •  1
  • Younes  · 技术社区  · 14 年前

    在此侧边栏中,可以将图像附加到项目。所附图片将显示在一个画廊与3个小拇指在底部和一个更大的图像在画廊的顶部。当访问者点击画廊底部的小拇指时,大图像将刷新为另一个图像。

    拇指没有问题,它们显示正确。

    感谢Zapping(此代码对我有用):

    int sourceWidth = imgToResize.Width;
       int sourceHeight = imgToResize.Height;
    
       float nPercent = 0;
       float nPercentW = 0;
       float nPercentH = 0;
    
       nPercentW = ((float)size.Width / (float)sourceWidth);
       nPercentH = ((float)size.Height / (float)sourceHeight);
    
       if (nPercentH < nPercentW)
          nPercent = nPercentH;
       else
          nPercent = nPercentW;
    
       int destWidth = (int)(sourceWidth * nPercent);
       int destHeight = (int)(sourceHeight * nPercent);
    
    3 回复  |  直到 14 年前
        1
  •  2
  •   Community kfsone    7 年前

    可以调整图像的大小以保持纵横比并重新保存。或者,如果您想保持相同的图像,那么您可以找到新的高度和宽度,保持纵横比,并将其相应地应用于img标记的高度和宽度属性。


    http://snippets.dzone.com/posts/show/4336
    http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

    如果你决定保留较大尺寸的图像,你也可以尝试一些平移的方法
    How to zoom in and zoom out image using jquery?

        2
  •  2
  •   A G    14 年前
        3
  •  0
  •   Martin    14 年前

    <img src="image.png" style="max-width:50;max-height:50;">
    

    <img src="image.png" style="min-width:500;min-height:500;">
    

    将产生和图像500x1000像素。

    对你来说

    <img src="image.png" style="max-width:239;max-height:179;">
    

    这可能是一个很好的解决方案,因为你的图像很小,你要加载缩略图和图像无论如何,可能是同一个图像,节省带宽和利用缓存。