读写隔离存储图像

时间:2012-01-31 18:35:49

标签: silverlight image windows-phone-7 isolatedstorage

我在一些Windows Phone网站上遇到了几种不同的方法将图像写入隔离存储,但是我不确定哪种方法最适合用于相机应用程序,或者是否有一些比其他应用程序更好:< / p>

第一篇是关于基本相机应用程序的帖子:http://msdn.microsoft.com/en-us/library/hh202956(v=VS.92).aspx它从摄像机获取jpeg并将其直接写入隔离存储。

   void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
    string fileName = savedCounter + ".jpg";

    try
    {   // Write message to the UI thread.
        Deployment.Current.Dispatcher.BeginInvoke(delegate()
        {
            txtDebug.Text = "Captured image available, saving picture.";
        });

        // Save picture to the library camera roll.
        library.SavePictureToCameraRoll(fileName, e.ImageStream);

        // Write message to the UI thread.
        Deployment.Current.Dispatcher.BeginInvoke(delegate()
        {
            txtDebug.Text = "Picture has been saved to camera roll.";

        });

        // Set the position of the stream back to start
        e.ImageStream.Seek(0, SeekOrigin.Begin);

        // Save picture as JPEG to isolated storage.
        using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
            {
                // Initialize the buffer for 4KB disk pages.
                byte[] readBuffer = new byte[4096];
                int bytesRead = -1;

                // Copy the image to isolated storage. 
                while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    targetStream.Write(readBuffer, 0, bytesRead);
                }
            }
        }

        // Write message to the UI thread.
        Deployment.Current.Dispatcher.BeginInvoke(delegate()
        {
            txtDebug.Text = "Picture has been saved to isolated storage.";

        });
    }
    finally
    {
        // Close image stream
        e.ImageStream.Close();
    }

}

似乎使用4kb缓冲区,这样做有什么意义吗?看起来比将图像转换为位图然后使用另存为Jpeg方法的方法复杂一点(http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and -save-图像):

   // Create a filename for JPEG file in isolated storage.
        String tempJPEG = "logo.jpg";

        // Create virtual store and file stream. Check for duplicate tempJPEG files.
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            StreamResourceInfo sri = null;
            Uri uri = new Uri(tempJPEG, UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(sri.Stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }

如果您有其他方法,我也会感兴趣,谢谢!

1 个答案:

答案 0 :(得分:3)

认为你应该看看这个:

我相信我已经回答了这个并得到了+50赏金

SO Save image to Isolated Storaged using byte[]