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

发布于 2024-08-25 00:57:35 字数 755 浏览 2 评论 0原文

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

在此侧栏中,可以将图像附加到项目中。附加的图像将显示在一个画廊中,底部有 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);

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. 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?

Thanks Zapping (this code is usable for me):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-09-01 00:57:36

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

您可以使用这些来调整图像大小或者找到高度和宽度。
http://snippets.dzone.com/posts/show/4336
http://www.switchonthecode.com/教程/csharp-tutorial-image-editing- saving-cropping-and-resizing

如果您决定保留较大尺寸的图像,您也可以尝试一些平移操作
如何使用 jquery 放大和缩小图像?

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.

You can use these to resize the image or find the height and width.
http://snippets.dzone.com/posts/show/4336
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?

关于从前 2024-09-01 00:57:36

比例缩放也可以用 CSS 来完成。考虑一张 100 像素宽 x 200 像素高的图像。

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

会将图像放入 50x50px 的框中,即生成 25x50px 的图像。类似地,

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

将生成 500x1000px 的图像。

在您的情况下,

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

这可能对您来说是一个很好的解决方案,因为您的图像很小,并且无论如何您都将加载缩略图和图像,也可能是相同的图像,节省带宽并利用缓存。

Proportional scaling can also be done with CSS. Consider an image 100px wide by 200px high.

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

will fit the image into a box 50x50px i.e. produce an image 25x50px. Analogously,

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

will produce and image 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文