zoukankan      html  css  js  c++  java
  • 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();
    }
    }
  • 相关阅读:
    编译预处理命令define
    共享数据的包含const
    友元类,友元函数
    静态成员static
    this 指针
    构造函数与析构函数
    c++类的基础
    void指针和const指针
    c++基础
    组播的实现
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1830805.html
Copyright © 2011-2022 走看看