zoukankan      html  css  js  c++  java
  • c# 生成微缩图

    ‍///<summary>
    /// 图片微缩图处理
    ///</summary>
    ///<param name="srcPath">源图片</param>
    ///<param name="destPath">目标图片</param>
    ///<param name="width">宽度</param>
    ///<param name="height">高度</param>
    publicstaticvoid CreateThumbnailPicture(string srcPath, string destPath, int width, int height)
    {
    //根据图片的磁盘绝对路径获取 源图片 的Image对象
    System.Drawing.Image img = System.Drawing.Image.FromFile(srcPath);

    //bmp: 最终要建立的 微缩图 位图对象。
    Bitmap bmp =new Bitmap(width, height);

    //g: 绘制 bmp Graphics 对象
    Graphics g = Graphics.FromImage(bmp);
    g.Clear(Color.Transparent);
    //为Graphics g 对象 初始化必要参数,很容易理解。
    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

    //源图片宽和高
    int imgWidth = img.Width;
    int imgHeight = img.Height;

    //绘制微缩图
    g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight)
    , GraphicsUnit.Pixel);

    ImageFormat format = img.RawFormat;
    ImageCodecInfo info = ImageCodecInfo.GetImageEncoders().SingleOrDefault(i => i.FormatID == format.Guid);
    EncoderParameter param =new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
    EncoderParameters parameters =new EncoderParameters(1);
    parameters.Param[0] = param;
    img.Dispose();

    //保存已生成微缩图,这里将GIF格式转化成png格式。
    if (format == ImageFormat.Gif)
    {
    destPath = destPath.ToLower().Replace(".gif", ".png");
    bmp.Save(destPath, ImageFormat.Png);
    }
    else
    {
    if (info !=null)
    {
    bmp.Save(destPath, info, parameters);
    }
    else
    {

    bmp.Save(destPath, format);
    }
    }

    img.Dispose();
    g.Dispose();
    bmp.Dispose();
    }

    调用

    /上传成功后网站内源图片相对路径
    2 string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath
    3+string.Format(@"Content/Upload/Images/{0}", fileName);
    4
    5/*
    6比例处理
    7微缩图高度(DefaultHeight属性值为 400)
    8*/
    9System.Drawing.Image img = System.Drawing.Image.FromFile(toFile);
    10int width = img.Width;
    11int height = img.Height;
    12float ratio = (float)width / height;
    13
    14//微缩图高度和宽度
    15 int newHeight = height <= DefaultHeight ? height : DefaultHeight;
    16int newWidth = height <= DefaultHeight ? width : Convert.ToInt32(DefaultHeight * ratio);
    17
    18FileInfo generatedfile =new FileInfo(toFile);
    19string newFileName ="Thumb_"+ generatedfile.Name;
    20string newFilePath = Path.Combine(generatedfile.DirectoryName, newFileName);
    21
    22PictureHandler.CreateThumbnailPicture(toFile, newFilePath, newWidth, newHeight);
    23
    24string thumbRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath
    25+string.Format(@"/Content/Upload/Images/{0}", newFileName);
    26
    27//返回原图和微缩图的网站相对路径
    28 relativePath =string.Format("{0},{1}", relativePath, thumbRelativePath);
    29
    30return relativePath;

  • 相关阅读:
    安装ArcGIS Desktop 9.3后,Windows 7的“打开或关闭Windows功能”对话框成了一片空白
    ubuntu安装锐捷客户端
    jsp中文乱码的问题
    C#操作MySQL时,出现的中文乱码的解决方案
    Jena读取Restriction
    MySQL导入Access数据
    No identifier specified for entity: main.java.com.sy.entity.User
    基于注解的Spirng MVC框架的搭建(基础篇)
    解决jQuery版本冲突的方法
    Sybase数据库的连接,JNDI,Hibernate配置
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175751.html
Copyright © 2011-2022 走看看