C# 保存图像路径问题 public void SaveJpeg(字符串路径、图像、整数质量) { //确保质量在正确的范围内 如果((质量100)) { //创建错误消息 字符串错误=string.Format(“Jpeg图像质量必须介于0和100之间,100是最高质量。指定了{0}的值。”,质量); //抛出一个有用的异常 抛出新ArgumentOutOfRangeException(错误); } //为图像质量创建编码器参数 EncoderParameter qualityParam=新的编码器参数(System.Drawing.Imaging.Encoder.Quality,Quality); //获取jpeg编解码器 ImageCodecInfo jpegCodec=GetEncoderInfo(“图像/jpeg”); //创建我们将传递给编码器的所有参数的集合 EncoderParameters encoderParams=新的EncoderParameters(1); //设置编解码器的质量参数 encoderParams.Param[0]=qualityParam; //使用编解码器和参数保存图像 保存(路径、JPEG编解码器、编码器参数); }

C# 保存图像路径问题 public void SaveJpeg(字符串路径、图像、整数质量) { //确保质量在正确的范围内 如果((质量100)) { //创建错误消息 字符串错误=string.Format(“Jpeg图像质量必须介于0和100之间,100是最高质量。指定了{0}的值。”,质量); //抛出一个有用的异常 抛出新ArgumentOutOfRangeException(错误); } //为图像质量创建编码器参数 EncoderParameter qualityParam=新的编码器参数(System.Drawing.Imaging.Encoder.Quality,Quality); //获取jpeg编解码器 ImageCodecInfo jpegCodec=GetEncoderInfo(“图像/jpeg”); //创建我们将传递给编码器的所有参数的集合 EncoderParameters encoderParams=新的EncoderParameters(1); //设置编解码器的质量参数 encoderParams.Param[0]=qualityParam; //使用编解码器和参数保存图像 保存(路径、JPEG编解码器、编码器参数); },c#,image,path,save,relative-path,C#,Image,Path,Save,Relative Path,这一行实际上是一个问题:image.Save(path,jpegodec,encoderParams) 如果我设置了path=“C:/PathToMyProject/imagename.jpg”,则可以保存,但如果使用相对路径,则会出现错误 GDI+中发生一般性错误 我也试过:Server.MapPath(path),但没有帮助 我的问题是如何设置上传文件夹的相对路径?您可以使用以下方法: public void SaveJpeg(string path, Image image, int qu

这一行实际上是一个问题:
image.Save(path,jpegodec,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);
}

您可以使用以下内容:

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

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


Path.Combine(Server.MapPath(“~/images/store”)、imageName)

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


Path.Combine(Server.MapPath(“~/images/store”)、imageName)

发生错误时,“路径”的值是多少?这是一个已经在磁盘上的文件(即,如果是文件,则覆盖以前的版本)?发生错误时“路径”的值是多少?这是一个已经在磁盘上的文件(即,如果是文件,则覆盖以前的版本)?