Skip to main content
 首页 » 编程设计

c#之在 C# 中缩放图像的好方法是什么

2024年02月24日28pengyingh

我有一个包含很多项目的网站,每个项目都包含一个侧边栏。

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

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

我的问题是画廊顶部的大图。上传的图片有多种尺寸,而我的支架的宽度为 239,高度为 179。缩放图片以正确显示给网站访问者的最佳方式是什么?

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

请您参考如下方法:

您可以调整图像大小以保持纵横比并重新保存。或者,如果您想保持相同的图像,那么您可以找到新的高度和宽度,保持纵横比,并将其相应地应用于 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?