[英]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放大和缩小图像?
All about resizing. 所有关于调整大小。 http://www.codeproject.com/KB/GDI-plus/imageresize.aspx http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
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.