• asp.net上传图片自动生成缩略图功能代码


    if (FileUpload1.FileName.ToString() == "")
    {
    Label3.Text = "请选择图片!";
    }
    else
    {
    Boolean FileOK = false;
    if (this.FileUpload1.HasFile)
    {
    // 限制上传图片小于 2M
    if (FileUpload1.PostedFile.ContentLength <= 2097152)
    {
    // 图片 Guid 重命名
    Session["WorkingImage"] = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName.ToString()).ToLower();
    String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower();
    String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
    if (FileExtension == allowedExtensions[i])
    {
    FileOK = true;
    }
    }
    }
    // 上传图片大于 2M 警告
    else
    {
    FileOK = false;
    Response.Write("<script language=javascript>alert('图片不要超过 2M 大小!');window.location.href=window.location.href;</script>");
    }
    }
    if (FileOK)
    {
    try
    {
    //生成原图
    Byte[] oFileByte = new byte[this.FileUpload1.PostedFile.ContentLength];
    System.IO.Stream oStream = this.FileUpload1.PostedFile.InputStream;
    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
    
    int oWidth = oImage.Width; //原图宽度
    int oHeight = oImage.Height; //原图高度
    int tWidth; //缩略图宽度
    int tHeight; //缩略图高度
    
    //按比例计算出缩略图的宽度和高度
    if (oWidth >= oHeight)
    {
    tWidth = 300;
    tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
    }
    else
    {
    tHeight = 300;
    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.FileUpload1.PostedFile.FileName.ToString();
    fullFileName = fullFileName.Remove(fullFileName.LastIndexOf('.'));
    string filename = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
    //存储路径
    string strLocation = path.ToString();
    string tFullName = strLocation + Session["WorkingImage"].ToString();
    try
    {
    // 上传缩略
    tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
    //输出上传成功提示并显示保存路径
    TextBox3.Text = "uploadfiles/product/" + Session["WorkingImage"].ToString();
    // pic_upload.Visible = false;
    // pic_crop.Visible = true;
    // 读取缩略图
    path = "../uploadfiles/product/" + Session["WorkingImage"].ToString();
    
    }
    catch (Exception)
    {
    Response.Write("<script language=javascript>alert('上传失败,请重试!');window.location.href=window.location.href;</script>");
    }
    finally
    {
    //释放资源
    oImage.Dispose();
    g.Dispose();
    tImage.Dispose();
    }
    }
    catch (Exception)
    {
    Response.Write("<script language=javascript>alert('上传失败,请重试!');window.location.href=window.location.href;</script>");
    }
    }
    }
    

      

  • 相关阅读:
    js 性能调试
    js 面向对象编程
    js 零碎
    如果遇到二维数组 想取某个字段的和
    昨天写支付接口时遇到支付接口返回数据接收地址,session数据丢失(或者说失效)的问题
    mysql报错: 1548-Cannot load from mysql.proc. The table is probably corrupted 解决办法
    php 时间倒计时代码 个人写法 有好的想法的欢迎贴出来分享
    linux 环境下安装mysql5.6
    关于数据库连接不上 出现错误的问题
    推荐一个不错的css3网站 可以直接调用的
  • 原文地址:https://www.cnblogs.com/nianyuwen/p/2351363.html
Copyright © 2020-2023  润新知