2016-12-07 55 views
0

的攻試圖捕捉UWP截圖但是,一些問題正在興起,說試圖採取截圖在UWP編程上的圖像

1)可寫的地圖不包含渲染的定義。

2)可寫的地圖不包含用於Save.jpeg

定義

3)鍵入或命名空間媒體庫不找到。 任何類型的幫助將不勝感激。 代碼

private void Download(object sender, TappedRoutedEventArgs e) 
     { 
      // Create a WriteableBitmap with height and width same as that of Layoutroot 
      WriteableBitmap bmp = new WriteableBitmap(480, 696); 

      //Render the layoutroot element on it 
      bmp.Render(Scrshot, null); 
      bmp.Invalidate(); 


      //Save the image to Medialibrary 

      //create a stream of image 
      var ms = new MemoryStream(); 
      bmp.SaveJpeg(ms, 480, 696, 0, 100); 
      ms.Seek(0, SeekOrigin.Begin); 


      //every path must be unique 
      var filePath = "myfile" + DateTime.Now.ToString(); 

      //Remember to include Medialib capabilty from WMAppManifest.xml for acessing the Medialibrary 
      var lib = new MediaLibrary(); 
      lib.SavePicture(filePath, ms); 

      MessageBox.Show("Saved in your media library!", "Done", MessageBoxButton.OK); 
     } 

回答

0

代替WriteableBitmap使用RenderTargetBitmap創建一個可見的UIElement的位圖表示。爲了這個位圖保存爲一個文件,你可以用我創造了這個擴展方法(here's a great example for extension methods):

public static class RenderTargetBitmapExtensions { 
    public static async Task<StorageFile> ToFile(this RenderTargetBitmap renderTargetBitmap, string filename, StorageFolder folder = null, bool overrideExisting = true) { 
     if (folder == null) folder = ApplicationData.Current.TemporaryFolder; 

     try { 
      byte[] pixels = (await renderTargetBitmap.GetPixelsAsync()).ToArray(); 

      StorageFile outputFile = await folder.CreateFileAsync(filename, overrideExisting ? CreationCollisionOption.ReplaceExisting : CreationCollisionOption.GenerateUnique); 
      var bitmapEncodingMode = BitmapEncoder.PngEncoderId; 

      using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { 
       var encoder = await BitmapEncoder.CreateAsync(bitmapEncodingMode, writeStream); 
       encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, 96, 96, pixels); 
       await encoder.FlushAsync(); 
      } 
      return outputFile; 
     } catch { 
      return null; 
     } 
    } 
} 

終於搞定了公共圖像文件夾(如MediaLibrary是一個Silverlight類和UWP不存在了),可以將下面根據this thread做:

StorageFolder picturesLibrary = KnownFolders.PicturesLibrary; 
StorageFolder savedPicturesFolder = await picturesLibrary.CreateFolderAsync("Saved Pictures", CreationCollisionOption.OpenIfExists); 

注:默認情況下,應用程序不能訪問到的圖片庫。您必須在Package.appxmanifest中添加該功能。打開Package.appxmanifest並點擊Capabilities選項卡。有一個圖片庫的複選框。

所以整個代碼要做到這一點應該是:

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); 
await renderTargetBitmap.RenderAsync(LayoutRoot, Convert.ToInt32(LayoutRoot.ActualWidth), Convert.ToInt32(LayoutRoot.ActualHeight)); 

StorageFolder savedPicturesFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Saved Pictures", CreationCollisionOption.OpenIfExists); 

await renderTargetBitmap.ToFile("filename.jpg", savedPicturesFolder); 

或者,如果你不想覆蓋現有文件,最後一行是:

await renderTargetBitmap.ToFile("filename.jpg", savedPicturesFolder, false); 

對於您也可以創建一個基於時間的文件名:

string filename = String.Format("downloaded_{0}.jpg", DateTime.Now.ToString("yyyyMMdd_HHmmss")); 
await renderTargetBitmap.ToFile(filename, savedPicturesFolder, false); 
+0

Thx @schumi但可以幫助我如何使用此在我的代碼中的擴展方法,並且你能夠正確地實現你提供的所有上述方法。實際上,我對uwp並不是完全意識到所有東西都是新的。 – micky

+0

我修改了答案,希望它有幫助。雖然擴展方法不是UWP專用的,但是一般情況下可以在C#中使用,據我所知,這是一件非常酷的事情;) – schumi1331

+0

Thx很多schumi ..它的工作原理。但它只下載一個圖像,並將其替換爲另一個圖像,我如何下載多個不同的圖像。 – micky