Upload images for Windows Phone development

Source: Internet
Author: User

The first is the background class

/// <Summary> /// upload a file /// </Summary> public class uploadfile {public uploadfile () {}/// <summary> /// implement the file name based on the file name, file type, and file stream /// </Summary> /// <Param name = "FILENAME"> file name </param> // <Param name = "stream"> file data stream </param> Public uploadfile (string filename, stream stream) {This. filename = system. io. path. getfilename (filename); this. contenttype = getcontenttype (this. filename); this. filestream = stream ;} /// <Summary> /// file name /// </Summary> Public String filename {Get; private set ;} /// <summary> /// file type /// </Summary> Public String contenttype {Get; private set ;} /// <summary> /// local file path /// </Summary> private string filepath {Get; set ;} /// <summary> // file data stream // </Summary> Public stream filestream {Get; private set ;} /// <summary> /// write the current file data to a data stream // </Summary> /// <Param name = "Stream"> </param> Public void writeto (Stream stream) {byte [] buffer = new byte [512]; int size = 0; If (this. filestream! = NULL) {// write the file stream while (size = This. filestream. read (buffer, 0, buffer. length)> 0) {stream. write (buffer, 0, size) ;}} if (! String. isnullorempty (this. filepath) & file. exists (this. filepath) {// write using (system. io. filestream reader = new filestream (this. filepath, filemode. open, fileaccess. read) {While (size = reader. read (buffer, 0, buffer. length)> 0) {stream. write (buffer, 0, size );}}}} /// <summary> /// obtain the file type based on the file extension /// </Summary> /// <Param name = "FILENAME"> file name </param> // /<returns> </returns> Private Static string getcontenttype (string filename) {var fileext = system. io. path. getextension (filename); Return getcommonfilecontenttype (fileext );} /// <summary> /// obtain the file type of the common file /// </Summary> /// <Param name = "fileext"> file extension. for example ". jpg ",". GIF ", etc. </param> /// <returns> </returns> Private Static string getcommonfilecontenttype (string fileext) {Switch (fileext) {Case ". jpg ": Case ". JPEG ": Return" image/JPEG "; case ". GIF ": Return" image/GIF "; case ". BMP ": Return" image/BMP "; case ". PNG ": Return" image/PNG "; default: Return" application/octetstream ";}/// <summary> // compress the image and only adjust the quality, do not adjust the resolution /// </Summary> /// <Param name = "Soucre"> image stream </param> /// <Param name = "quality"> quality 1 -100 </param> Public static stream compression (Stream soucres) {var Quality = 80; soucret. seek (0, seekorigin. begin); var P = Quality/100.0; var writeablebitmap = picturedecoder. decodejpeg (soucres); var width = writeablebitmap. pixelwidth * P; var Height = writeablebitmap. pixelheight * P; var outstream = new memorystream (); writeablebitmap. savejpeg (outstream, (INT) width, (INT) height, 0, quality); outstream. seek (0, seekorigin. begin); return outstream ;}}

Then there is the call from the front-end

/// <Summary> /// Image Upload processing /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void photochoosertask_completed (Object sender, photoresult e) {try {If (E. chosenphoto! = NULL) {stream S = uploadfile. compression (E. chosenphoto); bytepic = streamtobytes (s); encoding myencoding = encoding. getencoding ("UTF-8"); strpic = convert. tobase64string (bytepic); existspic = true ;}} catch (exception) {Throw ;}} /// <summary> // byte stream conversion // </Summary> /// <Param name = "stream"> </param> // <returns> </returns> Public byte [] streamtobytes (Stream) {byte [] bytes = new byte [stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the stream. seek (0, seekorigin. begin); return bytes ;}

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.