我在 JS Carousel 中显示图像。这些图像非常大,所以我在 HTML 中缩小了它们。我做了一些数学运算来保持纵横比。最后,尽管这些大图像质量下降。应该注意这些图像已经存在于服务器中,我不应该更改原始文件的大小。
是什么导致质量下降?我应该坚持使用较小尺寸的图像,增加将显示的最终尺寸吗?这是帮助的代码,最大宽度和高度目前分别为 270 和 180。所有这些都是 C#。
// srcccccc is the image file. Can be any kind of extension jpg, png, etc.
if (srcccccc != "")
{
Globals.NavigateURL();
string path = PortalSettings.HomeDirectory + srcccccc;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(@Server.MapPath(path)))
{
int[] newSizes = ResizeWithRatio(img);
contentsall += "<img src=\"" + PortalSettings.HomeDirectory + srcccccc + "\" alt=\"Image "
+ folderidddin + "\" height=\"" + newSizes[1] + "px\"" + "\" width=\"" + newSizes[0] + "px\" />";
}
}
// HTML is dynamically added later....
private int[] ResizeWithRatio(System.Drawing.Image img)
{
int[] sizes = new int[2];
if (img.Width > maxWidth || img.Height > maxHeight)
{
float ri = (float)img.Width / (float)img.Height;
float rs = (float)maxWidth / (float)maxHeight;
if (rs > ri)
{
sizes[0] = (int)((float)img.Width * (float)maxHeight / (float)img.Height);
sizes[1] = maxHeight;
}
else
{
sizes[0] = maxWidth;
sizes[1] = (int)((float)img.Height * (float)maxWidth / (float)img.Width);
}
}
else
{
sizes[0] = img.Width;
sizes[1] = img.Height;
}
return sizes;
}