New Fashion Windows 8 Development (37): Getting and setting user portraits

Source: Internet
Author: User

To obtain user information, we mainly use the userinformation class in the windows. system. USERPROFILE namespace. This guy is a static class and you should know how to use it.

It is easy to obtain TMD, such as a user name. You only need to call the corresponding method to complete the process. In today's example, we will get and set the user's profile picture.

 

Call the getaccountpicture method to obtain the user avatar, and set the user avatar to call setaccountpictureasync.

 

Create an app project and divide the home page into the left and right parts based on the new "golden line". We can get and display the user avatar on the left and set the user avatar on the right.

<Page X: class = "app1.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app1" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. columndefinitions> <columndefinition/> </grid. columndefinitions> <grid. column = "0" margin = "15"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <button grid. row = "0" margin = "," horizontalalignment = "center" content = "get user avatar" Click = "ongetpic_click"/> <image grid. row = "1" X: Name = "imgshow" stretch = "Uniform"/> </GRID> <grid. column = "1" margin = "15"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <stackpanel grid. row = "0" margin = "0, 18, 0, 15 "horizontalalignment =" center "orientation =" horizontal "> <button content =" open image "Click =" onopenpic_click "/> <button content =" set user profile picture "Click =" onsetpic_click "/> </stackpanel> <image X: name = "imgpreview" grid. row = "1" stretch = "Uniform"/> </GRID> </Page>

 

SwitchCodeView to introduce the following namespace:

 
Using Windows. system. USERPROFILE; using Windows. Storage; using Windows. Storage. pickers; using Windows. Storage. streams; using Windows. UI. XAML. Media. imaging;

 

The following code displays the user profile picture.

 
Private async void ongetpic_click (Object sender, routedeventargs e) {// obtain the user profile var imagefile = userinformation. getaccountpicture (accountpicturekind. largeimage); Using (irandomaccessstream = await imagefile. openasync (fileaccessmode. read) {bitmapimage BMP = new bitmapimage (); BMP. setsource (Stream); this. imgshow. source = BMP ;}}

The following two sections of Code show that the first section is to open and preview the image, and the second section is to set the user's profile picture.

Private async void onopenpic_click (Object sender, routedeventargs e) {// open an image fileopenpicker picker = new fileopenpicker (); picker. filetypefilter. add (". jpg "); picker. filetypefilter. add (". PNG "); picker. filetypefilter. add (". JPEG "); picker. suggestedstartlocation = pickerlocationid. pictureslibrary; var file = await picker. picksinglefileasync (); If (file! = NULL) {// display image preview bitmapimage BMP = new bitmapimage (); BMP. setsource (await file. openasync (fileaccessmode. read); this. imgpreview. source = BMP; // to facilitate later use, assign the file to the tag attribute this. imgpreview. tag = file ;}} private async void onsetpic_click (Object sender, routedeventargs e) {storagefile imgfile = This. imgpreview. tag as storagefile; If (imgfile = NULL) return; var result = await userinformation. setaccoun Tpictureasync (imgfile); windows. UI. popups. messagedialog DLG = NULL; If (result = setaccountpictureresult. success) {DLG = new windows. UI. popups. messagedialog ("operation successful. ");} Else {DLG = new windows. UI. popups. messagedialog (" operation failed. ");} Await DLG. showasync ();}

Open the inventory file, switch to the "Declaration" tab, and select "account image provision" from the drop-down list.Program", Click" add. Save.

 

After adding this extension, the system will not send a prompt when setting the user image.

Now you can run it.

A. click the button on the left side of the page to display the current user's profile picture.

 

B. Set the user image on the right side of the page.

 

Enable system settings to verify whether the setting is successful.

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.