2012-02-20 20 views
1

我目前正在对图像进行数据检查。我需要请求尺寸(宽度&高度)和图像的分辨率。超过70MB的文件在GDI问题上抛出“内存不足”异常。有没有其他方法来获取文件信息?通过FromStream解析它同样的错误......内存不足异常如果文件大到

Using myfile = Image.FromFile(filePath) 
... 
End Using 

回答

3

您可以使用以下代码获取图像属性(它仅加载元数据):

using (var fs = new FileStream(@"C:\Users\Dmitry\Pictures\blue-earth-wallpaper.jpg", FileMode.Open, FileAccess.Read)) { 
    var decoder = BitmapDecoder.Create(fs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
    var size = decoder.Frames[0].PixelWidth; 
    var height = decoder.Frames[0].PixelHeight; 
    var dpiX = decoder.Frames[0].DpiX; 
    var dpiY = decoder.Frames[0].DpiY; 
} 
+0

这不适合我。我需要比JPG更多的东西。谢谢 – Fantasterei 2012-02-20 22:14:28

+0

正如我之前所说,.NET中有很多解码器。该版本的代码自动选择正确的代码,并可以使用所有.net可识别的文件格式。 – 2012-02-20 22:45:05

+0

你是对的,但我忘了说这是一个简单的网站项目。 WPF处理程序不工作,或者我错了? – Fantasterei 2012-02-21 16:56:07

0

我发现这个链接http://www.fastgraph.com/help/image_file_header_formats.html它告知文件中,你可以找到的类型和其尺寸。我想,如果你使用类似下面这样寻求和获得前几个字节和关闭一旦你完成,不应该是使用多少资源

下面

未经测试的代码...

// This really needs to be a member-level variable; 
private static readonly object fsLock = new object(); 
// Instantiate this in a static constructor or initialize() method 
private static FileStream fs = new FileStream("myFile.txt", FileMode.Open); 


public string ReadFile(int fileOffset) { 

    byte[] buffer = new byte[bufferSize]; 

    int arrayOffset = 0; 

    lock (fsLock) { 
     fs.Seek(fileOffset, SeekOrigin.Begin); 

     int numBytesRead = fs.Read(bytes, arrayOffset , bufferSize); 

     // Typically used if you're in a loop, reading blocks at a time 
     arrayOffset += numBytesRead; 
    } 

    // Do what you want to the byte array and close 

} 
+0

图像的分辨率很重要。这些功能不会让我访问。谢谢。 – Fantasterei 2012-02-20 22:32:20