Save the image to the mobile phone Image Library

Source: Internet
Author: User

Recently, due to project requirements, you need to save the image to the mobile phone image library. In the past, you only stored the image in the independent storage. I just learned about it ~

The front-end is just a simple image control. Save the button and then click it to check whether the image is saved to the mobile phone Image Library ~

<Images Height = "395"
Horizontalalignment = "left"
Margin = "6, 6, 0, 0"
Name = "image1"
Stretch = "fill"
Verticalalignment = "TOP"
Width = "444"
Source = "/savepicture; component/testimage.jpg"/>

<Button content = "save"
Height = "70"
Horizontalalignment = "center"
Margin = "-12,516,319, 0"
Name = "btnsave"
Verticalalignment = "TOP"
Width = "149" Click = "btnsave_click"/>

<Button content = "choosepic" Height = "72" horizontalalignment = "Left" margin = "206,516, 177 "name =" button1 "verticalignment =" TOP "width =" "Click =" button#click "/>

Background

 

Private void btnsave_click (Object sender, routedeventargs E)
{
// Create a file name for the JPEG file in isolated storage.
String tempjpeg = "tempjpeg ";

// Create a virtual store and file stream. Check for duplicate tempjpeg files.
VaR mystore = isolatedstoragefile. getuserstoreforapplication ();
If (mystore. fileexists (tempjpeg ))
{
Mystore. deletefile (tempjpeg );
}

Isolatedstoragefilestream myfilestream = mystore. createfile (tempjpeg );

// Create a stream out of the sample JPEG file.
// For [Application name] In the URI, use the project name that you entered
// In the previous steps. Also, testimage.jpg is an example;
// You must enter your JPEG file name if it is different.
Streamresourceinfo Sri = NULL;
Uri uri = new uri ("savepicture; component/testimage.jpg", urikind. Relative );
SRI = application. getresourcestream (URI );

// Create a new writeablebitmap object and set it to the JPEG stream.
Bitmapimage bitmap = new bitmapimage ();
Bitmap. createoptions = bitmapcreateoptions. None;
Bitmap. setsource (SRI. Stream );
Writeablebitmap WB = new writeablebitmap (Bitmap );

// Encode the writeablebitmap object to a jpeg stream.
WB. savejpeg (myfilestream, WB. pixelwidth, WB. pixelheight, 0, 85 );
Myfilestream. Close ();

// Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
Myfilestream = mystore. openfile (tempjpeg, filemode. Open, fileaccess. Read );

// Save the image to the camera roll or saved pictures album.
Medialibrary library = new medialibrary ();

Picture PIC = library. savepicture ("savedpicture.jpg", myfilestream );
MessageBox. Show ("image saved to saved pictures album ");

Myfilestream. Close ();

}

Private void button#click (Object sender, routedeventargs E)
{
Photochoosertask task = new photochoosertask ();
Task. Completed + = new eventhandler <photoresult> (task_completed );
Task. Show ();
}

Void task_completed (Object sender, photoresult E)
{

}

 

 

The main implementation is to save the image stream to the multi-media library.

Picture PIC = library. savepicture ("savedpicture.jpg", myfilestream );

 

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.