简体   繁体   English

参数无效在C#中创建新的位图时发生异常?

[英]Parameter is not valid Exception in creating new Bitmap in C#?

We have a method to save an image as corrected format as the following : 我们提供了一种将图像保存为校正格式的方法,如下所示:

public static string SaveImageAsCorrectFormat(string tempFilePath, string newFileName, string destinationDirectoryPath)
{
    using (Image image = new Bitmap(tempFilePath))// Exception : Parameter is not valid.
    {
        string extension = image.RawFormat.GetExtension();
        string newAbsoluteFilePath = Path.Combine(destinationDirectoryPath, string.Format("{0}.{1}", newFileName, extension));
        image.Save(newAbsoluteFilePath, image.RawFormat);
        return newAbsoluteFilePath;
    }
}

but in in the following line an exception has occurred : 但在以下行中发生了异常:

//Parameter is not valid.
using (Image image = new Bitmap(tempFilePath))

I changed it to the following but Out of memory has occurred : 我将其更改为以下内容,但发生Out of memory

 // Out of memory
using (Bitmap image = (Bitmap)Image.FromFile(tempFilePath))

The image size is 10KB and 10GB of RAM is free. 图像大小为10KB,并且有10GB的RAM是免费的。

What's the problem? 有什么问题?

PS: PS:
I don't have the problem in local. 我在当地没有问题。 But when I publish the software on server this problem occur. 但是,当我在服务器上发布软件时,会出现此问题。

Edit: 编辑:
I'm using windows Server 2012 R2 and IIS 8.5.9600.16384 . 我正在使用windows Server 2012 R2IIS 8.5.9600.16384 The Application (website) has full control on IIS_IUSRS and IUSR . 该应用程序(网站)对IIS_IUSRSIUSR具有完全控制权。
I think the problem isn't related to permission, because I could open the file with the following code : 我认为问题与权限无关,因为我可以使用以下代码打开文件:

using (FileStream fileStream = File.Open(tempFilePath, FileMode.Open)) // OK
using (Image image = new Bitmap(fileStream))// Exception : Parameter is not valid.

Solution: 解:
I changed website application pool identity to Local System and it's OK now, Is it OK to change application pool identity or it is a security hole ? 我已将网站application pool identity更改为“ Local System ,现在可以,更改应用程序池标识是否可以,或者这是一个安全漏洞?

It appears that the image is corrupted, or maybe just has the wrong file extension. 图像似乎已损坏,或者文件扩展名错误。

If the extension is jpeg , .NET will attempt to decode it as a JPEG. 如果扩展名是jpeg ,则.NET将尝试将其解码为JPEG。 Note that this behaviour differs from browsers: browsers tend to look at the content of the file and decide its format based on that; 请注意,此行为与浏览器不同:浏览器倾向于查看文件的内容并根据该文件决定其格式; due to this, you could be under the misconception that a jpeg file is fine because it displays in the browser, when in reality it contains a PNG image. 因此,您可能会误以为jpeg文件很好,因为它在浏览器中显示,而实际上它包含PNG图像。

If you open the image file in Firefox, the window titlebar tells you what the real file format is independent of its file extension. 如果您在Firefox中打开图像文件,则窗口标题栏会告诉您实际的文件格式与文件扩展名无关。

You have to grant permission to the folder you are trying to access through your web application. 您必须授予尝试通过Web应用程序访问的文件夹的权限。 If you are using cPanel check here . 如果您使用的是cPanel, 请在此处检查

Late in the day but I could not find a reliable workaround. 当天晚些时候,但我找不到可靠的解决方法。 Sometimes environmental issues will affect operation. 有时环境问题会影响操作。 In my case I retried the action a number of times if an exception occurred. 就我而言,如果发生异常,我将重试该操作多次。 If you take this approach your action should succeed but make sure the action is idempotent. 如果您采用这种方法,则您的操作应该会成功,但是请确保该操作是幂等的。

eg in C# 例如在C#中

Action action = () => { //call SaveImageAsCorrectFormat }
try {
  action();
}
catch
{
  try {
    action();
  catch (Exception exception) {
    //handle
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM