通过c#从XAML网格+图像生成图像

时间:2014-01-14 21:21:30

标签: c# xaml windows-phone-8

我尝试为包含背景颜色网格和透明背景的PNG的图块生成图像。

var TestTile = new Grid()
                {
                    Background = colTemp,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    Margin = new Thickness( 0, 12, 0, 0 ),
                };
                TestTile.Arrange(new Rect(0, 0, 366, 366));

                var ico = new Image() { 
                    Source = new BitmapImage(new Uri("Images/mCloudSunT.png", UriKind.Relative)),
                };
                TestTile.Children.Add(ico);
...
bitmap.Render(TestTile, new TranslateTransform());
...

我使用背景颜色但没有PNG的图像。我没有错误,URI是正确的(测试)。

新代码:

var colTemp = new SolidColorBrush(Color.FromArgb(255, 174, 190, 206));
                var TestTile = new Grid()
                {                     
                    Background = colTemp,
                    Height = 336,
                    Width = 336,
                };                   

                var ico = new Image() {
                    Source = new BitmapImage(new Uri("Images/mCloudSunT.png", UriKind.Relative)),
                };
                Grid.SetColumn(ico, 0);
                Grid.SetRow(ico, 0);
                TestTile.Children.Add(ico);

                TestTile.Measure(new Size(336, 336));
                TestTile.Arrange(new Rect(0, 0, 366, 366));

                TestTile.UpdateLayout();

以下是生成图片的代码:

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

这里是用作平铺背景的代码:

ShellTile PinnedTile = ShellTile.ActiveTiles.First();
                FlipTileData UpdatedTileData = new FlipTileData
                {
                    BackgroundImage = new Uri("isostore:/shared/shellcontent/test.jpg", UriKind.RelativeOrAbsolute),                      
                };
                PinnedTile.Update(UpdatedTileData);

2 个答案:

答案 0 :(得分:0)

调用测量,然后在添加图标后进行排列。

TestTile.Measure( new Size( 366,366 ) );
TestTile.Arrange( new Rect( 0, 0, 366, 366 ) );
TestTile.UpdateLayout();

...
WritableBitmap...

答案 1 :(得分:0)

我认为没有理由调用SetColumn,SetRow,Measure,Arrange或UpdateLayout。此外,在调用Render()之后,调用Invalidate()。试试这个:

var colTemp = new SolidColorBrush(Color.FromArgb(255, 174, 190, 206));
            var TestTile = new Grid()
            {                     
                Background = colTemp,
                Height = 336,
                Width = 336,
            };                   

            var ico = new Image() {
                Source = new BitmapImage(new Uri("Images/mCloudSunT.png", UriKind.Relative)),
            };

            TestTile.Children.Add(ico);

....

bitmap.Render(TestTile, new TranslateTransform());
bitmap.Invalidate();