c#图片生成缩略图,保持最高清的图片
来源:互联网 发布:淘宝付款流程 编辑:程序博客网 时间:2024/05/02 12:15
1.生成缩略图的主要代码
/// <summary> /// 图片显示成缩略图 /// </summary> /// <param name="sourcePath" >图片原路径</param> /// <param name="newPath">生成的缩略图新路径</param> /// <param name="width">生成的缩略图宽度</param> /// <param name="height">生成的缩略图高度</param> public static void MakeThumbnail(string sourcePath, string newPath, int width, int height) { sourcePath = System.Web.HttpContext.Current.Server.MapPath(sourcePath); newPath = System.Web.HttpContext.Current.Server.MapPath(newPath); System.Drawing.Image ig = System.Drawing.Image.FromFile(sourcePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = ig.Width; int oh = ig.Height; if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight) { oh = ig.Height; ow = ig.Height * towidth / toheight; y = 0; x = (ig.Width - ow) / 2; } else { ow = ig.Width; oh = ig.Width * height / towidth; x = 0; y = (ig.Height - oh) / 2; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 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(System.Drawing.Color.Transparent); g.DrawImage(ig, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { throw ex; } finally { ig.Dispose(); bitmap.Dispose(); g.Dispose(); } }
2.具体操作流程
1.` protected void Page_Load(object sender, EventArgs e)
{
if (File.Exists(Server.MapPath(“现有图片路径”.ToString())))//判断现有图片路径是否存在
{
S_ImageHelper.MakeThumbnail(“现有图片路径”.ToString(), “新建缩略图路径”, 275, 254);
}
Response.Write("成功");}`
3。显示成功,则缩略图生成好了,去看看你的新建路径里面是否又该图片吧
1 0
- c#图片生成缩略图,保持最高清的图片
- C# 生成图片缩略图
- C#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- c#生成图片缩略图的类
- Asp.Net 高清图片缩略图生成
- 生成图片的缩略图
- C#上传图片生成缩略图
- c#实现图片生成缩略图
- c#实现图片生成缩略图
- C#中图片切割,图片压缩,缩略图的生成
- C#中图片切割,图片压缩,缩略图的生成
- C#中图片切割,图片压缩,缩略图的生成
- C#图片切割、图片压缩、缩略图生成
- iOS8以后的屏幕宽高变化
- 大数据量下高并发同步的讲解
- STM32 入门教程 系统时钟 SysTick
- 从零搭建LNMP环境(一)
- linux学习记录
- c#图片生成缩略图,保持最高清的图片
- 欢迎使用CSDN-markdown编辑器
- 继承与BaseActivity的抽取
- 基于资源的HTTP Cache的实现介绍
- JAVA线程池shutdown和shutdownNow的区别
- 使用jquery获取url及url参数的方法
- 连续范围问题
- 线性求逆元
- iOS开发 -- 自定义UITabBarController,子控件不隐藏问题