保存图像-路径问题

发布于 2024-10-15 22:59:40 字数 1381 浏览 3 评论 0原文

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) 但没有帮助。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温折酒 2024-10-22 22:59:40

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

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

Maps the specified virtual path to a physical path.

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

染年凉城似染瑾 2024-10-22 22:59:40

你可以使用类似的东西:

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

you can use something like:

Request.PhysicalApplicationPath + "/images/Image1.jpg";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文