【问题标题】:show image in picture gallery in windows phone 7在 windows phone 7 的图片库中显示图像
【发布时间】:2011-11-30 08:25:54
【问题描述】:

我在 wp7 中有一个图像应用程序。

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

在页面级别,我将标题和路径(相对于我的应用)绑定到一个列表。

我需要的是,当用户单击列表项时,相应的图像会在 windows phone 7 的图片库中打开。

【问题讨论】:

    标签: image windows-phone-7 binding picturegallery


    【解决方案1】:

    您应该澄清您的问题,但我想Path 是您的图像在隔离存储中的位置。假设 Image 是您在 xaml 中的图像名称

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

    LoadIfExists返回隔离存储中文件的二进制数据,GetImage将其作为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;
        }
    

    如果要将图像写入图片库,基本上是相同的过程,通过调用MediaLibrarySavePictureToCameraRoll()结束,如MSDN Article中所述

    【讨论】:

    • 我的应用程序文件夹中有图像,例如 images\image1.png、images\image2.png 等。我希望这些图像在图片库中打开,以便用户可以像在图片库中一样使用图像
    • 我已经更新了我的答案。您应用程序中的“文件夹”称为独立存储,因此我的原始答案适合加载此图像。图片库被 Microsoft 称为图片库,我的更新链接指向 MSDN 中的一篇文章,可满足您的所有需求。但是,您不能从您的应用程序中启动图片应用程序。
    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多