图片通过文件流方式生成缩略图
/// <summary>
/// 按模板的方式生成缩略图(以流的方式获取文件)
/// 生成缩略图函数
/// </summary>
/// <param name="fromFileStream">源图文件流</param>
/// <param name="fileSaveUrl">缩略图存放地址</param>
/// <param name="templateWidth">模板宽</param>
/// <param name="templateHeight">模板高</param>
public void MakeSmallImg(Stream fromFileStream, string fileSaveUrl, Double templateWidth, Double templateHeight)
{
//从文件取得图片对象,并使用流中嵌入颜色管理信息
System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
Double newWidth = myImage.Width;
Double newHeight = myImage.Height;
if (newWidth > newHeight || newWidth == newHeight)
{
if (newWidth > templateWidth)
{
newWidth = templateWidth;
newHeight = newHeight * (newWidth / myImage.Width);
}
}
else
{
if (newHeight > templateHeight)
{
newHeight = templateHeight;
newWidth = newHeight * (newHeight / myImage.Height);
}
}
System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.White);
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
//调用方法
protected void btnImg_Click(object sender, EventArgs e)
{
string path = Server.MapPath(@"image\12890012151.jpg");
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
MakeSmallImg(fs, Server.MapPath(@"image\12890012151a.jpg"), 80, 80);
}

浙公网安备 33010602011771号