Windows Phone development (23): cameracapturetask and photochoosertask

Source: Internet
Author: User

Both components belong to the selector, and they have many similarities. The most obvious one is that they are used to select images.

 

 

1. cameracapturetask selector.

 

It is used to start the camera. When you take a photo, the byte stream of the photo is automatically returned to the caller application. As mentioned above, the use and steps of the starter and selection are the same. This is also true for the cameracapturetask component. However, when processing a completed event, remember to use the dispatcher of the page class as much as possible. the begininvoke method is very likely to cause exceptions because it is insecure to directly access the UI element through asynchronous callback, but I am not saying absolutely.

 

<Grid> <br/> <grid. rowdefinitions> <br/> <rowdefinition Height = "*"/> <br/> <rowdefinition Height = "Auto"/> <br/> </grid. rowdefinitions> <br/> <image X: Name = "IMG" grid. row = "0" stretch = "Uniform" <br/> horizontalalignment = "stretch" <br/> verticalalignment = "stretch"/> <br/> <button X: name = "btncamera" grid. row = "1" <br/> content = "Start camera program" Click = "btncamera_click"/> <br/> </GRID> <br/>

 

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. net; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. documents; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. animation; <br/> using system. windows. shapes; <br/> using Microsoft. phone. controls; <br/> // introduce the following namespace. <Br/> using Microsoft. phone. tasks; <br/> using system. windows. media. imaging; </P> <p> namespace phoneapp1 <br/> {<br/> Public partial class mainpage: phoneapplicationpage <br/> {<br/> // step 1, declare local variables at the class level and instantiate them. <Br/> cameracapturetask mycamera = new cameracapturetask (); </P> <p> // constructor <br/> Public mainpage () <br/>{< br/> initializecomponent (); </P> <p> // Step 2: register the callback event in the page constructor. <br/> mycamera. completed + = new eventhandler <photoresult> (mycamera_completed); <br/>}</P> <p> private void btncamera_click (Object sender, routedeventargs E) <br/>{< br/> // Step 3: display components <br/> mycamera. show (); <br/>}</P> <p> // Step 4: process and return Result <br/> void mycamera_completed (Object sender, photoresult e) <br/>{< br/> // confirm whether the user has confirmed or canceled the operation. <Br/> If (E. taskresult = taskresult. OK) <br/>{< br/> // create an image from the returned stream <br/> bitmapimage BMP = new bitmapimage (); <br/> try <br/> {<br/> BMP. setsource (E. chosenphoto); <br/> // uses the image as the source of the image control. <Br/> // prevents the asynchronous callback from directly accessing the UI element. Therefore, use the begininvoke method. <Br/> dispatcher. begininvoke () => <br/>{< br/> This. IMG. source = BMP; <br/>}); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. message); <br/>}</P> <p >}< br/>}

 

Of course, you cannot take a photo in the simulator, but you can simulate it. That is to say, no matter what you take, the same photo is returned.

 

 

 

 

2. photochoosertask selector.

 

This selector already contains the cameracapturetask function. Of course, it is mainly used to select images.

1. Set the showcamera attribute to show the buttons that allow users to start the camera;

2. pixelheight: select the cropped height of the image;

3. The pixelwidth attribute is the same as the preceding attribute.

After the photo is selected, it is returned as a stream. The chosenphoto attribute of the parameter photoresult of the completed event is obtained.

 

<Grid> <br/> <grid. rowdefinitions> <br/> <rowdefinition Height = "*"/> <br/> <rowdefinition Height = "Auto"/> <br/> </grid. rowdefinitions> <br/> <image X: Name = "IMG" stretch = "Uniform" horizontalalignment = "stretch" verticalignment = "stretch" grid. row = "0"/> <br/> <grid. row = "1"> <br/> <grid. columndefinitions> <br/> <columndefinition width = "Auto "/> <br/> <columndefinition width = "Auto"/> <br/> </grid. columndefinitions> <br/> <grid. rowdefinitions> <br/> <rowdefinition Height = "Auto"/> <br/> <rowdefinition Height = "Auto"/> <br/> </grid. rowdefinitions> <br/> <textblock grid. column = "0" grid. row = "0" text = "height:"/> <br/> <textblock grid. column = "2" grid. row = "0" text = "width:"/> <br/> <textbox X: Name = "txtheight" grid. column = "1" <br/> grid. row = "0" width = "160" Height = "Auto" fontsize = "20"> <br/> <textbox. inputscope> <br/> <inputscopename namevalue = "Number"/> <br/> </inputscope> <br/> </textbox. inputscope> <br/> </textbox> <br/> <textbox X: Name = "txtwidth" grid. column = "3" <br/> grid. row = "0" width = "160" Height = "Auto" fontsize = "20"> <br/> <textbox. inputscope> <br/> <inputscopename namevalue = "Number"/> <br/> </inputscope> <br/> </textbox. inputscope> <br/> </textbox> <br/> <checkbox X: Name = "chkshowcamera" <br/> grid. row = "1" grid. columnspan = "2" <br/> content = "show camera startup"/> <br/> <button X: Name = "btnshow" <br/> grid. column = "2" grid. row = "1" <br/> grid. columnspan = "2" <br/> content = "select image... "<br/> margin =" 5 "<br/> click =" btnshow_click "/> <br/> </GRID> <br/>
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. net; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. documents; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. animation; <br/> using system. windows. shapes; <br/> using Microsoft. phone. controls; <br/> // <br/> Using Mi Crosoft. phone. tasks; <br/> using system. windows. media. imaging; </P> <p> namespace phoneapp1 <br/> {<br/> Public partial class page1: phoneapplicationpage <br/>{< br/> photochoosertask PTC = new photochoosertask (); </P> <p> Public page1 () <br/>{< br/> initializecomponent (); </P> <p> PTC. completed + = new eventhandler <photoresult> (ptc_completed); <br/>}</P> <p> void ptc_completed (Object sender, photoresul T e) <br/>{< br/> If (E. taskresult = taskresult. OK) <br/>{< br/> bitmapimage BMP = new bitmapimage (); <br/> try <br/>{< br/> BMP. setsource (E. chosenphoto); <br/> dispatcher. begininvoke () =>{ <br/> This. IMG. source = BMP; <br/>}); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. message); <br/>}</P> <p> private void btnshow_click (Object sender, routedeventarg S e) <br/>{< br/> // set relevant properties <br/> PTC. pixelheight = int. parse (txtheight. text); <br/> PTC. pixelwidth = int. parse (txtwidth. text); <br/> PTC. showcamera = This. chkshowcamera. ischecked. hasvalue? Chkshowcamera. ischecked. Value: false; </P> <p> PTC. Show (); <br/>}< br/>}

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.