asp.net 将JPG格式的图像生成一定宽高的缩略图方法
2012-08-22 09:47
549 查看
JPG图片在上传之后宽度和高度往往并不是自己想要,这个时候我们在上传之后要对这张
图片进行一定宽度和高度的缩放,将JPG格式的图像生成一定宽高的缩略图方法如下:
添加引用:
using System.Drawing;
using System.Drawing.Drawing2D;
调用方法:
CreateImage(Server.MapPath("~/a.jpg"), Server.MapPath("~/a_160_120.jpg"), 160, 120);
方法如下:
/// <summary>创建规定大小的图像
/// 源图像只能是JPG格式
/// </summary>
/// <param name="oPath">源图像绝对路径</param>
/// <param name="tPath">生成图像绝对路径</param>
/// <param name="width">生成图像的宽度</param>
/// <param name="height">生成图像的高度</param>
public static void CreateImage(string oPath, string tPath, int width, int height)
{
Bitmap originalBmp = new Bitmap(oPath);
// 源图像在新图像中的位置
int left, top;
if (originalBmp.Width <= width && originalBmp.Height <= height)
{
// 原图像的宽度和高度都小于生成的图片大小
left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);
// 最终生成的图像
Bitmap bmpOut = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bmpOut))
{
// 设置高质量插值法
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充
graphics.Clear(Color.White);
// 把源图画到新的画布上
graphics.DrawImage(originalBmp, left, top);
}
bmpOut.Save(tPath);
bmpOut.Dispose();
originalBmp.Dispose();
return;
}
// 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,
// 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上
int newWidth, newHeight;
if (width * originalBmp.Height < height * originalBmp.Width)
{
newWidth = width;
newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
// 缩放成宽度跟预定义的宽度相同的,即left=0,计算top
left = 0;
top = (int)Math.Round((decimal)(height - newHeight) / 2);
}
else
{
newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
newHeight = height;
// 缩放成高度跟预定义的高度相同的,即top=0,计算left
left = (int)Math.Round((decimal)(width - newWidth) / 2);
top = 0;
}
// 生成按比例缩放的图,如:160*80的图
Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(bmpOut2))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
}
// 再把该图画到预先定义的宽高的画布上,如160*120
Bitmap lastbmp = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(lastbmp))
{
// 设置高质量插值法
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充
graphics.Clear(Color.White);
// 把源图画到新的画布上
graphics.DrawImage(bmpOut2, left, top);
}
lastbmp.Save(tPath);
lastbmp.Dispose();
bmpOut2.Dispose();
originalBmp.Dispose();
}
图片进行一定宽度和高度的缩放,将JPG格式的图像生成一定宽高的缩略图方法如下:
添加引用:
using System.Drawing;
using System.Drawing.Drawing2D;
调用方法:
CreateImage(Server.MapPath("~/a.jpg"), Server.MapPath("~/a_160_120.jpg"), 160, 120);
方法如下:
/// <summary>创建规定大小的图像
/// 源图像只能是JPG格式
/// </summary>
/// <param name="oPath">源图像绝对路径</param>
/// <param name="tPath">生成图像绝对路径</param>
/// <param name="width">生成图像的宽度</param>
/// <param name="height">生成图像的高度</param>
public static void CreateImage(string oPath, string tPath, int width, int height)
{
Bitmap originalBmp = new Bitmap(oPath);
// 源图像在新图像中的位置
int left, top;
if (originalBmp.Width <= width && originalBmp.Height <= height)
{
// 原图像的宽度和高度都小于生成的图片大小
left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);
// 最终生成的图像
Bitmap bmpOut = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bmpOut))
{
// 设置高质量插值法
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充
graphics.Clear(Color.White);
// 把源图画到新的画布上
graphics.DrawImage(originalBmp, left, top);
}
bmpOut.Save(tPath);
bmpOut.Dispose();
originalBmp.Dispose();
return;
}
// 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,
// 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上
int newWidth, newHeight;
if (width * originalBmp.Height < height * originalBmp.Width)
{
newWidth = width;
newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
// 缩放成宽度跟预定义的宽度相同的,即left=0,计算top
left = 0;
top = (int)Math.Round((decimal)(height - newHeight) / 2);
}
else
{
newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
newHeight = height;
// 缩放成高度跟预定义的高度相同的,即top=0,计算left
left = (int)Math.Round((decimal)(width - newWidth) / 2);
top = 0;
}
// 生成按比例缩放的图,如:160*80的图
Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(bmpOut2))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
}
// 再把该图画到预先定义的宽高的画布上,如160*120
Bitmap lastbmp = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(lastbmp))
{
// 设置高质量插值法
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充
graphics.Clear(Color.White);
// 把源图画到新的画布上
graphics.DrawImage(bmpOut2, left, top);
}
lastbmp.Save(tPath);
lastbmp.Dispose();
bmpOut2.Dispose();
originalBmp.Dispose();
}
相关文章推荐
- ASP.NET实现根据URL生成网页缩略图的方法
- asp.net生成缩略图示例方法分享
- ASP.NET实现上传图片并生成缩略图的方法
- 【转】 asp.net从视频文件中抓取一桢并生成图像文件的方法 实现多语言本地化应用程序 自动返回上次请求页面
- ASP.NET实现上传图片并生成缩略图的方法
- ASP.NET中生成缩略图的方法
- ASP.NET实现上传图片并生成缩略图的方法
- asp.net实现生成缩略图及给原始图加水印的方法示例
- ASP.NET实现根据URL生成网页缩略图的方法
- 相册用缩略图生成类(C#,ASP.NET),自适应图像比例。
- asp.net 等比例生成上传图片的高质量缩略图的方法
- asp.net生成缩略图示例方法分享
- ASP.NET实现根据URL生成网页缩略图的方法
- 在ASP.NET中上传图片并生成缩略图
- Asp.net 2.0 中将网站首页生成静态页的一个比较好的方法
- Asp.net生成htm静态文件的源代码 2种方法
- ASP.NET生成二维码的方法总结
- ASP.NET中生成Excel遇到的问题及改进方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(4):异步通讯层生成的客户端代理类、使用HTTP GET进行调用