2014-09-24 19 views
0

我正在爲Windows Phone 8.1開發一個小遊戲,爲此我使用一組圖像路徑並選擇一個它作爲網格的背景並使用RenderAsync()生成位圖並將其保存到設備的圖片文件夾中。通過RenderAsync()創建和保存圖像不適用於在Windows Phone 8.1中隨機選擇的圖像

我的圖像路徑是正確的,我使用XAML下面的代碼來創建UI

<StackPanel> 
    <Grid x:Name="ResultGrid" Margin="0,-26.667,0,-0.333" Visibility="Visible"> 
     <Grid.Background> 
      <ImageBrush Stretch="Fill" ImageSource="Assets/result/b (3).png"/> 
     </Grid.Background> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="200*"/> 
      <ColumnDefinition Width="200*"/> 
     </Grid.ColumnDefinitions> 
     <StackPanel x:Name="SuperHeroName" Margin="71,262,-22,212" Grid.Column="1"> 
      <StackPanel.Background> 
       <ImageBrush Stretch="Fill" ImageSource="Assets/result/h (10).png"/> 
      </StackPanel.Background> 
     </StackPanel> 
     <TextBlock x:Name="txtResultSharing" Margin="14,259,107,12" Grid.ColumnSpan="2" FontSize="48" Text="I have earned 78555 points in 8000 levels" TextWrapping="WrapWholeWords" Foreground="#FFA2A2A2" FontFamily="Buxton Sketch"/> 
     <Image x:Name="AppLogo" Margin="130,586,10,2" Grid.Column="1" Source="/Assets/FULL LOGO.png"/> 
     <TextBlock x:Name="txtResultSharing1" Margin="12,257,109,14" Grid.ColumnSpan="2" FontSize="48" Text="I have earned 78555 points in 8000 levels" TextWrapping="WrapWholeWords" Foreground="White" FontFamily="Buxton Sketch"/> 
    </Grid> 
</StackPanel> 

和代碼是

private async void boom() 
{ 
    Random rnd = new Random(); 
    string[] heroImages = { "ms-appx:///Assets/result/h (1).png", 
         "ms-appx:///Assets/result/h (2).png", 
         "ms-appx:///Assets/result/h (3).png", 
         "ms-appx:///Assets/result/h (4).png", 
         "ms-appx:///Assets/result/h (5).png", 
         "ms-appx:///Assets/result/h (6).png", 
         "ms-appx:///Assets/result/h (7).png", 
         "ms-appx:///Assets/result/h (8).png", 
         "ms-appx:///Assets/result/h (9).png", 
         "ms-appx:///Assets/result/h (10).png", 
         "ms-appx:///Assets/result/h (11).png", 
         "ms-appx:///Assets/result/h (12).png", 
         "ms-appx:///Assets/result/h (13).png", 
         "ms-appx:///Assets/result/h (14).png", 
         "ms-appx:///Assets/result/h (15).png", 
         "ms-appx:///Assets/result/h (16).png", 
         "ms-appx:///Assets/result/h (17).png", 
         "ms-appx:///Assets/result/h (18).png", 
         "ms-appx:///Assets/result/h (19).png", 
         "ms-appx:///Assets/result/h (20).png", 
         "ms-appx:///Assets/result/h (21).png", 
         "ms-appx:///Assets/result/h (22).png", 
         "ms-appx:///Assets/result/h (23).png" }; 
    var imageBrushT = new ImageBrush 
    { 
     ImageSource = new BitmapImage(new Uri(heroImages[rnd.Next(22)])) 
    }; 
    SuperHeroName.Background = imageBrushT; 

    string[] backImages = { "ms-appx:///Assets/result/b (1).png", 
         "ms-appx:///Assets/result/b (2).png", 
         "ms-appx:///Assets/result/b (3).png", 
         "ms-appx:///Assets/result/b (5).png", 
         "ms-appx:///Assets/result/b (6).png" }; 
    var imageBrushX = new ImageBrush 
    { 
     ImageSource = new BitmapImage(new Uri(backImages[rnd.Next(5)])) 
    }; 
    ResultGrid.Background = imageBrushX; 

    ResultGrid.UpdateLayout(); 


    int x = int.Parse(localSettings.Values["CurrLevel"].ToString()); 
    int y = int.Parse(localSettings.Values["CurrPoints"].ToString()); 
    txtResultSharing.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString()); 
    txtResultSharing1.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString()); 
    ResultGrid.Visibility = Visibility.Visible; 
    ResultGrid.UpdateLayout(); 
    RenderTargetBitmap renderTargetBitmapZ = new RenderTargetBitmap(); 
    await renderTargetBitmapZ.RenderAsync(ResultGrid); 
    var pixels = await renderTargetBitmapZ.GetPixelsAsync();    

    var file = await KnownFolders.PicturesLibrary.CreateFileAsync("picresult.jpg", CreationCollisionOption.ReplaceExisting); 

    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
    { 
     var encoder = await 
      BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); 
     byte[] bytes = pixels.ToArray(); 
     encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
          BitmapAlphaMode.Ignore, 
          (uint)renderTargetBitmapZ.PixelWidth, 
          (uint)renderTargetBitmapZ.PixelHeight, 
          96, 96, bytes); 
     await encoder.FlushAsync(); 
    } 
} 

此代碼顯示正確的圖像在顯示時,我按顯示按鈕即 enter image description here 當我打開並看到保存的圖像(即picresult.jpg)文件時,它顯示沒有超級英雄圖像和背景圖像。它只顯示徽標和文字。 (即,它不保存隨機從數組中選擇的圖像,但它表明他們沒有任何問題)

請幫我

+0

首先避免在圖像名稱中的空格,然後保存圖像獲取圖像,並打開它在您的電腦,看看圖像是否很好地保存/寫入圖片文件夾...然後,如果保存的圖像是好吧,將圖像的事件標記爲ImageFailed,以查看是否有occours .... – saramgsilva 2014-09-24 17:40:20

+0

我嘗試了這些步驟@saramgsilva,但那些不起作用。當我複製並打開它到PC或通過Windows Phone中的文件查看它們的圖像時,它看起來像http://oi60.tinypic.com/27ywftw.jpg – 2014-09-24 18:06:15

+0

提供的圖像的網址不起作用... – saramgsilva 2014-09-24 18:27:28

回答

2

問題是,你正試圖使電網的位圖右後你創建的圖像,可能還沒有繪製。事實上,由於你所看到的,看起來他們沒有。
預加載時你的類的構造與圖像:

heroImages = new BitmapImage[3]; 
heroImages[0] = new BitmapImage(new Uri("ms-appx:///Assets/Image1.png")); 
heroImages[1] = new BitmapImage(new Uri("ms-appx:///Assets/Image2.jpg")); 
heroImages[2] = new BitmapImage(new Uri("ms-appx:///Assets/Image3.jpg")); 

backImages = new BitmapImage[5]; 
backImages[0] = new BitmapImage(new Uri("ms-appx:///Assets/Image1.png")); 
backImages[1] = new BitmapImage(new Uri("ms-appx:///Assets/Image2.jpg")); 
backImages[2] = new BitmapImage(new Uri("ms-appx:///Assets/Image3.jpg")); 
backImages[3] = new BitmapImage(new Uri("ms-appx:///Assets/Image1.png")); 
backImages[4] = new BitmapImage(new Uri("ms-appx:///Assets/Image2.jpg")); 

移動您的代碼的函數:

private async Task DrawToFile() 
{ 
    RenderTargetBitmap renderTargetBitmapZ = new RenderTargetBitmap(); 
    await renderTargetBitmapZ.RenderAsync(ResultGrid); 
    var pixels = await renderTargetBitmapZ.GetPixelsAsync(); 

    var file = await KnownFolders.PicturesLibrary.CreateFileAsync("picresult.jpg", CreationCollisionOption.ReplaceExisting); 

    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
    { 
     var encoder = await 
      BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); 
     byte[] bytes = pixels.ToArray(); 
     encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
          BitmapAlphaMode.Ignore, 
          (uint)renderTargetBitmapZ.PixelWidth, 
          (uint)renderTargetBitmapZ.PixelHeight, 
          96, 96, bytes); 
     await encoder.FlushAsync(); 
    } 
} 

然後在類建立一個某處DispatcherTimer如下:

DispatcherTimer _timer; // member of class 

// Add Following to constructor 
_timer = new DispatcherTimer(); 
_timer.Interval = new TimeSpan(0, 0, 1); // 1 second 
_timer.Tick += _timer_Tick; 

// Timer Routine 
void _timer_Tick(object sender, object e) 
{ 
    _timer.Stop(); 
    DrawToFile(); 
} 

最後在Re​​nderGrid.UpdateLayout()調用後啓動計時器:

txtResultSharing.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString()); 
txtResultSharing1.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString()); 
ResultGrid.Visibility = Visibility.Visible; 
ResultGrid.UpdateLayout(); 

_timer.Start(); 

這會讓用戶界面1第二次繪製圖像,然後再嘗試將它們保存到文件中。應該管用。

+0

這個工程,但我不能添加我的隨機圖像。如果我對第68至107行進行了註釋,則可以使用我的.cs這裏https://gist.github.com/IshamMohamed/855e643f87c55dd69b52,這與我給予網格的默認背景非常吻合,但我的場景是,網格背景發生變化動態和我從2個數組中選擇。 – 2014-09-25 02:01:06

+0

嘗試預加載圖像。我編輯了我原來的帖子。 – 2014-09-25 22:51:37

+0

這工作的人。非常感謝 :) – 2014-09-26 04:31:26

相關問題