当前位置:编程学习 > C#/ASP.NET >>

上传图片时,生成缩约图错误?

按大小生成缩约图
public class Limage
{


    public Image ResourceImage;
    private int ImageWidth;
    private int ImageHeight;
    public string ErrorMessage;

    public Limage(string ImageFileName)
    {
        ResourceImage = Image.FromFile(ImageFileName);
        ErrorMessage = "";
    }

    public bool ThumbnailCallback()
    {
        return false;
    }

    // 方法1,按大小
    public bool ReducedImage(int Width, int Height, string targetFilePath)
    {
        try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return false;
        }
    }


    // 方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
    public bool ReducedImage(double Percent, string targetFilePath)
    {
        try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
            ImageHeight = (ResourceImage.Height)*ImageWidth/ ResourceImage.Width;//等比例缩放
            ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return false;
        }
    }


}



   protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string filepath = FileUpload1.PostedFile.FileName; 
        int fileSize = FileUpload1.PostedFile.ContentLength;
        if (fileSize > 1024 * 100 * 2 * 10)
        {
            Show(this.Page, "不能超过2M");
            return;
        }
        else
        {

            string filex = filepath.Substring(filepath.LastIndexOf(".") + 1).ToLower();
            
            if (filex == "jpg" || filex == "gif" || filex == "jpeg" || filex == "png")
            {
                string newsfilename = "S" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + filex;
                string path = Server.MapPath("~/UploadFiles/NewProduct/");
                string serverpath2 = path + DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + filex;
                string serverpath1 = path + newsfilename;

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileUpload1.PostedFile.SaveAs(serverpath1);
                Limage limg = new Limage(newsfilename);//这里文件错误,应是路径错误吧
                limg.ReducedImage(502,648,serverpath2);
            }

            else
            {
               Show(this.Page, "格式不对");
            }
        }
    }

这代码是网上找的,结合我的要求
我每次调用生成缩约图的代码是,Limage limg = new Limage(newsfilename);执行到这里就失败,错误: System.IO.FileNotFoundException: 201110101205.gif
可能是不存在文件吧,但是我查看文件时,文件是存在的啊?请问下怎么处理,或者提供相应的生成缩约图代码,网上很不能用

--------------------编程问答-------------------- http://blog.csdn.net/taomanman/article/details/5721341 --------------------编程问答-------------------- 权限都加上了没 --------------------编程问答--------------------
      string serverpath2 =Path.Combine( path ,DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + filex);
                string serverpath1 =Path.Combine( path , newsfilename);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileUpload1.PostedFile.SaveAs(serverpath1);
                Limage limg = new Limage(serverpath1);
--------------------编程问答-------------------- 路径错误, 我以前也碰到多, 你用 绝对路径 试试
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,