Save Camera Captures in Media Library for Windows Phone
Join the DZone community and get the full member experience.
Join For Free
cameracapturetask
chooser is used to capture photo using windows phone camera. to work with cameracapturetask , first you need add namespace:
then define a global variable:
in constructor of the page, you need to instantiate
cameracapturetask
and attach completed event handler.
next you need to show camera to user.
you can call show function anywhere as per your business requirement
however i am calling it on click event of a button as below,
now in the completed event of the
cameracapturetask we need to save the image in media library. to work
with medialibrary, you need to add reference of
microsoft.xna.framework.
after adding the reference add below namespace:
in completed event of cameracapturetask, make instance of
medialibrary
and call
savepicture
method as below:
as you see savepicture function takes
two input parameters. it takes name of picture as one input parameter
and picture stream as another. in this example we are saving picture
taken from camera. you may also save picture downloaded from services as
stream.
for your reference full source code is given as below:
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using microsoft.phone.controls; using microsoft.phone.tasks; using microsoft.xna.framework.media; namespace phoneapp17 { public partial class mainpage : phoneapplicationpage { // constructor cameracapturetask cameratask; public mainpage() { initializecomponent(); cameratask = new cameracapturetask(); cameratask.completed += new eventhandler<photoresult>(cameratask_completed); } void cameratask_completed(object sender, photoresult e) { if (e.taskresult == taskresult.ok) { medialibrary medialibrary = new medialibrary(); medialibrary.savepicture("givenameofimage", e.chosenphoto); } } private void btnshowcamera_click(object sender, routedeventargs e) { cameratask.show(); } } }
in this way you can save picture to media library. i hope this post is useful. thanks for reading.
Opinions expressed by DZone contributors are their own.
Comments