Development in Windows 8-converting image cloud storage and stream

Source: Internet
Author: User

About uploading and downloading images

Currently, it is found that the image cannot be named in Chinese (it may be related to the encoding method used by you, but you are not sure ). If the image path contains a Chinese name, the image data may be uploaded, but an exception may occur when it is downloaded. The obtained data is empty.

(1) You can obtain an image from a local device and upload the image as follows:

Fileopenpicker openpicker = new fileopenpicker () {// Add file filter filetypefilter = {". jpg ",". JPEG ",". PNG ",". BMP "}, // set the rendering view mode to thumbnail viewmode = pickerviewmode. thumbnail, // set the starting position when the file selector is opened. Set suggestedstartlocation = pickerlocationid in the Image Library. pictureslibrary}; // call picksinglefileasync storagefile file = await openpicker asynchronously. picksinglefileasync (); If (file! = NULL) {m_isresetuserhead = true; bitmapimage bitmap = new bitmapimage (); bitmap. decodepixelheight = 120; bitmap. decodepixelwidth = 120; using (irandomaccessstream stream = await file. openasync (fileaccessmode. read) {bitmap. setsource (Stream); If (m_imagestream = NULL) m_imagestream = new memorystream (); m_imagestream.position = 0; stream. asstream (). position = 0; stream. asstream (). copyto (m_imagestream);} // sets the local Avatar userheadimage. source = bitmap; // upload the Avatar to the cloud storage saveuserheadtocloud (m_imagestream, file. name, file. filetype );

However, for file. Name, you 'd better manually ensure it is an English character.

The process of converting stream to byte [] is as follows:

byte[] fileData = new byte[m_ImageStream.Length];m_ImageStream.Read(fileData, 0, fileData.Length);


(2) if it is a download, it can be implemented as follows:

Try {var streamreference = randomaccessstreamreference. createfromuri (New uri (URI); irandomaccessstream stream = await streamreference. openreadasync (); If (stream! = NULL) {bitmapimage image = new bitmapimage (); image. decodepixelheight = 120; image. decodepixelwidth = 120; image. setsource (Stream); userheadimage. source = image ;}} catch {// when a network connection error occurs, the Avatar cannot be updated}


When the URI path contains Chinese characters, randomaccessstreamreference. createfromuri throws an exception.

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.