0

I have to draw something on a Image wich is captured by the Camera. This works on many Devices, but sometimes the RAM is too small or the pictures too big and the function crashed with a OutOfMemory Exception.

How am I able to: a) optimize the code to prevent this Exceptions b) Handle this Exceptions (making pictures smaller, free Ram etc.

here is the code:

 Dim from_bmp As Bitmap
 Dim bmp As Bitmap

 from_bmp = New Bitmap(picname)
 'I also tryed this with a function which is creating the Bitmap from Filestream
 'I also tryed this with the OpenNETCF.Drawing.Imaging.IImage
 'If the Original Pictiure is too big, the function will crash here.

 bmp = New Bitmap(from_bmp.Width, from_bmp.Height + stampheight)
 'now I Create a bitmap which is higher than the original, in order to write the stamp beneth the picture

 Dim font As New System.Drawing.Font("Arial", 30, FontStyle.Regular)
 gr = Graphics.FromImage(bmp)
 gr.DrawImage(from_bmp, 0, 0)
 from_bmp.Dispose()
 'here I draw somethin in the Bitmap
 bmp.Save(deststring, System.Drawing.Imaging.ImageFormat.Jpeg)
 gr.Dispose()
 bmp.Dispose()
5
  • Upgrading your system memory is the best bet Commented May 26, 2011 at 15:08
  • @Gens That exception usually comes on many platform, not only Windows, not only .NET. Commented May 26, 2011 at 15:13
  • this is net.cf, the code is running on mobile devices, I'm not able to upgrade the Memorya of them ^^ Commented May 26, 2011 at 15:14
  • How big is the image (w x h) and what is it's color depth? Commented May 26, 2011 at 17:46
  • this depends on the camerasettings of the device, i use this function on 1800 mobile devices and round about 20 different types. Ist's running well for years, but The Problem occurs very often on the new Motorola ES400. The Images on this device are (in the smallest case) 1024*1380 px, 192 dpi, 24 Bit Commented May 27, 2011 at 6:52

1 Answer 1

2

I'd likely use a "using" pattern for your Bitmaps. Also, be aware that an OOM on Bitmap creation can often be overcome by simply trying again (here's a diatribe as to why). My bet is that the GC Heap is too full and a second request, especially after a collection, would succeed.

I'd go through the rest of the code (that not shown) and make sure that all other graphics objects are getting properly Disposed - the CF doesn't handle auto clean-up of the native resources of graphic objects very well and often needs a bit of help (again, see the above link).

Sign up to request clarification or add additional context in comments.

1 Comment

yes, I shoud have mention that, i perform a GC.collect(), i dispose every Bitmaps I uses and in the catch block I try it again after GC.WaitForPendingFinalizers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.