0

The image i trying to convert into bitmap is of size 36.7 MB and having dimensions 29696 x 4381.

I am posting my code below:

What I have tried:

try
{
    FileStream fs = new FileStream(FilePath,FileMode.Open,FileAccess.Read);

    System.Drawing.Image objImage = System.Drawing.Image.FromStream(fs);//From File

    int height = objImage.Height;//Actual image width
    int width = objImage.Width;//Actual image height
    LineNumber = 1;
    System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);// create bitmap with same size of Actual image.Getting error on this line    
}
catch(Exception ex)
{
}

error

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • please see this answer (http://stackoverflow.com/questions/6333681/c-sharp-parameter-is-not-valid-creating-new-bitmap). You are more then likely hitting the memory cap – timkly Nov 11 '16 at 06:59
  • Consider using WIC https://msdn.microsoft.com/en-us/library/windows/desktop/ee719654%28v=vs.85%29.aspx – scotru Nov 11 '16 at 07:02
  • Could you please show as a whole code – Roman Marusyk Nov 11 '16 at 07:10
  • I am a little bit confused. How the bitmap image can have size 36.7 MB if the image resolution is 29696 x 4381? I guess that you are using an ARGB bitmap that needs 32bit per pixel. Is it correct? The expected size is around 500MB – cristallo Nov 11 '16 at 08:40
  • Probably a JPEG image. Project > Properties > Build tab, untick the "Prefer 32-bit" checkbox. You don't prefer it when you want to load such huge images. – Hans Passant Nov 11 '16 at 08:48
  • @cristallo: Raw image file is 36.7 Mb. The Bitmap size is more than 500 mb – Dattaprasad Dhuri Nov 11 '16 at 09:10
  • @HansPassant Prefer 32-bit is already unchecked – Dattaprasad Dhuri Nov 11 '16 at 09:10
  • It is technically possible to run out of memory in a 64-bit process as well. You are not calling the **required** Dispose() method to get rid of old bitmap objects. You forgot it on the *objImage* object, probably in the rest of your program as well. The GC cannot usually help, the Bitmap class is quite small. Practice using the *using* statement. Also, the `fs` variable is quite dangerous. You cannot close the stream until the bitmap is disposed. Your program will also fail if you try to load the same file again. – Hans Passant Nov 11 '16 at 09:14
  • I think that your problem is the image format not the memory. Can you try to create an empty bit image using the desired size without loading the image itself? `bitmap=new Bitmap(newWidth,newHeight);`. It is a test for excluding memory issue. – cristallo Nov 11 '16 at 09:17
  • @cristallo i am able to create empty bitmap image – Dattaprasad Dhuri Nov 11 '16 at 10:29
  • Can you try to load the image from stream con-validating the image data? `Image.FromStream(fs, false, true);` – cristallo Nov 11 '16 at 11:34
  • @cristallo still giving the same error – Dattaprasad Dhuri Nov 11 '16 at 11:53
  • Can you confirm that the error is still happening on line `new System.Drawing.Bitmap(objImage, width, height);`? – cristallo Nov 11 '16 at 11:55
  • @cristallo ya it's on the same line – Dattaprasad Dhuri Nov 11 '16 at 12:21

0 Answers0