zoukankan      html  css  js  c++  java
  • C# 生成缩略图方法

      private static string CreateThumbnail(string filepath, int tWidth, int tHeight)
            {
                if (string.IsNullOrEmpty(filepath))
                {
                    return "";
                }
                string paramOriginalDirectory = "thumbnail";
                string oldfilepath = filepath;
                //string originalImagePath = HttpContext.Current.Server.MapPath(filepath);//全路径(即绝对路径)
                string originalImagePath = filepath;//全路径(即绝对路径)
                //originalImagePath = ApiToWeb(originalImagePath);
                string filename = System.IO.Path.GetFileName(originalImagePath);
                string oldthumbnailImagePath = oldfilepath.Replace(filename, "") + paramOriginalDirectory + "/" + filename;
                string thumbnailPath = originalImagePath.Replace(filename, "") + paramOriginalDirectory;
                string thumbnailImagePath = originalImagePath.Replace(filename, "") + paramOriginalDirectory + "/" + filename;
                if (!Directory.Exists(thumbnailPath))
                {
                    Directory.CreateDirectory(thumbnailPath);
                }
    
                if (!File.Exists(originalImagePath))
                {
                    return oldthumbnailImagePath;
                }
    
                if (File.Exists(thumbnailImagePath))
                {
                    return oldthumbnailImagePath;
                }
    
                try
                {
                    FileStream fileStream = File.OpenRead(originalImagePath);
                    Int32 filelength = 0;
                    filelength = (int)fileStream.Length;
                    Byte[] image = new Byte[filelength];
                    fileStream.Read(image, 0, filelength);
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(fileStream);
                    fileStream.Close();
                    int oWidth = oImage.Width;//原图宽度
                    int oHeight = oImage.Height;//原图高度
                    if (tWidth == 0)
                    {
                        tWidth = 100;
                    }
                    if (tHeight == 0)
                    {
                        tHeight = 100;
                    }
                    //按比例计算出缩略图的宽度和高度
                    if (oWidth >= oHeight)
                    {
                        tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oHeight)));
                    }
                    else
                    {
                        tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                    }
                    //生成缩略图
                    Bitmap tImage = new Bitmap(tWidth, tHeight);
                    Graphics g = Graphics.FromImage(tImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    g.Clear(Color.Transparent);//清空画布并以透明背景色填充
                    g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
                    //string oPath = originalImagePath + "\" + filename + ".jpg";
                    //string thumbnailImagePath = thumbnailPath + "\" + filename + ".jpg";
                    tImage.Save(thumbnailImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //释放资源                    
                    oImage.Dispose();
                    g.Dispose();
                    tImage.Dispose();
                    return oldthumbnailImagePath;
                }
                catch (Exception ex)
                {
                    return ex.Message + ex.StackTrace;
                }
            }
  • 相关阅读:
    LeetCode Merge Two Sorted Lists 归并排序
    LeetCode Add Binary 两个二进制数相加
    LeetCode Climbing Stairs 爬楼梯
    034 Search for a Range 搜索范围
    033 Search in Rotated Sorted Array 搜索旋转排序数组
    032 Longest Valid Parentheses 最长有效括号
    031 Next Permutation 下一个排列
    030 Substring with Concatenation of All Words 与所有单词相关联的字串
    029 Divide Two Integers 两数相除
    028 Implement strStr() 实现 strStr()
  • 原文地址:https://www.cnblogs.com/mojiejushi/p/13489059.html
Copyright © 2011-2022 走看看