当前位置:编程学习 > wap >>

windows phone开发之图片上传

首先是 后台类

[csharp]
/// <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); 
   &nbs

补充:移动开发 , Windows Phone ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,