show image in picture gallery in windows phone 7

944 views Asked by At

I have an image app in wp7.

class Images
{
   public string Title {get;set;}
   public string Path {get;set;}
}

on page level, i bind title and path(relative to my app) it to a list.

What i need is, when user click on list item the respective image open in picture gallery of windows phone 7.

1

There are 1 answers

2
Thomas Joulin On

You should clarify your question, but I suppose Path is the location of your image in isolated storage. Providing that Image is the name of your Image in xaml

img.Source = GetImage(LoadIfExists(image.Path));

LoadIfExists returns the binary data for a file in Isolated Storage, and GetImage returns it as a WriteableBitmap :

    public static WriteableBitmap GetImage(byte[] buffer)
    {
        int width = buffer[0] * 256 + buffer[1];
        int height = buffer[2] * 256 + buffer[3];

        long matrixSize = width * height;

        WriteableBitmap retVal = new WriteableBitmap(width, height);

        int bufferPos = 4;

        for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
        {
            int pixel = buffer[bufferPos++];
            pixel = pixel << 8 | buffer[bufferPos++];
            pixel = pixel << 8 | buffer[bufferPos++];
            pixel = pixel << 8 | buffer[bufferPos++];
            retVal.Pixels[matrixPos] = pixel;
        }

        return retVal;
    }

    public static byte[] LoadIfExists(string fileName)
    {
        byte[] retVal;

        using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (iso.FileExists(fileName))
            {
                using (IsolatedStorageFileStream stream = iso.OpenFile(fileName, FileMode.Open))
                {
                    retVal = new byte[stream.Length];
                    stream.Read(retVal, 0, retVal.Length);
                }
            }
            else
            {
                retVal = new byte[0];
            }
        }
        return retVal;
    }

If you want to write the image into the Picture Library, it's basically the same process, ending by calling SavePictureToCameraRoll() of MediaLibrary as explained on this MSDN Article