Tuesday, 15 September 2015

image - solution for OutOfMemoryExecption in silverlight -



image - solution for OutOfMemoryExecption in silverlight -

i'm creating application in silverlight saves images in isolated storage. managed save images in isolated storage i'm having problem loading , displaying image.

here code:

public partial class mainpage : usercontrol { private const string imagename = "google1.png"; public mainpage() { initializecomponent(); } private void button1_click(object sender, routedeventargs e) { writeablebitmap bitmap = new writeablebitmap(saveimage, new transformgroup()); loadedimage.source = bitmap; imagetostore(savebuffer(bitmap), imagename); messagebox.show("saved"); } public void imagetostore(byte[] buffer, string filename) { using (isolatedstoragefile iso = isolatedstoragefile.getuserstoreforapplication()) { isolatedstoragefilestream s = new isolatedstoragefilestream(filename, filemode.create, iso); int64 freespace = iso.availablefreespace; int64 needspace = 20971520; // 20 mb in bytes if (freespace < needspace) { if (!iso.increasequotato(iso.quota + needspace)) { messagebox.show("user rejected increment spacerequest"); } else { messagebox.show("space increased"); } } using (streamwriter author = new streamwriter(s)) { writer.write(buffer); } } } private static byte[] savebuffer(writeablebitmap bitmap) { long matrixsize = bitmap.pixelwidth * bitmap.pixelheight; long bytesize = matrixsize * 4 + 4; byte[] retval = new byte[bytesize]; long bufferpos = 0; retval[bufferpos++] = (byte)((bitmap.pixelwidth / 256) & 0xff); retval[bufferpos++] = (byte)((bitmap.pixelwidth % 256) & 0xff); retval[bufferpos++] = (byte)((bitmap.pixelheight / 256) & 0xff); retval[bufferpos++] = (byte)((bitmap.pixelheight % 256) & 0xff); homecoming retval; } private void button2_click(object sender, routedeventargs e) { byte[] buffer = _loadifexists(imagename); loadedimage.source = _getimage(buffer); messagebox.show("loaded"); } private static byte[] _loadifexists(string filename) { byte[] retval; using (isolatedstoragefile iso = isolatedstoragefile.getuserstoreforapplication()) { if (iso.fileexists(filename)) { using (isolatedstoragefilestream stream = iso.openfile(filename, filemode.open)) { retval = new byte[stream.length]; stream.read(retval, 0, retval.length); stream.close(); } } else { retval = new byte[0]; } } homecoming retval; } private static writeablebitmap _getimage(byte[] buffer) { int width = buffer[0] * 256 + buffer[1]; int height = buffer[2] * 256 + buffer[3]; long matrixsize = width * height; //this section exception of type 'system.outofmemoryexception' thrown. writeablebitmap retval = new writeablebitmap(width, height); int bufferpos = 4; (int matrixpos = 0; matrixpos < matrixsize; matrixpos++) { int pixel = buffer[bufferpos++]; pixel = pixel << 8 | buffer[bufferpos++]; pixel = pixel << 8 | buffer[bufferpos++]; pixel = pixel << 8 | buffer[bufferpos++]; retval.pixels[matrixpos] = pixel; } homecoming retval; }}}

hope guys can help me. lot.

basically, utilize silverlight manage image not easy task.

whatever powerfulness of computer running application, anyway limited browser, security limit ram , processor dedicated application. (it depend of version of browser, around 1go of used ram).

the solution otpimize memory management (always tricky in managed language...):

try avoid new instruction (reuse maximum of object)

as not need object anymore, set pointer null (to maintain free collect garbage collector)

in lastly option, seek phone call gc.collect() in strategic place (but carefull that, performance dramatically decreased if phone call often)

image silverlight out-of-memory

No comments:

Post a Comment