从流加载图像并保存它给出“GDI +中发生一般错误”[复制]

时间:2022-05-07 21:19:54

This question already has an answer here:

这个问题在这里已有答案:

I'm working on a website where the user can upload some images. And I'm using HttpPostedFileBase to get the uploaded image

我正在一个用户可以上传一些图像的网站上工作。我正在使用HttpPostedFileBase来获取上传的图像

Here is the Action header:

这是Action标题:

public async Task<ActionResult> Create(Advert advert, HttpPostedFileBase FileURL)

public async Task Create(广告广告,HttpPostedFileBase FileURL)

I can save the image using FileURL.SaveAs(HttpContext.Server.MapPath(fileName)) where fileName is a parameter i generate randomly, and everything is working perfectly.

我可以使用FileURL.SaveAs(HttpContext.Server.MapPath(fileName))保存图像,其中fileName是我随机生成的参数,一切都运行良好。

Now I want to generate a thumbnail for my image, so I wrote the next method:

现在我想为我的图像生成缩略图,所以我编写了下一个方法:

public static void SaveJpeg(string path, HttpPostedFileBase uploadedFile, int quality)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");

        // Encoder parameter for image quality 
        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        // JPEG image codec 

        ImageCodecInfo codec = GetEncoderInfo("image/jpeg");
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        Image img = Image.FromStream(uploadedFile.InputStream);
        var thumbnail = img.GetThumbnailImage(img.Width / 5, img.Height / 5, null, IntPtr.Zero);
        //img.Save(path, codec, encoderParams);
        thumbnail.Save(path, codec, encoderParams);
    }

But I'm getting A generic error occured in GDI+ error when saving the thumbnail

但是我在保存缩略图时收到GDI +错误中发生的一般错误

PS: I tested my code on local image (passing Image to the method) and everything worked well.

PS:我在本地图像上测试了我的代码(将Image传递给方法),一切运行良好。

Please help.

Thanks in advance :)

提前致谢 :)

1 个解决方案

#1


0  

You may already have set this, but this kind of error can occur if the permissions are not set correctly on the directory you are saving to. By default, sites in IIS don't have permissions to write to the IIS directories. The default user is IIS_IUSRS and will need to be given Modify access if this is the issue.

您可能已经设置了此项,但如果未在要保存的目录上正确设置权限,则可能会发生此类错误。默认情况下,IIS中的站点无权写入IIS目录。默认用户是IIS_IUSRS,如果出现此问题,则需要为其提供修改权限。

从流加载图像并保存它给出“GDI +中发生一般错误”[复制]

#1


0  

You may already have set this, but this kind of error can occur if the permissions are not set correctly on the directory you are saving to. By default, sites in IIS don't have permissions to write to the IIS directories. The default user is IIS_IUSRS and will need to be given Modify access if this is the issue.

您可能已经设置了此项,但如果未在要保存的目录上正确设置权限,则可能会发生此类错误。默认情况下,IIS中的站点无权写入IIS目录。默认用户是IIS_IUSRS,如果出现此问题,则需要为其提供修改权限。

从流加载图像并保存它给出“GDI +中发生一般错误”[复制]