在asp中显示小图像目录

时间:2011-06-30 21:12:16

标签: c# asp.net

您好我想将产品放到目录中,如何将图片裁剪为小图并保存小图和原图?

1 个答案:

答案 0 :(得分:0)

试试看:

C# Thumbnail Image With GetThumbnailImage

或在这里:

Convert Bitmap to Thumbnail

Create thumbnail and reduce image size

Create thumbnail image

<强>更新

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

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