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(picna开发者_如何学JAVAme)
'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()
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).
精彩评论