Silverlight 如何在独立存储中保存bitmapimage?

Silverlight 如何在独立存储中保存bitmapimage?,silverlight,isolatedstorage,bitmapimage,sharepoint-clientobject,Silverlight,Isolatedstorage,Bitmapimage,Sharepoint Clientobject,我正在使用客户机对象模型开发silverlight web部件。我正在从sharepoint server获取位图图像。现在我想把这个位图图像保存在独立的存储器中。所以我使用下面的代码 WriteableBitmap wb = new WriteableBitmap(attachments); using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { usin

我正在使用客户机对象模型开发silverlight web部件。我正在从sharepoint server获取位图图像。现在我想把这个位图图像保存在独立的存储器中。所以我使用下面的代码

 WriteableBitmap wb = new WriteableBitmap(attachments);

 using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
 {
     using (IsolatedStorageFileStream isoStream =
     new IsolatedStorageFileStream("abcd1.jpg", FileMode.Create, isoFile))
     {
         using (StreamWriter sw = new StreamWriter(isoStream))
         {
             sw.Write(wb.ToByteArray());
         }
     }
 }
现在,我看到保存的图像位于位置C:\Users\Rent2\AppData\LocalLow\Microsoft\Silverlight\is\vzvpufsm.s4i\m0laonzr.til\1\s\nkhajster01es5wdoyfxd0n5rd2dls3ovyu4wcdig04zjx44hyaafea\f


当我点击它时,它给我的信息是“无效图像”。您能告诉我如何编写代码,以便在将实际图像保存到独立存储中后可以看到它吗?

我可以在这里想出两个可行的选项

  • 在上面的代码中,保存WriteableBitmap的像素数组。然后要恢复它,您需要创建一个大小合适的可写位图,并将像素数组设置为存储的数据
  • -或-

  • 使用HttpWebRequest或WebClient请求获取原始图像流并将其保存到IsolatedStorage

  • 这两种方法各有优缺点,在第一种情况下,数据未压缩,将在独立存储中占用更多空间,您将无法从Silverlight之外的磁盘打开映像,类似于上面的问题。对于第二个选项,如果映像位于与Silverlight XAP文件不同的服务器上,并且实现起来有点复杂,则可能会遇到跨域问题。

    我可以在这里想出两个可行的选项

  • 在上面的代码中,保存WriteableBitmap的像素数组。然后要恢复它,您需要创建一个大小合适的可写位图,并将像素数组设置为存储的数据
  • -或-

  • 使用HttpWebRequest或WebClient请求获取原始图像流并将其保存到IsolatedStorage

  • 这两种方法各有优缺点,在第一种情况下,数据未压缩,将在独立存储中占用更多空间,您将无法从Silverlight之外的磁盘打开映像,类似于上面的问题。对于第二个选项,如果图像位于与Silverlight XAP文件不同的服务器上,并且实现起来有点复杂,则可能会遇到跨域问题。

    基本问题是,您将解码的RGBA像素数组存储为JPEG文件,但它们不同。您的代码基本上是正确的:您不能使用此方法将其存储为JPEG文件。但是,如果您碰巧知道图像是(比如)800x600,您可以创建一个800x600 WriteableBitmap,然后将Pixels属性设置为检索到的流的字节数组。或者,如果在检索时不知道维度,可以将维度存储为流中的前两个整数(在将像素写入流之前),并在读取文件时读取维度。实际上,您可以创建自己的非常简单的.bmp格式。

    基本问题是,您将解码后的RGBA像素数组存储为JPEG文件,但它们不同。您的代码基本上是正确的:您不能使用此方法将其存储为JPEG文件。但是,如果您碰巧知道图像是(比如)800x600,您可以创建一个800x600 WriteableBitmap,然后将Pixels属性设置为检索到的流的字节数组。或者,如果在检索时不知道维度,可以将维度存储为流中的前两个整数(在将像素写入流之前),并在读取文件时读取维度。您实际上是在创建自己的非常简单的.bmp格式。

    private void zipFile()
    
        private void zipFile()
                {     
                    context = Microsoft.SharePoint.Client.ClientContext.Current;
                    Microsoft.SharePoint.Client.File.OpenBinaryDirect(
                            context,
                            @"/TemplateInvoice/" + App.templateFileName + ".xlsx", successFile, FailFile);                  
                }
    
        private void successFile(object sender, OpenBinarySucceededEventArgs args)
                {
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // Obtain the isolated storage for an application.
                        try
                        {
                            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                Stream strm = args.Stream;
    
                                using (var isoStream = store.OpenFile(App.templateFileName + ".zip", FileMode.OpenOrCreate))
                                {
                                    // Read the resource file into a byte array.
                                    bytes = new byte[strm.Length];
                                    int numBytesToRead = (int)strm.Length;
                                    int numBytesRead = 0;
                                    while (numBytesToRead > 0)
                                    {
                                        // Read may return anything from 0 to numBytesToRead.
                                        int n = strm.Read(bytes, numBytesRead, numBytesToRead);
                                        // The end of the file is reached.
                                        if (n == 0)
                                            break;
                                        numBytesRead += n;
                                        numBytesToRead -= n;
                                    }
    
    
                                    numBytesToRead = bytes.Length;
    
                                    // Write the byte array to the IsolatedStorageFileStream.
                                    isoStream.Write(bytes, 0, numBytesToRead);
                                    //isoStream.Dispose();
                                }
    
                                strm.Close();
    
                                string path = App.templateFileName + ".zip";
                                ZipHelp.UnZip(path, System.IO.Path.GetDirectoryName(path), 4096);
    
                                replaceFileContent();
    
                                string rootDirectory = System.Windows.Browser.HttpUtility.UrlDecode(path);
                                string fileName = ZipHelp.Zip(rootDirectory.Replace(".zip", ""), invoice);
    
                                //string filename = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "Invoice1.xlsx";
    
                                // Read the resource file into a byte array.
                                using (var stream = store.OpenFile(fileName, FileMode.Open))
                                {
    
                                    bytes = new byte[stream.Length];
                                    int numBytesToRead = (int)stream.Length;
                                    int numBytesRead = 0;
                                    while (numBytesToRead > 0)
                                    {
                                        // Read may return anything from 0 to numBytesToRead.
                                        int n = stream.Read(bytes, numBytesRead, numBytesToRead);
                                        // The end of the file is reached.
                                        if (n == 0)
                                            break;
                                        numBytesRead += n;
                                        numBytesToRead -= n;
                                    }
    
    
                                    InvoiceTemplete invoiceTemplate = new InvoiceTemplete(fileName, bytes, SetMessage);
                                    invoiceTemplate.AddDocument(invoiceTemplate);
                                }
    
                            }
    
                            //mark rows as billed
                            foreach (var item in PrivatePayList)
                            {
                                MedwaiverViewModel MedwaiverViewModelObj = new MedwaiverViewModel();
                                MedwaiverViewModelObj.ChangeBillingStatus(item.ListItemId, "Billed");
    
                                if (MedwaiverTimeLogList != null)
                                {
                                    MedwaiverTimeLogList.Remove(item);
                                }
    
                                if (ClientSpecificTimeLogList != null)
                                {
                                    ClientSpecificTimeLogList.Remove(item);
                                }
    
                                if (rangeBoundTimeLogListForDate != null)
                                {
                                    rangeBoundTimeLogListForDate.Remove(item);
                                }
    
                                if (vRangeBoundTimeLogListForDateAndClient != null)
                                {
                                    vRangeBoundTimeLogListForDateAndClient.Remove(item);
                                }
    
                            }
    
                        }
                        catch (IsolatedStorageException isx)
                        {
                            MessageBox.Show(isx.Message);
                        }
                    });
                }
    
         private void FailFile(object sender, OpenBinaryFailedEventArgs e)
                {
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Fail");
                    });
                }
    
    
    //The following class zip and unzip the file
    
    class ZipHelp
        {
            static List<string> folderPathList = new List<string>();      
    
            public static string Zip(string rootDirectory, string fileName)
            {
                byte[] buffer;
    
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    IsolatedStorageFileStream zipFileStream = store.CreateFile(fileName + ".xlsx");
                    ZipOutputStream zipOutStream = new ZipOutputStream(zipFileStream);
                    zipOutStream.UseZip64 = UseZip64.Off;
                    //zipOutStream.CanPatchEntries
    
                    foreach (var item in folderPathList)
                    {
                        string entryName = "";
    
                        buffer = new byte[4096];
    
                        if (item.Substring(0,1) == @"/")
                        {
                            //removes leading /
                            entryName = item.Substring(1);
                        }
                        else
                        {
                            entryName = item;
                        }
    
                        ZipEntry entry = new ZipEntry(entryName);
                        //entry.CompressionMethod = CompressionMethod.Deflated;
                        //entry.
                        entry.IsZip64Forced();
                        //entry.IsDirectory
    
    
    
                        zipOutStream.PutNextEntry(entry);
    
    
                        using (IsolatedStorageFileStream stream = store.OpenFile(rootDirectory + @"\" + item, FileMode.Open))
                        {
    
    
    int size;
                            do
                            {
                                size = stream.Read(buffer, 0, buffer.Length);
                                zipOutStream.Write(buffer, 0, size);
                            } while (size > 0);
    
                        stream.Close();
                    }
                }
    
                zipOutStream.Close();
                zipFileStream.Close();
            }
    
            return fileName + ".xlsx";
    
    
            //string[] directories = GetLocationTypes();
    
            //zip();
    
            //string[] filenames = Directory.GetFiles(directories);
    
            //using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            //{                
            //    using (var isoStream = store.OpenFile("@" + rootDirectory, FileMode.OpenOrCreate))
            //    {
            //        //foreach(string dir in 
            //    }
            //}
        }       
    
        private static string[] GetLocationTypes()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return store.GetDirectoryNames();
            }
    
        }
    
        /// <summary>
        /// UnZip a file
        /// </summary>
        /// <param name="SrcFile">source file path</param>
        /// <param name="DstFile">unzipped file path</param>
        /// <param name="BufferSize">buffer to use</param>
        public static void UnZip(string SrcFile, string DstFile, int BufferSize)
        {
            folderPathList.Clear();
    
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            FileStream fileStreamIn = store.OpenFile(SrcFile, FileMode.Open, FileAccess.Read);
    
            ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
            string rootDirectory = System.Windows.Browser.HttpUtility.UrlDecode(SrcFile);
            rootDirectory = rootDirectory.Replace(".zip", "");
            store.CreateDirectory(rootDirectory);
    
            while (true)
            {
                ZipEntry entry = zipInStream.GetNextEntry();
    
                if (entry == null)
                    break;
    
                if (entry.Name.Contains("/"))
                {
                    string[] folders = entry.Name.Split('/');
    
                    string lastElement = folders[folders.Length - 1];
                    var folderList = new List<string>(folders);
                    folderList.RemoveAt(folders.Length - 1);
                    folders = folderList.ToArray();
    
                    string folderPath = "";
                    foreach (string str in folders)
                    {
                        folderPath = folderPath + "/" + str;
                        if (!store.DirectoryExists(rootDirectory + "/" + folderPath))
                        {
                            store.CreateDirectory(rootDirectory + "/" + folderPath);
                        }
                    }
    
                    folderPath = folderPath + "/" + lastElement;
                    writeToFile(BufferSize, fileStreamIn, zipInStream, rootDirectory, folderPath);
    
                }
                else
                {
                    writeToFile(BufferSize, fileStreamIn, zipInStream, rootDirectory, entry.Name);
                }
    
            }
    
            zipInStream.Close();
            fileStreamIn.Close();
    
        }
    
        private static void writeToFile(int BufferSize, FileStream fileStreamIn, ZipInputStream zipInStream, string rootDirectory, string folderPath)
        {
            IsolatedStorageFile store1 = IsolatedStorageFile.GetUserStoreForApplication();
            FileStream fileStreamOut = store1.OpenFile(rootDirectory + "/" + folderPath, FileMode.Create, FileAccess.Write);
    
            folderPathList.Add(folderPath);
    
            int size;
            byte[] buffer = new byte[BufferSize];
            do
            {
                size = zipInStream.Read(buffer, 0, buffer.Length);
                fileStreamOut.Write(buffer, 0, size);
            } while (size > 0);
    
    
            fileStreamOut.Close();
    
        }
    }
    
    { context=Microsoft.SharePoint.Client.ClientContext.Current; Microsoft.SharePoint.Client.File.OpenBinaryDirect( 上下文 @“/TemplateInvoice/”+App.templateFileName+“.xlsx”、成功文件、失败文件); } 私有void successFile(对象发送方,OpenBinaryScceedeEventArgs) { System.Windows.Deployment.Current.Dispatcher.BeginInvoke(()=> { //获取应用程序的隔离存储。 尝试 { 使用(var store=IsolatedStorageFile.GetUserStoreForApplication()) { Stream strm=args.Stream; 使用(var isoStream=store.OpenFile(App.templateFileName+“.zip”、FileMode.OpenOrCreate)) { //将资源文件读入字节数组。 字节=新字节[strm.Length]; int numBytesToRead=(int)strm.Length; int numBytesRead=0; 而(numBytesToRead>0) { //Read可能返回0到numBytesToRead之间的任何内容。 int n=strm.Read(字节,numBytesRead,numBytesToRead); //到达文件的末尾。 如果(n==0) 打破 numBytesRead+=n; numBytesToRead-=n; } numBytesToRead=字节数。长度; //将字节数组写入IsolatedStorageFileStream。 写入(字节,0,numBytesToRead); //Dispose(); } strm.Close(); 字符串路径=App.templateFileName+“.zip”; 解压缩(路径,System.IO.path.GetDirectoryNa