• asp.net上传图片


    //检查上传文件的格式是否有效
    if (this.uploadfile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
    {
    Response.Write("<script> alert('上传图片格式无效'); </script>");
    return;
    }
    //生成原图
    Byte[] oFileByte = new byte[this.uploadfile.PostedFile.ContentLength];
    System.IO.Stream oStream = this.uploadfile.PostedFile.InputStream;
    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

    int oWidth = oImage.Width; //原图宽度
    int oHeight = oImage.Height; //原图高度
    int tWidth =800; //设置缩略图初始宽度 大于800自动取800
    int tHeight =1000); //设置缩略图初始高度 大于1000自动取1000


    //按比例计算出缩略图的宽度和高度
    if (oWidth >= oHeight)
    {
    tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
    }
    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 fullFileName = this.uploadfile.PostedFile.FileName.ToString();
    fullFileName = fullFileName.Remove(fullFileName.LastIndexOf('.'));
    string filename = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);


    string strLocation = this.Server.MapPath(Page.Request.ApplicationPath) + "\\uploads\\"; //存储路径


    }


    Random r = new Random(); //防止重名
    int i = (int)(r.NextDouble() * 10000);
    if (!Directory.Exists(strLocation))
    {
    Directory.CreateDirectory(strLocation);
    }



    //string oFullName =strLocation +"x"+i.ToString() + filename+".jpg"; //保存原图的物理路径
    string tFullName = strLocation + i.ToString() + filename + ".jpg"; //保存缩略图的物理路径

    try
    {
    //以JPG格式保存图片
    //oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
    tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);

    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    //释放资源
    oImage.Dispose();
    g.Dispose();
    tImage.Dispose();
    }
    }
  • 相关阅读:
    实现168732363.66元用逗号格式为168,732,363.66元
    程序员的十步学习法
    js中字符串方法大全
    js中数组方法大全
    异常,常用工具
    抽象类,常用类
    this 关键字
    面向对象
    DOS.JDK
    Android
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1830805.html
Copyright © 2020-2023  润新知