Windows Phone开发之图片上传
2012-02-08 15:04
330 查看
首先是 后台类
然后是前台的调用
/// <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; }
相关文章推荐
- windows phone开发之图片上传
- windows phone开发之图片上传
- Windows Phone实用开发技巧(1):保存图片及加载图片
- Windows Phone实用开发技巧(39):WebBrowser引用独立存储空间中的图片
- Ios开发,图片上传
- iOS软件开发实现类似微信上传图片选择
- iOS 开发之--使用AFNetWorking3.1.0上传单张/多张图片
- android app开发中将表情图片上传到服务器并获取显示
- thinkphp微信开发之jssdk图片上传并下载到本地服务器
- uwp开发图片上传插件demo
- .NET开发微信小程序-生成二维码/上传图片到服务器
- JAVA微信公众号开发——上传图片,新增永久素材,群发
- Web图片上传插件开发文档-PHP
- Windows Phone 7开发遇到的事:保存、读取图片
- weex开发实战(8)--在weex页面实现图片上传
- XNA 通过图片播放动画,Windows Phone 游戏开发
- 微信公众平台开发[10]-开发模式-高级功能-将图片、视频、音频I内容上传到微信服务器并发送给用户
- 详细阐述Web开发中的图片上传问题
- Windows Phone 实用开发技巧(24):上传日志 推荐
- Windows phone手机开发之图片选择器,照相机功能,计时器,加速度,图片路径问题,页面间传值,随机产生数字