简体   繁体   English

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

[英]What would be a good way to scale images in C#?

I got a website which contains a lot of projects with each project containing a sidebar. 我有一个网站,其中包含很多项目,每个项目都包含一个侧边栏。

In this sidebar it is possible to attach images to a project. 在此侧栏中,可以将图像附加到项目中。 The images attached will be shown in a gallery with 3 small thumbs at the bottom and one bigger image at the top of the gallery. 附加的图像将显示在一个图库中,底部有3个小拇指,图库顶部有一个较大的图像。 The big image will refresh to another image when a visitor clicks on the small thumb @ the bottom of the gallery. 当访问者点击图库底部的小拇指时,大图像将刷新为另一个图像。

The thumbs are no problem, they are shown correctly. 拇指没问题,它们显示正确。

My problem is the bigger image at the top of the gallery. 我的问题是画廊顶部的图像更大。 The images that get uploaded have a big variety of sizes, while my holder has a width of 239 and height of 179. What would be the best way to scale the images so that they are shwon correctly to the visitors of the website? 上传的图片有各种各样的尺寸,而我的版块宽度为239,高度为179.什么是缩放图像以便正确显示网站访问者的最佳方法?

Thanks Zapping (this code is usable for me): 谢谢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);

You can resize the image maintaining the aspect ratio and resave it. 您可以保持纵横比调整图像大小并重新保存。 Or if you want to maintain the same image then you could find out new height and width, maintaining the aspect ratio, and apply it accordingly to the height and width properties of the img tag. 或者,如果要保持相同的图像,则可以找到新的高度和宽度,保持纵横比,并相应地应用于img标签的高度和宽度属性。

You can use these to resize the image or find the height and width. 您可以使用它们来调整图像大小或查找高度和宽度。
http://snippets.dzone.com/posts/show/4336 http://snippets.dzone.com/posts/show/4336
http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

you can try out something on panning as well in case you decide to keep the larger sized images 如果您决定保留较大尺寸的图像,也可以尝试平移
How to zoom in and zoom out image using jquery? 如何使用jquery放大和缩小图像?

Proportional scaling can also be done with CSS. 比例缩放也可以使用CSS完成。 Consider an image 100px wide by 200px high. 考虑100px宽,200px高的图像。

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

will fit the image into a box 50x50px ie produce an image 25x50px. 将图像放入50x50px的框中,即生成25x50px的图像。 Analogously, 类似地,

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

will produce and image 500x1000px. 将生产和成像500x1000px。

In your case 在你的情况下

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

This might be a good solution for you since your images are small and you're going to load the thumbnail and the image anyway, might as well be the same image, save bandwidth and make use of the caches. 这可能是一个很好的解决方案,因为你的图像很小,无论如何你要加载缩略图和图像,也可能是相同的图像,节省带宽和使用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#Winforms应用程序中从命令提示符收集数据的好方法是什么? - What would be a good way to collect data from the command prompt in a C# Winforms Application? 什么是C#中C ++向量的良好替代品? - What would be a good replacement for C++ vector in C#? 这将是什么设计模式,这是一个好主意吗? (C#) - What design pattern would this be, and is it a good idea? (C#) 对于C#,是否有相当于Java的ToStringBuilder?什么是好的C#版本功能? - Is there an equivalent to Java's ToStringBuilder for C#? What would a good C# version feature? 在 c# 中放大条形码图像的最佳无损方式是什么 - What is the best lossless way to scale up a barcode image in c# 在C#中编写批处理脚本的好方法是什么? - What's a good way to write batch scripts in C#? 在C#项目中存储不变变量的好方法是什么? - What's a good way to store unchanging variables in a C# project? 什么是处理从MySQL和C#返回的数据的好方法? - What is a good way to handle returned data from MySQL and C#? 在C#中捕获StackOverflow异常的一般方法是什么? - What's a good general way of catching a StackOverflow exception in C#? 在c#中的文件夹中查找最新创建的文件的好方法是什么? - What is a good way to find the latest created file in a folder in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM