zoukankan      html  css  js  c++  java
  • 上传图片生成缩略图

    上传图片 生成不同大小的缩略图

    Code
    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="Path"></param>
    /// <param name="filePic"></param>
    /// <returns></returns>
    public static string[] UploadPic(string Path, FileUpload filePic)
    {
    //原图
    System.Drawing.Image original_image = null;
    System.Drawing.Bitmap final_image
    = null;
    System.Drawing.Graphics graphic
    = null;
    MemoryStream ms
    = null;
    //文件类型
    string preveiw = Path+"preview/";
    string display = Path + "display/";
    string print = Path + "print/";
    string[] uploadPath = new string[3];
    string filename;
    try
    {
    #region 判断目录是否存在
    if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + preveiw) == false)
    {
    System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
    + preveiw);
    }
    if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + display) == false)
    {
    System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
    + display);
    }
    if (System.IO.Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + print) == false)
    {
    System.IO.Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath
    + print);
    }
    #endregion

    HttpPostedFile image_upload
    = filePic.PostedFile;

    original_image
    = System.Drawing.Image.FromStream(image_upload.InputStream);

    #region 计算缩略各种图宽、高
    //preview
    int preview_width = original_image.Width / 4;
    int preview_height = original_image.Height / 4;
    //display
    int r=Convert.ToInt32(Math.Max(original_image.Width / 200, original_image.Height / 200));
    r
    = (r == 0 ? 1 : r);
    int display_width = original_image.Width / r;
    int display_height = original_image.Height / r;
    #endregion

    #region 上传图片
    //preview
    final_image = new System.Drawing.Bitmap(preview_width, preview_height);
    graphic
    = System.Drawing.Graphics.FromImage(final_image);
    graphic.FillRectangle(
    new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, preview_width, preview_height));
    graphic.InterpolationMode
    = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphic.DrawImage(original_image,
    0, 0, preview_width, preview_height);

    filename
    = FileName;
    final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, preveiw
    + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
    final_image
    = null;
    graphic
    = null;
    uploadPath[
    0] = preveiw + filename;
    //display
    final_image = new System.Drawing.Bitmap(display_width, display_height);
    graphic
    = System.Drawing.Graphics.FromImage(final_image);
    graphic.FillRectangle(
    new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, display_width, display_height));
    graphic.InterpolationMode
    = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphic.DrawImage(original_image,
    0, 0, display_width, display_height);

    final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, display
    + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
    final_image
    = null;
    graphic
    = null;
    uploadPath[
    1] = display + filename;

    //print
    final_image = new System.Drawing.Bitmap(original_image.Width, original_image.Height);
    graphic
    = System.Drawing.Graphics.FromImage(final_image);
    graphic.FillRectangle(
    new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, original_image.Width, original_image.Height));
    graphic.InterpolationMode
    = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphic.DrawImage(original_image,
    0, 0,original_image.Width,original_image.Height);

    final_image.Save(System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, print
    +filename), System.Drawing.Imaging.ImageFormat.Jpeg);
    final_image
    = null;
    graphic
    = null;
    uploadPath[
    2] = print + filename;

    #endregion

    return uploadPath;
    }
    catch (Exception ex)
    {
    System.Console.Error.Write(ex.Message);
    return null;
    }
    }
  • 相关阅读:
    笔记:今天必须读完的文章
    windows android 第三方模拟器 看日志
    彻底搞懂Android文件存储---内部存储,外部存储以及各种存储路径解惑
    texturepacker命令行处理图片 格式选择
    Android插件化主流框架和实现原理
    Socket心跳包机制与实现 一般的应用下,判定时间在30-40秒比较不错。如果实在要求高,那就在6-9秒。
    图解:HTTP 范围请求,助力断点续传、多线程下载的核心原理
    localstorage的跨域存储方案 介绍
    UGUI的图集处理方式-SpriteAtlas的前世今生
    web自动化,下拉滚动到底部/顶部和下拉滚动到指定的元素
  • 原文地址:https://www.cnblogs.com/jinweida/p/1332769.html
Copyright © 2011-2022 走看看