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.