wingyiu
wingyiu

Reputation: 43

How can i get the length of a bitmapimage(jpg/png)?

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?

Upvotes: 0

Views: 243

Answers (2)

Den
Den

Reputation: 16826

If it is a PNG, why would you use SaveJpeg to actually store it? Why not simply use the standard "data-to-file" approach? If it is already encoded as a PNG, all you need to do is store the content.

Read more here:

Upvotes: 1

Igor Ralic
Igor Ralic

Reputation: 15006

Convert to byte array

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

Then just get the size of the byte array and convert to something more reasonable (KB, MB...)

Upvotes: 0

Related Questions