Image.FromFile 内存不足
Out of memory on Image.FromFile
我在 Windows 7 上编写了一个程序,用于从放入文件夹的图像中读取 QR 码并且工作正常,但现在我在 Windows 10 上尝试它并崩溃。
我正在使用 Image.FromFile 来读取文件。第一次好像没问题,第二次就抛出Out of memory异常
代码如下:
private void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
string strFileExt = Path.GetExtension(e.FullPath);
if (Regex.IsMatch(strFileExt, @"\.jpg|\.png", RegexOptions.IgnoreCase))
{
try
{
using (var b = (Bitmap)Image.FromFile(e.FullPath))
{
var result = reader.Decode(b);
if (result != null)
{
if (result.ResultPoints[0].X < result.ResultPoints[2].X)
{
if (result.ResultPoints[0].Y < result.ResultPoints[2].Y)
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
}
}
else
{
if (result.ResultPoints[0].Y < result.ResultPoints[2].Y)
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
}
else
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
}
}
string fileName = result.Text + DateTime.Now.ToString("yyyyMMdd-HHmmss") + Path.GetExtension(e.FullPath);
b.Save(Path.Combine(ConfigurationManager.AppSettings["destinationFolder"], fileName));
client.SendData(result.Text + "|" + fileName, "qrListener");
Console.WriteLine("QR: " + result.Text + " File: " + fileName);
}
}
File.Delete(e.FullPath);
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}
我已经删除了 using 中的所有内容,但仍然崩溃。
根据MSDN
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.
我认为您尝试加载的图像不再受支持。您能否上传示例图片以帮助我们重现图像。
可能图像的内部结构混淆了 GDI+ 编码器。尝试使用图像编辑器打开错误图像并重新保存。现在尝试使用您的代码加载新图像。
由于某种原因,当文件已经被解锁时 watcher 第一次触发,但下一次文件仍在写入,不完整,这使得 OOM 异常变得有意义,因为
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.
我添加了一个方法,每 100 毫秒检查一次文件是否已解锁。
我刚注意到的另一件事是,它在我复制文件时抛出异常,而不是在移动文件时抛出异常。
我在 Windows 7 上编写了一个程序,用于从放入文件夹的图像中读取 QR 码并且工作正常,但现在我在 Windows 10 上尝试它并崩溃。 我正在使用 Image.FromFile 来读取文件。第一次好像没问题,第二次就抛出Out of memory异常
代码如下:
private void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
string strFileExt = Path.GetExtension(e.FullPath);
if (Regex.IsMatch(strFileExt, @"\.jpg|\.png", RegexOptions.IgnoreCase))
{
try
{
using (var b = (Bitmap)Image.FromFile(e.FullPath))
{
var result = reader.Decode(b);
if (result != null)
{
if (result.ResultPoints[0].X < result.ResultPoints[2].X)
{
if (result.ResultPoints[0].Y < result.ResultPoints[2].Y)
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
}
}
else
{
if (result.ResultPoints[0].Y < result.ResultPoints[2].Y)
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
}
else
{
b.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
}
}
string fileName = result.Text + DateTime.Now.ToString("yyyyMMdd-HHmmss") + Path.GetExtension(e.FullPath);
b.Save(Path.Combine(ConfigurationManager.AppSettings["destinationFolder"], fileName));
client.SendData(result.Text + "|" + fileName, "qrListener");
Console.WriteLine("QR: " + result.Text + " File: " + fileName);
}
}
File.Delete(e.FullPath);
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}
我已经删除了 using 中的所有内容,但仍然崩溃。
根据MSDN
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.
我认为您尝试加载的图像不再受支持。您能否上传示例图片以帮助我们重现图像。
可能图像的内部结构混淆了 GDI+ 编码器。尝试使用图像编辑器打开错误图像并重新保存。现在尝试使用您的代码加载新图像。
由于某种原因,当文件已经被解锁时 watcher 第一次触发,但下一次文件仍在写入,不完整,这使得 OOM 异常变得有意义,因为
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.
我添加了一个方法,每 100 毫秒检查一次文件是否已解锁。
我刚注意到的另一件事是,它在我复制文件时抛出异常,而不是在移动文件时抛出异常。