在隔离存储中从图像设置辅助平铺BackgroundImage

时间:2013-11-20 21:42:46

标签: c# windows-phone-8

这是我从图片网址获取流的方式:

        using (var httpClient = new HttpClient())
        {
            response = await httpClient.GetStreamAsync(new Uri(IMAGEURL_HERE, UriKind.Absolute));
        }

        SaveImage(response);

这就是我将它保存到IsoloatedStorage的方式:

    private void SaveImage(Stream result)
    {
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(result);
            var wb = new WriteableBitmap(bitmap);

            using (IsolatedStorageFileStream fileStream = file.CreateFile("FILENAME.jpg"))
            {
                int width = wb.PixelWidth;
                int height = wb.PixelHeight;
                if (wb.PixelWidth > 336)
                {
                    width = 336;
                }
                if (wb.PixelHeight > 336)
                {
                    height = 336;
                }
                Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);
            }
        }
    }

所以让我们说文件是FILENAME.jpg,我想我可以把它设置为像这样的辅助图块的BackgroundImage:

var tileData = new FlipTileData()
{
...
BackgroundImage = new Uri("isostore:/Shared/ShellContent/FILENAME.jpg", UriKind.Absolute),
...

它无效。它不会抛出异常,只会显示图像。我错过了什么?当然如果我将Image Url作为Uri放到BackgroundImage它可以工作,但这不是我想要的。

编辑:我在这里看到过类似的问题,但它对我的代码没有帮助。

1 个答案:

答案 0 :(得分:2)

试试这个。可能是它的帮助。

string imageFolder = @"\Shared\ShellContent"; 
string shareJPEG = "FILENAME.jpg";

private void SaveImage(Stream result)
{
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if(!myIsolatedStorage.DirectoryExists(imageFolder))
        {
            myIsolatedStorage.CreateDirectory(imageFolder);
        }

        if (myIsolatedStorage.FileExists(shareJPEG))
        {
            myIsolatedStorage.DeleteFile(shareJPEG);
        }

        string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filePath);
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(result);
        WriteableBitmap wb = new WriteableBitmap(bitmap);

        // Encode WriteableBitmap object to a JPEG stream. 
        int width = wb.PixelWidth;
        int height = wb.PixelHeight;
        if (wb.PixelWidth > 336)
        {
            width = 336;
        }
        if (wb.PixelHeight > 336)
        {
            height = 336;
        }
        Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);

        fileStream.Close();


    }
} 

private void CreateTile()
{
    var tileData = new FlipTileData()
    {
         ....
         string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);                
         BackgroundImage = new Uri(@"isostore:" + filePath, UriKind.Absolute);
         ....
    } 
}