如何在windows手机中将图像加载到writeablebitmap?

时间:2014-04-23 10:27:56

标签: c# windows-phone-7 windows-phone-8 writablebitmap

我试图创建一个writablebitmap。我想将图像加载到我的可写图像但它不起作用(请注意图像): enter image description here

这是我的代码:

var b = new WriteableBitmap(336, 336);

        var background = new Canvas();
        background.Height = b.PixelHeight;
        background.Width = b.PixelWidth;
        background.Background = new SolidColorBrush(Colors.Red);

        var canvas = new Grid();
        canvas.Width = b.PixelWidth;
        canvas.Height = b.PixelHeight;

        var textBlock = new TextBlock();
        textBlock.Text = "sample";
        textBlock.Margin = new Thickness(0, 150, 0, 0);
        textBlock.Width = 336;
        textBlock.Foreground = new SolidColorBrush(Colors.Blue); //color of the text on the Tile    
        textBlock.FontSize = 26;

        canvas.Children.Add(textBlock);

        Image img = new Image();
        img.Source = new BitmapImage(new Uri("Images/TileBgs/Medium/sun.png", UriKind.Relative));
        img.ImageOpened += (s, e) =>
        {
            b.Render(background, null);
            b.Render(img, null);
            b.Render(canvas, null);
            b.Invalidate(); //Draw bitmap

            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/myImage.jpg", System.IO.FileMode.Create, isf))
                {
                    b.SaveJpeg(imageStream, b.PixelWidth, b.PixelHeight, 0, 100);
                }
            }
        };

有什么不对?

1 个答案:

答案 0 :(得分:1)

     var canvas = new Canvas();
        canvas.Width = 336;
        canvas.Height = 336;
        canvas.Background = new SolidColorBrush(Colors.Red);
        var textBlock = new TextBlock();
        textBlock.Text = "sample";
        textBlock.Margin = new Thickness(130, 250, 0, 0);
        textBlock.Width = 336;
        textBlock.Foreground = new SolidColorBrush(Colors.Blue); 
        textBlock.FontSize = 26;

        canvas.Children.Add(textBlock);

        Image img = new Image();
        img.Source = new BitmapImage(new Uri("Images/TileBgs/Medium/sun.png", UriKind.Relative));
        img.Margin = new Thickness(40, 10, 0, 0);
        img.Width = 250;
        canvas.Children.Add(img);
        Maingrid.Children.Add(canvas);