zoukankan      html  css  js  c++  java
  • C# 缩略图(有问题)请大家帮忙,谢谢,我没有分给

    希望帮我看看,为什么我以上传图片,生成缩略图,这个图片就被IIS占用了,改怎么修改。求

    //aspx.cs文件

    Boolean fileOK = false;
    String fileExtension = System.IO.Path.GetExtension(FileUpload_img.FileName).ToLower();
    String[] allowedExtensions =  { ".jpg" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
        if (fileExtension == allowedExtensions[i])
        {
            fileOK = true;
        }
    }
    if (fileOK)
    {
        try
        {

            string path_BigPic = md5code.UploadFilePic(this.FileUpload_img, ref Path_SmlPic);
            return path_BigPic;
        }
        catch (Exception ex)
        {
        }
    }
    else
    {
        this.RegisterStartupScript("jpg", "<script>alert('格式不正确!暂只支持 jpg 格式')</script>");
        return "-1";
    }

    ********************************

    //调用的cs文件

    public string UploadFilePic(FileUpload fu, ref string path_xiao)
    {

        if (fu == null)
        {
            return "-1";
        }

        string pathdatu = HttpContext.Current.Server.MapPath("../UploadFiles/BigPic/");

     

        //获得客户端的文件物理路径
        string clientFileName = fu.PostedFile.FileName;


        //获得文件的后缀名称
        string extension = System.IO.Path.GetExtension(clientFileName);

     


        //得到一个   年月日+4位随机数   的字符串 作为 图片文件名
        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + new Random().Next(1000, 9999).ToString();

        //判断文件夹是否存在
        if (!System.IO.Directory.Exists(pathdatu))
        {
            //创建文件夹
            System.IO.Directory.CreateDirectory(pathdatu);
        }

        if (!System.IO.Directory.Exists(pathdatu))
        {
            //系统异常,如:硬盘空间不够
            //写日志

            return "0";
        }

        string path = pathdatu + fileName + extension;
        fu.PostedFile.SaveAs(path);
       
        System.Drawing.Image image_da = System.Drawing.Image.FromFile(path);

        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image_da);

        int oWidth = image_da.Width; //原图宽度
        int oHeight = image_da.Height; //原图高度

        int tWidth = 152; //设置缩略图初始宽度
        int tHeight = 152; //设置缩略图初始高度

        //按比例计算出缩略图的宽度和高度
        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);

        g = Graphics.FromImage(tImage);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
        g.Clear(Color.Transparent); //清空画布并以透明背景色填充
        g.DrawImage(image_da, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);

        string tname = DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";

        string xiaotupath = HttpContext.Current.Server.MapPath("../UploadFiles/SmlPic/");  //小图

        string tFullName = xiaotupath + "t" + tname; //保存缩略图的物理路径

        try
        {
            //以JPG格式保存图片 ------ 保存小图文件

            tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            //释放资源

            System.GC.Collect();   //关键就在这句(如果不加,上传的图片就会被IIS进程占用)
            fu.Dispose();
            image_da.Dispose();
            g.Dispose();
            tImage.Dispose();
        }

        path_xiao = "../UploadFiles/SmlPic/t" + tname;
        return "../UploadFiles/BigPic/" + fileName + extension;
    }

  • 相关阅读:
    二叉树
    树的存储表示
    Jarvis OJ | WEB
    xctf | pwn进阶
    《C++Primer Plus》 | 处理数据
    xctf---stack2 | gdb&IDA 调试
    IDA | Windows主机与Ubuntu 16.04远程调试
    ROP | 中级
    IDA | 窗口
    epub reading
  • 原文地址:https://www.cnblogs.com/binlunia/p/11267795.html
Copyright © 2011-2022 走看看