c# - OutOfMemoryException when rendering TextBlock using WriteableBitmapEx -
i have fight outofmemoryexception
again.
i have code snippet render images using writeablebitmapex in windows phone background agent task (which have memory usage limit approach 10m).
the following works fine:
deployment.current.dispatcher.begininvoke(() => { var wbbg = bitmapfactory.new(0, 0); var bmp = bitmapfactory.new(0, 0); (int = 0; < 30; i++) { using (var iso = isolatedstoragefile.getuserstoreforapplication()) { wbbg = bitmapfactory.new(0, 0).fromcontent("assets/image" + + ".jpg"); wbbg.invalidate(); (int j = 0; j < 6; j++) { bmp = bitmapfactory.new(0, 0).fromcontent("assets/" + j + ".png"); bmp = bmp.resize(60, 60, writeablebitmapextensions.interpolation.bilinear); wbbg.blit(new rect(j * 65, 0, 60, 60), bmp, new rect(0, 0, 60, 60)); wbbg.invalidate(); } string filenamebg = "/shared/" + + ".jpg"; using (var stream = iso.createfile(filenamebg)) { wbbg.savejpeg(stream, 480, 800, 0, 85); stream.close(); } wbbg = null; gc.collect(); gc.waitforpendingfinalizers(); } } notifycomplete(); });
however, if add or change use textblock
in loop, fail in 2nd loop withoutofmemoryexception
:
deployment.current.dispatcher.begininvoke(() => { var wbbg = bitmapfactory.new(0, 0); textblock tb; (int = 0; < 30; i++) { using (var iso = isolatedstoragefile.getuserstoreforapplication()) { wbbg = bitmapfactory.new(0, 0).fromcontent("assets/image" + + ".jpg"); //the above line thrown outofmemoryexception in 2nd loop wbbg.invalidate(); (int j = 0; j < 6; j++) { tb = new textblock(){ text = j.tostring(), //fontsize = 13, //height = 20, //width = 240, //fontweight = system.windows.fontweights.bold, //horizontalalignment = system.windows.horizontalalignment.center, //foreground = new solidcolorbrush(colors.white) }; wbbg.render(tb, new translatetransform() { x = j*65, y = 350 }); wbbg.invalidate(); tb = null; } string filenamebg = "/shared/" + + ".jpg"; using (var stream = iso.createfile(filenamebg)) { wbbg.savejpeg(stream, 480, 800, 0, 85); stream.close(); } wbbg = null; gc.collect(); gc.waitforpendingfinalizers(); } } notifycomplete(); });
any idea why textblock
cause more memory usage?
furthermore, don't see there better method render text on images.
also textblock
not idisposable
.
well, that's opinions, wrong, i'd appreciate help, thank!
i don't know library have forgot release resources when aren't in use anymore try this:
(var = 0; < 30; i++) { using (var iso = isolatedstoragefile.getuserstoreforapplication()) { using (var wbbg = bitmapfactory.new(0, 0).fromcontent("assets/image" + + ".jpg")) { wbbg.invalidate(); (int j = 0; j < 6; j++) { var tb = new textblock() { text = j.tostring(), //fontsize = 13, //height = 20, //width = 240, //fontweight = system.windows.fontweights.bold, //horizontalalignment = system.windows.horizontalalignment.center, //foreground = new solidcolorbrush(colors.white) }; wbbg.render(tb, new translatetransform() { x = j * 65, y = 350 }); wbbg.invalidate(); } using (var stream = iso.createfile("/shared/" + + ".jpg")) { wbbg.savejpeg(stream, 480, 800, 0, 85); } } } }
Comments
Post a Comment