在asp中显示小图像目录

本文关键字:图像 显示 asp | 更新日期: 2023-09-27 18:00:54

嗨,我想把产品放在目录中,如何将图片裁剪成小图片并保存小图片和原始图片

在asp中显示小图像目录

试试看这里:

带有GetThumbnailImage 的C#缩略图

或此处:

将位图转换为缩略图

创建缩略图并缩小图像大小

创建缩略图

更新

例如,您可以将图像保存到文件夹中,然后可以执行以下操作:

var filename = "sourceImage.png";
using(var image = Image.FromFile(filename))
{
    using(var thumbnail = image.GetThumbnailImage(20/*width*/, 40/*height*/, null, IntPtr.Zero))
    {
        thumbnail.Save("thumb.png");
    }
}

更新

要按比例调整大小,请尝试以下操作:

public Bitmap ProportionallyResizeBitmap (Bitmap src, int maxWidth, int maxHeight)
{
// original dimensions
int w = src.Width;
int h = src.Height;
// Longest and shortest dimension
int longestDimension = (w>h)?w: h;
int shortestDimension = (w<h)?w: h;
// propotionality
float factor = ((float)longestDimension) / shortestDimension;
// default width is greater than height
double newWidth = maxWidth;
double newHeight = maxWidth/factor;
// if height greater than width recalculate
if ( w < h )
{
    newWidth = maxHeight / factor;
    newHeight = maxHeight;
}
// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using ( Graphics g = Graphics.FromImage((System.Drawing.Image)result) )
g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
return result;
}