保存图像 - 路径问题

时间:2011-02-03 21:21:25

标签: c# image path save relative

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);
}

这一行实际上是一个问题:image.Save(path, jpegCodec, encoderParams);

如果我设置path = "C:/PathToMyProject/imagename.jpg"然后保存工作,但如果我使用相对路径然后我得到错误

  

GDI +中发生了一般性错误。

我也尝试过:Server.MapPath(path)但没有帮助。

我的问题是如何设置上传文件夹的相对路径?

2 个答案:

答案 0 :(得分:2)

将指定的虚拟路径映射到物理路径。

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

答案 1 :(得分:0)

你可以使用类似的东西:

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