0

When i use this function to save image(fetch from net) to IsolatedStorage, i found the file size is larger than that i save from the webbrowse.

public static bool CreateImageFile(string filePath, BitmapImage bitmapImage)
{
        //StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));

        using (isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            string directoryName = System.IO.Path.GetDirectoryName(filePath);
            if (!string.IsNullOrEmpty(directoryName) && !isolatedStorage.DirectoryExists(directoryName))
            {
                isolatedStorage.CreateDirectory(directoryName);
            }

            if (isolatedStorage.FileExists(filePath))
            {
                isolatedStorage.DeleteFile(filePath);
            }

            //bitmapImage

            using (IsolatedStorageFileStream fileStream = isolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write))
            {
                bitmapImage.CreateOptions = BitmapCreateOptions.None;
                WriteableBitmap wb = new WriteableBitmap(bitmapImage);
                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
                fileStream.Close();
            }
        }
        return true;
}

Is that ok to save png images using WriteableBitmap.SaveJpeg(...)? And is there any function to get the length of BitmapImage?

4

2 回答 2

1

如果它是 PNG,为什么要使用 SaveJpeg 来实际存储它?为什么不简单地使用标准的“数据到文件”方法?如果它已经被编码为 PNG,您需要做的就是存储内容。

在这里阅读更多:

于 2012-07-17T16:35:32.820 回答
0

转换为字节数组

byte[] data;
using (MemoryStream ms = new MemoryStream()
{
    bitmapImage.SaveJpeg(ms, LoadedPhoto.PixelWidth, LoadedPhoto.PixelHeight, 0, 95);
    ms.Seek(0, 0);
    data = new byte[ms.Length];
    ms.Read(data, 0, data.Length);
    ms.Close();
}

然后只需获取字节数组的大小并转换为更合理的值(KB,MB ...)

于 2012-07-17T13:10:13.120 回答