1

I need to load tiff file in a picture box and I use the following code to do this:

picBox.Image = Image.FromFile(files[current].FullName);

this code works great on my computer, but when I deploy it on another PC, it throws OutOfMemoryException.

tiff files are generated by a Fax service.

I want to know how to load this file in an exception safe manner.

Yurii
  • 4,811
  • 7
  • 32
  • 41
Navid_pdp11
  • 3,487
  • 3
  • 37
  • 65
  • What is the file size? – jwatts1980 Aug 18 '14 at 05:33
  • maximum size of my file are 40k – Navid_pdp11 Aug 18 '14 at 05:35
  • Try loading the file into a MemoryStream or a byte array and instantiating the image from that. Weird things happen when streams are closed while GDI is doing things with them. See : http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream. – Andrew Kennan Aug 18 '14 at 05:36
  • In my experience it is less to do with the size of the file and more to do with the size in pixels when loaded. Memory usage for images is going to relate to the number of pixels used multiplied by the bytes per pixel. – ScruffyDuck Aug 18 '14 at 05:44
  • 1
    Yes, forget the file size; what are the dimensions and pixel depth? That said, 40k is pretty small, even for a high performance compression algorithm. Seems unlikely that it would be too large once decoded, probably no more than a MB or two. Are you sure that this is actually causing the problem and that you're not just "leaking" memory? for example, you're responsible for `Dispose()`ing `picBox.Image` before setting a new value. How many images are you loading and how quickly? – Ed S. Aug 18 '14 at 05:57
  • i only load 1 image ... – Navid_pdp11 Aug 18 '14 at 06:11
  • i try to use filestream to load image using : picShow.Image = Image.FromStream(stream); but now i get parameter is not valid exception – Navid_pdp11 Aug 18 '14 at 06:36
  • 1
    The remarks on MSDN say "You must keep the stream open for the lifetime of the Image" and there appears to be a lot of other people with the same issue. I think you need to prevent the stream being closed or disposed while the image is being displayed. – Andrew Kennan Aug 18 '14 at 07:02

2 Answers2

1

i solve this problem by changing Pixel format of my loaded image using this code :

  Bitmap orig = new Bitmap(filePath);
            Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            using (Graphics gr = Graphics.FromImage(clone))
            {
                gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
                picShow.Image = clone;
            }

i hope this help other persons

Navid_pdp11
  • 3,487
  • 3
  • 37
  • 65
0

I have same problem and so confused to solving. you save my time about 3 days.

but i test your code and i found that your image lost transparency at the end.

i need some png image and edit your code and my problem solved.

thanx