How to read 1MB.tif file with bitmap class in C #

I have a problem's. how to read 1MB.tif file with bitmap class in C #. I used the code below, but got an "Out of memory" error. I searched google but couldn't find an answer.

string imgPath; imgPath = @"C:\Documents and Settings\shree\Desktop\2012.06.09.15.35.42.2320.tif"; Image newImage = Image.FromFile(imgPath); Bitmap img; img = new Bitmap(imgPath, true); MessageBox.Show("Width: "+ img.Width + " Height: " + img.Height); 
+4
source share
2 answers

If you just need to read the width and height of a file without loading it into memory, you can use the WPF BitmapDecoder class:

 using System; using System.IO; using System.Linq; using System.Windows.Media.Imaging; class Program { static void Main() { using (var stream = File.OpenRead(@"c:\work\some_huge_image.tif")) { var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); var frame = decoder.Frames.First(); Console.WriteLine( "width: {0}, height: {1}", frame.PixelWidth, frame.PixelHeight ); } } } 

If for some reason you are stuck in some era of pre- .NET 3.0, you can look at the image metadata to extract this information without loading the entire image in memory, as shown in the following answer .

+2
source

No problem creating a Bitmap object from a 1 MB file. It works easily with much larger images. I tried with a 16 MB image. You are using a lot of unwanted code. Just use

 Bitmap mybitmap=new Bitmap(path); 
-2
source

All Articles