c# - Windows 8 - Save Image in XAML as local file ( inside Application folder ) -


in windows store application, displaying picture using image control in xaml. source of image control set, code wise using writeablebitmap. trying send image attachment in email. there easy way that? trying save image locally( within application ) , attach saved image email. not able save locally. appreciated.

here code.

bitmap = await writeablebitmaprenderextensions.render(datacanvas);
image.source = bitmap;

datacanvas canvas control consists of 2 images, 1 place above another. have place sunglass top of users face , display image in xaml. email image.

check out writeablebitmapsaveextensions class in winrt xaml toolkit savetofile() extension methods can use save writeablebitmap.

the core 1 this:

public static async task savetofile(     writeablebitmap writeablebitmap,     storagefile outputfile,     guid encoderid) {     stream stream = writeablebitmap.pixelbuffer.asstream();     byte[] pixels = new byte[(uint)stream.length];     await stream.readasync(pixels, 0, pixels.length);      using (var writestream = await outputfile.openasync(fileaccessmode.readwrite))     {         var encoder = await bitmapencoder.createasync(encoderid, writestream);         encoder.setpixeldata(             bitmappixelformat.bgra8,             bitmapalphamode.premultiplied,             (uint)writeablebitmap.pixelwidth,             (uint)writeablebitmap.pixelheight,             96,             96,             pixels);         await encoder.flushasync();          using (var outputstream = writestream.getoutputstreamat(0))         {             await outputstream.flushasync();         }     } } 

you can encoder id using overload of method:

public static async task<storagefile> savetofile(     writeablebitmap writeablebitmap,     storagefolder storagefolder,     string filename,     creationcollisionoption options = creationcollisionoption.replaceexisting) {     storagefile outputfile =         await storagefolder.createfileasync(             filename,             options);      guid encoderid;      var ext = path.getextension(filename);      if (new[] { ".bmp", ".dib" }.contains(ext))     {         encoderid = bitmapencoder.bmpencoderid;     }     else if (new[] { ".tiff", ".tif" }.contains(ext))     {         encoderid = bitmapencoder.tiffencoderid;     }     else if (new[] { ".gif" }.contains(ext))     {         encoderid = bitmapencoder.gifencoderid;     }     else if (new[] { ".jpg", ".jpeg", ".jpe", ".jfif", ".jif" }.contains(ext))     {         encoderid = bitmapencoder.jpegencoderid;     }     else if (new[] { ".hdp", ".jxr", ".wdp" }.contains(ext))     {         encoderid = bitmapencoder.jpegxrencoderid;     }     else //if (new [] {".png"}.contains(ext))     {         encoderid = bitmapencoder.pngencoderid;     }      await writeablebitmap.savetofile(outputfile, encoderid);      return outputfile; } 

Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -