将图像读写到隔离存储中。

3
我在一些Windows Phone网站上看到了几种不同的将图像写入独立存储的方法,但我不确定哪种方法最适合用于相机应用程序或是否存在比其他方法更好的方法:
首先是从一个基本的相机应用程序帖子中得到的方法: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-Images)更加复杂。
   // 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个回答

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接