如何在隔离存储中保存位图



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

 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 ren2 appdata locallow microsoft Silverlightis vzvpufms .s4im0laonzr.til1snkhajster01es5wdoyfxd0n5rd2dls3ovyu4wcdig04zjx44hyaaafeaf

当我点击它,它给我的消息为"无效的图像"。你能告诉我我应该如何写代码,使我可以看到实际的图像保存在隔离存储后?

    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();
    }
}

我可以想到两个可行的选择。

    在上面的代码中,保存WriteableBitmap的Pixels数组。然后要恢复它,您将创建一个适当大小的WriteableBitmap,并将Pixels数组设置为存储的数据。

或者

  1. 使用HttpWebRequest或WebClient请求获取原始图像流并保存到IsolatedStorage。

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

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

相关内容

  • 没有找到相关文章

最新更新