生成错误颜色的瓷砖图像

时间:2014-01-13 20:50:52

标签: c# xaml windows-phone-8

我正在尝试使用生成的图片更新应用磁贴。我没有错误但是图像现在只是黑色而不是这种颜色:'Color.FromArgb(255,0,100,50)'。任何想法为什么?

//This have to be the Image, just a colored squer for the beginning
var TestTile = new Grid()
                {
                    Background          = new SolidColorBrush(Color.FromArgb( 255, 0, 100, 50 ) ),
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment   = VerticalAlignment.Stretch,
                    Height              = 336,
                    Width               = 336,
                    Margin              = new Thickness( 0, 12, 0, 0 ),
                };

                //generation the image and save it within isoStorage
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!store.DirectoryExists("shared/shellcontent"))
                    {
                        store.CreateDirectory("shared/shellcontent");
                    }  
                    var bitmap = new WriteableBitmap(336, 336);
                    bitmap.Render(TestTile, new TranslateTransform());
                    var stream = store.CreateFile("/shared/shellcontent/test.jpg");
                    bitmap.Invalidate();
                    bitmap.SaveJpeg(stream, 366, 336, 0, 100);
                    stream.Close();
                }

                // Tile Update
                ShellTile PinnedTile = ShellTile.ActiveTiles.First();
                FlipTileData UpdatedTileData = new FlipTileData
                {

                    BackgroundImage = new Uri("isostore:/shared/shellcontent/test.jpg", UriKind.RelativeOrAbsolute), 

                };
                PinnedTile.Update(UpdatedTileData);

2 个答案:

答案 0 :(得分:1)

如果有帮助,这就是我如何保存平铺图像。

  1. 我总是使用Shared \ ShellContent \作为位置。不确定案件是否重要(不应该)
  2. 我总是调用Measure,然后在将其发送到WriteableBitmap
  3. 之前安排UIElement
  4. 使用UriKind.Absolute作为背景uri
  5. 尝试保存您的图片:

    string fileName = @"Shared\ShellContent\Tile.jpg";
    Grid someElement = new Grid() 
    {
        // Add Stuff
    }
    someElement.Measure(new Size(width, height));
    someElement.Arrange(new Rect(0,0,width, height));
    var bitmap = new WriteableBitmap(someElement, null);
    
    using(var stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(fileName, FileMode.OpenOrCreate))
    {
        bitmap.SaveJpeg(stream, width, height, 0, 100);
    }
    
    Uri uri = new Uri("isostore:/" + fileName, UriKind.Absolute);
    

答案 1 :(得分:0)

尝试使用“ms-appdata”URI方案来访问该文件。然后,图像的路径如下:

BackgroundImage = new Uri("ms-appdata:///local/shared/shellcontent/test.jpg", UriKind.RelativeOrAbsolute)

这就是我用来在我的一个应用程序中设置磁贴图像的方法,它按预期工作。

有关详细信息,请参阅Data for Windows Phone文章中的“本地文件夹URI方案”部分:http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402541(v=vs.105).aspx#BKMK_Medialibrary