windows phone开发之图片上传

///

 
/// 上传文件 
///
 
public class UploadFile 

    public UploadFile() 
    { } 
    ///  
    /// 根据文件名、文件类型与文件流实现化 
    ///
 
    /// 文件名 
    /// 文件数据流 
    public UploadFile(string fileName, Stream stream) 
    { 
        this.FileName = System.IO.Path.GetFileName(fileName); 
        this.ContentType = GetContentType(this.FileName); 
        this.FileStream = stream; 
    } 
 
    ///  
    /// 文件名称 
    ///
 
    public string FileName { get; private set; } 
 
    ///  
    /// 文件类型 
    ///
 
    public string ContentType { get; private set; } 
 
    ///  
    /// 本地文件路径 
    ///
 
    private string FilePath { get; set; } 
    ///  
    /// 文件数据流 
    ///
 
    public Stream FileStream { get; private set; } 
 
    ///  
    /// 将当前的文件数据写入到某个数据流中 
    ///
 
    ///  
    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); 
                } 
            } 
        } 
 
    } 
 
    ///  
    /// 根据文件扩展名获取文件类型 
    ///
 
    /// 文件名 
    ///  
    private static string GetContentType(string fileName) 
    { 
        var fileExt = System.IO.Path.GetExtension(fileName); 
        return GetCommonFileContentType(fileExt); 
    } 
    ///  
    /// 获取通用文件的文件类型 
    ///
 
    /// 文件扩展名.如".jpg",".gif"等 
    ///  
    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"; 
        } 
    } 
 
    ///  
    /// 压缩图片,只调整质量,不调整分辨率 
    ///
 
    /// 图片流 
    /// 质量1-100 
    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; 
    }  

然后是前台的调用
[csharp]
///

 
       /// 图片上传处理 
       ///
 
       ///  
       ///  
       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; 
           } 
       } 
       ///  
       /// 字节流转换byte 
       ///
 
       ///  
       ///  
       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)