2016-12-28 4 views
1

私は、子、Image、TextBlockを持つGridを持つUWPアプリケーションを開発しています。私は達成したい、助けが必要な2つのことがあります。(UWP)pngとしてグリッドを保存

  1. は、どのように私は事前に定義された名前のローカルフォルダ内の画像(好ましくはPNG)として、それの子の内容とともに、Grid要素を保存しますか?

  2. この保存されたイメージを元に戻して、それを他の互換性のあるアプリケーションと共有するための添付ファイルとして含めるにはどうすればよいですか?

例:

<StackPanel Orientation="Vertical"> 
     <Grid Name="myGrid" 
       HorizontalAlignment="Stretch" 
       Background="Black"> 

      <Image Name="myImage" 
        Source="Assets/image1.png" 
        Stretch="Uniform"/> 

      <TextBlock Name="myTextBlock" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         TextWrapping="Wrap" 
         Text="Sample user text"/> 
     </Grid> 

     <Button Name="saveButton" 
       Content="Save" 
       Margin="10,10,0,0" 
       Click="saveButton_Click" /> 

     <Button Name="shareButton" 
       Content="Share" 
       Margin="10,10,0,0" 
       Click="shareButton_Click" /> 
    </StackPanel> 

EDIT:saveButton_Clickイベント内のコードのこのビットを試してみました。うまくいかないようです。

RenderTargetBitmap rtb = new RenderTargetBitmap(); 
    await rtb.RenderAsync(myGrid); 

    var pixelBuffer = await rtb.GetPixelsAsync(); 
    var pixels = pixelBuffer.ToArray(); 

    var displayInformation = DisplayInformation.GetForCurrentView(); 

    var stream = new InMemoryRandomAccessStream(); 
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); 
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
         BitmapAlphaMode.Premultiplied, 
         (uint)rtb.PixelWidth, 
         (uint)rtb.PixelHeight, 
         displayInformation.RawDpiX, 
         displayInformation.RawDpiY, 
         pixels); 

    await encoder.FlushAsync(); 
    stream.Seek(0); 

    FileStream filestream = File.Create("C:\\Users\\pcUser\\Desktop\\testimage.png", (int)stream.Size); 

EDIT2:このコードを試してみてください。それは多少動作するようですが、私はおそらくそれはmyGridを保存するだけなので、おそらくそれは子供のコンテンツではない黒いイメージを取得しています。

RenderTargetBitmap rtb = new RenderTargetBitmap(); 
    await rtb.RenderAsync(quoteGrid); 

    var pixelBuffer = await rtb.GetPixelsAsync(); 
    var pixels = pixelBuffer.ToArray(); 

    var displayInformation = DisplayInformation.GetForCurrentView(); 

    var stream = new InMemoryRandomAccessStream(); 
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); 
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
         BitmapAlphaMode.Premultiplied, 
         (uint)rtb.PixelWidth, 
         (uint)rtb.PixelHeight, 
         displayInformation.RawDpiX, 
         displayInformation.RawDpiY, 
         pixels); 

    await encoder.FlushAsync(); 
    stream.Seek(0); 

    var wbm = new WriteableBitmap(rtb.PixelWidth, rtb.PixelHeight); 
    await wbm.SetSourceAsync(stream); 

    StorageFolder folder = ApplicationData.Current.LocalFolder; 
    if (folder != null) 
    { 
     StorageFile file = await folder.CreateFileAsync("testImage" + ".png", CreationCollisionOption.ReplaceExisting); 
     using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
     { 
      encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, storageStream); 
      var pixelStream = wbm.PixelBuffer.AsStream(); 
      await pixelStream.ReadAsync(pixels, 0, pixels.Length); 
      encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)wbm.PixelWidth, (uint)wbm.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, new byte[pixelStream.Length]); 
      await encoder.FlushAsync(); 
     } 
    } 
+0

Windows.UI.Xaml.Media.Imaging名前空間のRenderTargetBitmapクラスを使用して、手順1を実行できるようです。このガイドの最初の部分では、この問題を解決しています。http://metulev.com/render-xaml-/ –

+0

@ LincolnGreen次に、RenderTargetBitmapオブジェクトを保存するにはどうすればよいですか?私はWindowsプログラミングにとって非常に新しいです。 –

+0

ストリームをファイルに保存するだけです – chris579

答えて

0

どのように私は保存しない、それに伴いGrid要素は、あらかじめ定義された名前のローカルフォルダ内の画像(好ましくはPNG)として、子の内容は?

あなたのコードを確認しました。 WriteableBitmapを使用する必要はありませんでしたが、ファイルストリームをBitmapEncoder.CreateAsyncメソッドに渡すだけです。私は戻って、この保存された画像を取得し、互換性のある他のアプリケーションと共有する添付ファイルとしてそれを含めるにはどうすればよい

private async void saveButton_Click(object sender, RoutedEventArgs e) 
{ 
     RenderTargetBitmap rtb = new RenderTargetBitmap(); 
     await rtb.RenderAsync(myGrid); 

     var pixelBuffer = await rtb.GetPixelsAsync(); 
     var pixels = pixelBuffer.ToArray(); 
     var displayInformation = DisplayInformation.GetForCurrentView(); 
     var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("testImage" + ".png", CreationCollisionOption.ReplaceExisting); 
     using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
     { 
      var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); 
      encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
           BitmapAlphaMode.Premultiplied, 
           (uint)rtb.PixelWidth, 
           (uint)rtb.PixelHeight, 
           displayInformation.RawDpiX, 
           displayInformation.RawDpiY, 
           pixels); 
      await encoder.FlushAsync(); 
     } 
} 

:詳細については、次のコードを確認してください?

あなたの場合は、画像をローカルフォルダに保存します。あなたはそれを取得したいのであれば、あなたはストレージAPIs.For例を使用する必要があります:

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("testImage.png"); 

あなたが詳細については、公式Sharing content source app sampleを参照することができ、「互換性のある他のアプリと共有する添付ファイルとしてそれを含める」について。

+0

素晴らしい作品です!どうもありがとうございました! –

関連する問題