c# - Any way to save an UIElement created programmatically in an image file? -


i'm trying save uielement created programmatically in jpg/png/bmp image in windows phone 8.1 (c#) application.

i'm using class rendertargetbitmap using method renderasync() works ui elements created in xaml code. when use on ui elements created directly in c# have exception: "system.argumentexception (value not fall within expected range.)"

am doing wrong or class doesn't allow rendering of uielement(s) created programmatically? there way on windows phone 8.1? thank you!

here's code use:

        private static async void rendertext(string text, int width, int height, int fontsize, string imagename)     {         rendertargetbitmap b = new rendertargetbitmap();          var canvas = new grid();          canvas.width = width;         canvas.height = height;          var background = new canvas();         background.height = width;         background.width = height;          solidcolorbrush backcolor = new solidcolorbrush(colors.red);         background.background = backcolor;          var textblock = new textblock();         textblock.text = text;         textblock.fontweight = fontweights.bold;         textblock.textalignment = textalignment.left;         textblock.horizontalalignment = horizontalalignment.center;         textblock.verticalalignment = verticalalignment.stretch;         textblock.margin = new thickness(35);         //textblock.width = b.pixelwidth - textblock.margin.left * 2;         textblock.textwrapping = textwrapping.wrap;         textblock.foreground = new solidcolorbrush(colors.white); //color of text on tile         textblock.fontsize = fontsize;          canvas.children.add(textblock);          await b.renderasync(background);         await b.renderasync(canvas);          // pixels          var pixelbuffer = await b.getpixelsasync();           // local folder.         storagefolder local = windows.storage.applicationdata.current.localfolder;          // create new folder name datafolder.         var datafolder = await local.createfolderasync("datafolder",             creationcollisionoption.openifexists);          storagefile file = await datafolder.createfileasync(imagename, creationcollisionoption.replaceexisting);           // encode image selected file on disk         using (var filestream = await file.openstreamforwriteasync())         {              var encoder = await bitmapencoder.createasync(bitmapencoder.pngencoderid, filestream.asrandomaccessstream());              encoder.setpixeldata(                 bitmappixelformat.bgra8,                 bitmapalphamode.ignore,                 (uint)b.pixelwidth,                 (uint)b.pixelheight,                 displayinformation.getforcurrentview().logicaldpi,                 displayinformation.getforcurrentview().logicaldpi,                 pixelbuffer.toarray());              await encoder.flushasync();         }     } 

it not working elements created in xaml these (as msdn says) in visualtree:

renders snapshot of uielement visual tree image source.

so method work if add elements example current page:

layoutroot.children.add(canvas); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -