Сохранение изображения - проблема пути - PullRequest
0 голосов
/ 04 февраля 2011
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);

Если я задаю путь = "C:/PathToMyProject/imagename.jpg", тогда сохранение работает, но если я использую относительный путь, то я получаю ошибку

В GDI + произошла общая ошибка.

Я также попытался: Server.MapPath(path), но без помощи.

Мой вопрос: как задать относительный путь для загрузки папки?

Ответы [ 2 ]

3 голосов
/ 04 февраля 2011

Сопоставляет указанный виртуальный путь с физическим путем.

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

0 голосов
/ 04 февраля 2011

вы можете использовать что-то вроде:

Request.PhysicalApplicationPath + "/images/Image1.jpg";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...