• (转)图像处理类(生成缩略图)


       /// <summary>
    /// 图像处理类
    /// </summary>
    public static class ImageHandler
    {
    /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="originalImagePath">源图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param>
    private static void MakeThumbnail(Stream PicStream, string thumbnailPath, int width, int height, string mode, string PicExtension)
    {
    System.Drawing.Image originalImage = System.Drawing.Image.FromStream(PicStream);
    int towidth = width;
    int toheight = height;

    int x = 0;
    int y = 0;
    int ow = originalImage.Width;
    int oh = originalImage.Height;

    switch (mode)
    {
    case "HW"://指定高宽缩放(可能变形)
    break;
    case "W"://指定宽,高按比例
    toheight = originalImage.Height * width / originalImage.Width;
    break;
    case "H"://指定高,宽按比例
    towidth = originalImage.Width * height / originalImage.Height;
    break;
    case "Cut"://指定高宽裁减(不变形)
    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
    {
    oh = originalImage.Height;
    ow = originalImage.Height * towidth / toheight;
    y = 0;
    x = (originalImage.Width - ow) / 2;
    }
    else
    {
    ow = originalImage.Width;
    oh = originalImage.Width * height / towidth;
    x = 0;
    y = (originalImage.Height - oh) / 2;
    }
    break;
    default:
    break;
    }

    //新建一个bmp图片
    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(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
    new System.Drawing.Rectangle(x, y, ow, oh),
    System.Drawing.GraphicsUnit.Pixel);

    try
    {
    //根据原来的图片格式,保存为原图片格式
    switch (PicExtension.ToLower())
    {
    case "jpg":
    //以jpg格式保存缩略图
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    break;
    case "jpeg":
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    break;
    case "pjpeg":
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    break;
    case "gif":
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
    break;
    case "bmp":
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
    break;
    case "png":
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
    break;
    default:
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    break;
    }

    }
    catch (System.Exception e)
    {
    throw e;
    }
    finally
    {
    originalImage.Dispose();
    bitmap.Dispose();
    g.Dispose();
    }
    }

    /// <summary>
    /// 通过FileUpload上传图片并压缩图片大小
    /// 四各形式:1)指定高 按比例缩放; 2)指定宽 按比例缩放; 3)不缩放; 4)指定宽高裁剪(不会变形).
    /// </summary>
    /// <param name="fu">FileUpload对象</param>
    /// <param name="file">文件完整路径Path</param>
    /// <param name="width">图片压缩后的宽度</param>
    /// <param name="height">图片压缩后的高度</param>
    /// <returns>返回文件存储的完整路径</returns>
    public static string SavePicture(FileUpload fu, string file, int width, int height)
    {
    Random myRd = new Random();
    string prefix = System.IO.Path.GetExtension(fu.FileName).ToLower();
    string fileName = "";
    if (prefix.Equals(".jpg") || prefix.Equals(".gif") || prefix.Equals(".jpeg") || prefix.Equals(".pjpeg") || prefix.Equals(".png") || prefix.Equals(".bmp"))
    {
    string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + myRd.Next(1000) + prefix;
    string name = System.Web.HttpContext.Current.Server.MapPath(file + strFileName);
    fileName = HttpContext.Current.Server.MapPath(file);
    string filepath = HttpContext.Current.Server.MapPath("/");
    try
    {
    //获取上传图片的大小
    Stream PicStream = fu.PostedFile.InputStream;
    MakeThumbnail(PicStream, fileName + strFileName, 500, 375, "HW", prefix);//指定高宽缩放(可能变形)
    if (width == 0 && height != 0)//指定高 按比例缩放
    {
    MakeThumbnail(PicStream, fileName + "small\\" + strFileName, width, height, "H", prefix);
    }
    else if (width != 0 && height == 0)//指定宽 按比例缩放
    {
    MakeThumbnail(PicStream, fileName + "small\\" + strFileName, width, height, "W", prefix);
    }
    else if (width == 0 && height == 0)//不缩放
    {
    fu.SaveAs(fileName + "small\\" + strFileName);
    }
    else//指定宽高裁剪(不会变形)
    {
    MakeThumbnail(PicStream, fileName + "small\\" + strFileName, width, height, "Cut", prefix);
    }
    //返回图片保存的路径
    //fu.SaveAs(fileName + strFileName);//上传原图
    return file + strFileName;
    }
    catch
    {
    return null;
    }
    }
    else
    {
    return null;
    }
    }

    /// <summary>
    /// 创建文件夹
    /// </summary>
    /// <param name="directoryPath">文件夹路径</param>
    public static void CreatDirectory(string directoryPath)
    {
    try
    {
    DirectoryInfo dirInfo;
    string new_path = System.Web.HttpContext.Current.Server.MapPath(directoryPath);
    dirInfo = new DirectoryInfo(new_path);

    if (!dirInfo.Exists)
    {
    dirInfo.Create();
    System.IO.Directory.CreateDirectory(new_path);
    }
    }
    catch (System.Exception E)
    {
    throw E;
    }
    }
    }
  • 相关阅读:
    2020软件工程第三次作业
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
    2020软件工程作业05
    2020软件工程作业00——问题清单
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
  • 原文地址:https://www.cnblogs.com/shanlin/p/2303750.html
Copyright © 2020-2023  润新知