none
Silverlight 如何对图片进行压缩 RRS feed

  • 问题

  • 最近项目需要上传图片到服务器,所以我想在上传之前先对图片进行压缩,我现在已经实现了wcf上传功能,不知道哪个兄弟能帮我解决下压缩的问题,代码如下

       /// <summary>
            /// WCF 文件上传
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void btnWCFUpload_Click(object sender, RoutedEventArgs e)
            {
                //选择本地文件对话框
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "(*.*)|*.*";
                //单选
                openFileDialog.Multiselect = false;

                if (openFileDialog.ShowDialog() == true)
                {
                    //所选上传文件信息
                    FileInfo fileInfo = openFileDialog.File;
             
                    uploadFile.Name = fileInfo.Name;
                    //文件内容
                    Stream stream = fileInfo.OpenRead();
                    uploadFile.Size = stream.Length / 1024.00;
                    uploadFile.Context = new List<byte[]>();

                    //读取指定大小的文件流内容到uploadFile.Context以便上传
                    int b;
                    while (stream.Position > -1 && stream.Position < stream.Length)
                    {
                        if (stream.Length - stream.Position >= 3000)
                        {
                            b = 3000;
                        }
                        else
                        {
                            b = (int)(stream.Length - stream.Position);
                        }

                        byte[] filebyte = new byte[b];
                        stream.Read(filebyte, 0, b);
                        uploadFile.Context.Add(filebyte);
                    }
                    stream.Close();
                    MainPage.myDataBase.ActionUpload(uploadFile.Name, uploadFile.Context[0], false).Completed
                        += new  EventHandler(ActionUpload_Completed);          
                }
            }
            void ActionUpload_Completed(object send ,EventArgs e)
            {
                    uploadFile.CompletedStatus += uploadFile.Context[0].Length / 1024.00;
                    uploadFile.Context.RemoveAt(0);
                    if (uploadFile.Context.Count == 0)
                    {
                        //btnWCFUpload.Content = "WCFUpload";
                        MessageBox.Show("文件上传成功!");
                    }
                    else
                    {
                        MainPage.myDataBase.ActionUpload(uploadFile.Name, uploadFile.Context[0], true).Completed
                        += new EventHandler(ActionUpload_Completed);
                    }

                }

    2010年8月31日 2:08

答案

全部回复

  • Silverlight本身不能够压缩的,我感觉你应该是需要在服务端压缩,如果使用WCF的话,你可以使用GZip进行压缩,函数代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Compression;
    using System.IO;
    
    //文件流 
    FileStream reader; 
    reader = File.Open("D:\\ddd.txt", FileMode.Open); 
    FileStream writer; 
    writer = File.Create("D:\\ddd.gz"); 
    
    //压缩相关的流 
    MemoryStream ms = new MemoryStream(); 
    GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress, true); 
    
    //往压缩流中写数据 
    byte[] sourceBuffer = new byte[reader.Length]; 
    
    
    reader.Read(sourceBuffer, 0, sourceBuffer.Length); 
    zipStream.Write(sourceBuffer, 0, sourceBuffer.Length); 
    
    //一定要在内存流读取之前关闭压缩流 
    zipStream.Close(); 
    zipStream.Dispose(); 
    
    //从内存流中读数据 
    ms.Position = 0; //注意,不要遗漏此句 
    byte[] destBuffer = new byte[ms.Length]; 
    //ms.Read(destBuffer, 0, destBuffer.Length); 
    
    byte[] header = new byte [10];
    ms.Read(header, 0, 10);
    header[3] = 8;    //表示包含文件名信息
    byte[] fielContent = new byte[ms.Length -10] ;
    ms.Read(fielContent, 0, fielContent.Length);
    
    string strfilename = "widebright的文件.txt"; //指定初始文件名
     
    //byte  [] filename=System.Text.Encoding.Convert(System.Text.Encoding.Default,
    //             // System.Text.Encoding.GetEncoding("ISO-8859-1"),
    //              System.Text.Encoding.Default,
    //              System.Text.Encoding.Default.GetBytes(strfilename));
    byte[] filename = System.Text.Encoding.Default.GetBytes(strfilename);
    
    writer.Write(header, 0, header.Length);
    writer.Write(filename, 0, filename.Length);
    writer.WriteByte(0); //文件名以0 字节结束
    writer.Write(fielContent, 0, fielContent.Length); 
    
    //关闭并释放内存流 
    ms.Close(); 
    ms.Dispose(); 
    
    //关闭并释放文件流 
    writer.Close(); 
    writer.Dispose(); 
    reader.Close(); 
    reader.Dispose(); 
    
    

    My blog: http://blog.csdn.net/dotfun http://dotfun.cnblogs.com

    My contact: QQ:372900288 E-mail:372900288@qq.com msn:sellnet007@hotmail.com


    《银光志--Silverlight 3.0开发详解与最佳实践》
    • 已标记为答案 xmuczq 2010年9月2日 8:19
    • 取消答案标记 xmuczq 2010年9月3日 2:06
    2010年8月31日 15:46
    版主
  • 你好,

    这里有个Silverlight的上传控件,已经实现了对图像缩放再上传的功能

    http://silverlightfileupld.codeplex.com/

    若需要zip压缩,你可以使用Silverlight版本的 SharpZipLib

    http://slsharpziplib.codeplex.com/

    若要对图像进行编码压缩,可以参见这篇文章

    http://programmerpayback.com/2010/01/21/use-silverlight-to-resize-images-and-increase-compression-before-uploading/

    其中使用的 sivlerlight jpeg encoder 是 FJCore

    http://code.google.com/p/fjcore/

    谢谢,


    Mog Liang
    • 已标记为答案 xmuczq 2010年9月2日 8:09
    2010年9月1日 3:58
  • 谢谢两位

    2010年9月2日 8:30