4

i am in trouble. how to read 1MB .tif file with bitmap class in c#. i used below code but getting error "Out of memory".I searched google lot but not find any answer yet.

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);
Nikhil D
  • 2,479
  • 3
  • 21
  • 41

2 Answers2

2

If you just need to read the width and height of the file without loading it in memory you could use WPF's 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 pre-.NET 3.0 era you could look at the image metadata to extract this information without loading the entire image in memory as shown in the following answer.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks Darin for ur valuable reply it works for me but i have one problem i have to find pixel value of .TIF image on the basis of X Y coordinates. – Nikhil D Jun 23 '12 at 05:56
-2

There is no problem for Creating an Bitmap object from a File of 1MB Size.It works easily with much larger images.I have tried with a 16MB Image.You are using lot of unwanted code.just use

Bitmap mybitmap=new Bitmap(path);
techno
  • 6,100
  • 16
  • 86
  • 192
  • In your situation it works because I guess that your memory is fully free. But what will happen if there's no more RAM left because your application has used it for other purposes? – Darin Dimitrov Jun 22 '12 at 12:51
  • To Process large Images you require a system which is not dinosaur age with atleast 1MB of Physical Memory free – techno Jun 22 '12 at 12:53
  • Who said that the system is of dinosaur age? You could perfectly fine have a modern server hardware with lots of RAM that simply runs out because you have loaded lots of images in your application without ever releasing them. By the way have you ever attempted to work with images in a very modern era Windows Phone 7 application? If you had you would have very quickly realized the limitations. – Darin Dimitrov Jun 22 '12 at 12:55
  • You are Talking about Dynamic Memory Management.If your application does not Dispose Bitmaps after required processing,then i would say such a system is faulty.No one will have infinite resources,proper management is necessary in any case. – techno Jun 22 '12 at 12:58
  • I completely agree that a system that doesn't dispose the bitmaps is faulty. But you could have a system which needs to process lots of bitmaps in parallel to determine say their width and height. Imagine what will happen if you attempt to run your code in parallel on 100 bitmaps of an important size. And I am not saying that your code is faulty: you could have perfectly fine disposed everything. It's just that this code will blow with an OME. – Darin Dimitrov Jun 22 '12 at 13:00
  • I don't want to enter into a debate here.Let me say-If i have implemented a system, i know its limitations and the amount of concurrent operations it cam carry on like image processing.As im the developer i know that i cannot process 100 images in parallel,as my system does not have sufficient resources.So i will do one by one,disposing of the unnecessary.As per your answer ,you can just read the Bitmap data like Height,width etc.What if the user wants to create a Graphic object and manipulate the Bitmap.Nothing is possible without proper resources – techno Jun 22 '12 at 13:08