2012-02-20 2 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

+0

전에 말했듯이 .NET에는 많은 디코더가 있습니다. 이 버전의 코드는 자동으로 올바른 것을 선택하고 모든 .net 인식 가능한 파일 형식으로 작동합니다. –

+0

당신 말이 맞아요. 그러나 그것은 단순한 웹 사이트 프로젝트라고 말하는 걸 잊었습니다. WPF 처리기가이 작업을 수행하지 않거나 잘못 되었습니까? – Fantasterei

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