上传图片并保存缩小后的图片,但是不保存原图能实现吗?
public bool ThumbnailCallback()
    {        return false;    }
    private void SaveImage(string filePath)
    {
        string fileName = Path.GetExtension(upLoad.PostedFile.FileName).ToLower();
        string _filename = DateTime.Now.ToString("yyMMddHHmmss");        System.Drawing.Image image = null, aNewImage = null;        //下面是生成缩略图
        int newwidth = 0, newheight = 0;
        image = System.Drawing.Image.FromFile(filePath);
        System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);        //取高和宽  
        int phWidth = image.Width;
        int phHeight = image.Height;        //设置缩略的宽度和高度
        if (phWidth > 110)
        {
            //指定宽度
            newwidth = 110;
            //根据宽度算出高度
            newheight = phHeight * newwidth / phWidth;
        }
        aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
        //将缩略图重新命名并保存
        aNewImage.Save(Server.MapPath("~/files/file/") + DateTime.Now.ToString("yyMMddHHmmss") + fileName);
        image.Dispose();
    }SaveImage(string filePath)里面的filePath可不可以用上传空间里面得到的upLoad.PostedFile.FileName值?
难道只能用服务器端的路径?

解决方案 »

  1.   

    image = System.Drawing.Image.FromFile(filePath);
    改成image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(upLoad.FileBytes));就可以了
      

  2.   


    private static System.Drawing.Bitmap ResizeImage(System.IO.Stream fileStream)
    {
    int maxW = Information.ReportPhotoMaxWidth;
    int maxH = Information.ReportPhotoMaxHeight;
    System.Drawing.Bitmap img = new System.Drawing.Bitmap(fileStream);
    int w = img.Width;
    int h = img.Height;
    bool flag = false;
    if (w > maxW)
    {
    flag = true;
    h = maxW * h / w;
    w = maxW;
    }
    if (h > maxH)
    {
    flag = true;
    w = maxH * w / h;
    h = maxH;
    } return flag ? new System.Drawing.Bitmap(img, w, h) : img;
    }调用方式
    ResizeImage(HttpPostedFile.InputStream);