Saving image - path problem

萝らか妹 提交于 2019-12-25 14:08:40

问题


public void SaveJpeg(string path, Image image, int quality)
{
    //ensure the quality is within the correct range
    if ((quality < 0) || (quality > 100))
    {
        //create the error message
        string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality.  A value of {0} was specified.", quality);
        //throw a helpful exception
        throw new ArgumentOutOfRangeException(error);
    }

    //create an encoder parameter for the image quality
    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
    //get the jpeg codec
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

    //create a collection of all parameters that we will pass to the encoder
    EncoderParameters encoderParams = new EncoderParameters(1);
    //set the quality parameter for the codec
    encoderParams.Param[0] = qualityParam;
    //save the image using the codec and the parameters
    image.Save(path, jpegCodec, encoderParams);
}

This line is actually a problem: image.Save(path, jpegCodec, encoderParams);

If I set path = "C:/PathToMyProject/imagename.jpg" then saving works, but if I use relative path then I get error

A generic error occurred in GDI+.

I also tried: Server.MapPath(path) but no help.

My question is how to set relative path to upload folder?


回答1:


Maps the specified virtual path to a physical path.

Path.Combine(Server.MapPath("~/images/store"), imageName);




回答2:


you can use something like:

Request.PhysicalApplicationPath + "/images/Image1.jpg";


来源:https://stackoverflow.com/questions/4891880/saving-image-path-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!