Windows Phone開發之圖片上傳

來源:互聯網
上載者:User

首先是 後台類

        /// <summary>        /// 上傳檔案        /// </summary>        public class UploadFile        {            public UploadFile()            { }            /// <summary>            /// 根據檔案名稱、檔案類型與檔案流實現化            /// </summary>            /// <param name="fileName">檔案名稱</param>            /// <param name="stream">檔案資料流</param>            public UploadFile(string fileName, Stream stream)            {                this.FileName = System.IO.Path.GetFileName(fileName);                this.ContentType = GetContentType(this.FileName);                this.FileStream = stream;            }            /// <summary>            /// 檔案名稱            /// </summary>            public string FileName { get; private set; }            /// <summary>            /// 檔案類型            /// </summary>            public string ContentType { get; private set; }            /// <summary>            /// 本地檔案路徑            /// </summary>            private string FilePath { get; set; }            /// <summary>            /// 檔案資料流            /// </summary>            public Stream FileStream { get; private set; }            /// <summary>            /// 將當前的檔案資料寫入到某個資料流中            /// </summary>            /// <param name="stream"></param>            public void WriteTo(Stream stream)            {                byte[] buffer = new byte[512];                int size = 0;                if (this.FileStream != null)                {                    //寫入檔案流                    while ((size = this.FileStream.Read(buffer, 0, buffer.Length)) > 0)                    {                        stream.Write(buffer, 0, size);                    }                }                if (!string.IsNullOrEmpty(this.FilePath)                    && File.Exists(this.FilePath))                {                    //寫入本地檔案流                    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>            /// 根據副檔名擷取檔案類型            /// </summary>            /// <param name="fileName">檔案名稱</param>            /// <returns></returns>            private static string GetContentType(string fileName)            {                var fileExt = System.IO.Path.GetExtension(fileName);                return GetCommonFileContentType(fileExt);            }            /// <summary>            /// 擷取通用檔案的檔案類型            /// </summary>            /// <param name="fileExt">副檔名.如".jpg",".gif"等</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>            /// 壓縮圖片,只調整品質,不調整解析度            /// </summary>            /// <param name="soucre">圖片流</param>            /// <param name="quality">品質1-100</param>            public static Stream Compression(Stream soucre)            {                var quality = 80;                soucre.Seek(0, SeekOrigin.Begin);                var p = quality / 100.0;                var writeableBitmap = PictureDecoder.DecodeJpeg(soucre);                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;            }         }

然後是前台的調用

 /// <summary>        /// 圖片上傳處理        /// </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        /// </summary>        /// <param name="stream"></param>        /// <returns></returns>        public byte[] StreamToBytes(Stream stream)        {            byte[] bytes = new byte[stream.Length];            stream.Read(bytes, 0, bytes.Length);            // 設定當前流的位置為流的開始             stream.Seek(0, SeekOrigin.Begin);            return bytes;        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.