c# - Saving a visual element to a file -


i'm trying save framework element file way:

async task savevisualelementtofile(frameworkelement element, storagefile file) {     element.width = element.actualwidth;     element.height = element.actualheight;     //string filename = "customphoto.jpg";     var rendertargetbitmap = new rendertargetbitmap();     await rendertargetbitmap.renderasync(element, (int)element.width, (int)element.height);     var pixels = await rendertargetbitmap.getpixelsasync();     using (irandomaccessstream stream = await file.openasync(fileaccessmode.readwrite))     {         var encoder = await         bitmapencoder.createasync(bitmapencoder.jpegencoderid, stream);         byte[] bytes = pixels.toarray();         encoder.setpixeldata(bitmappixelformat.bgra8,                              bitmapalphamode.ignore,                              (uint)element.width, (uint)element.height,                              96,                              96,                              bytes);          await encoder.flushasync();     } } 

but result not expected. here's example:

original image :

http://i.stack.imgur.com/z1xxd.png

saved image :

http://i.stack.imgur.com/o7ztu.jpg

what's wrong code?

your method quite ok, pixels got shifted due incorrect width/height. have use rendertargetbitmap.pixelheight instead of element.height. problematic code should this:

encoder.setpixeldata(bitmappixelformat.bgra8,                      bitmapalphamode.ignore,                      (uint)rendertargetbitmap.pixelwidth,                       (uint)rendertargetbitmap.pixelheight,                      96, 96, bytes); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -