2017-01-26 58 views
1

我有一個圖像SurfaceImageSource,我會將它轉換爲PNG。 我試過用這個工程: link將SurfaceImageSource轉換爲PNG

我試過用SharpDX庫,但是沒成功。

    private void initialize() 
       { 
         StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists); 
         StorageFile imagePng = await folder.CreateFileAsync("file.png", CreationCollisionOption.ReplaceExisting); 

         if (imagePng != null) 
         { 
          //surfaceImageSource to PNG method 
          surfaceToPng(surfaceImage,imagePng); 
         } 
       } 

       private void surfaceToPng(SurfaceImageSource surface,StorageFile imagePng){ 
         IRandomAccessStream stream = await imagePng.OpenAsync(FileAccessMode.ReadWrite); 

          //.....// 
       } 

回答

1

sample您鏈接是關於「如何SurfaceImageSource目標另存爲通用應用圖像」,這就是你想要的。它創建一個名爲「MyImageSourceComponent」的C++ Windows Runtime Component並提供一個名爲「MyImageSource」的密封類,其中包含方法public void SaveSurfaceImageToFile(IRandomAccessStream randomAccessStream);您可以調用此方法將SurfaceImageSource保存爲png。

uint imageWidth; 
uint imageHeight; 
MyImageSource myImageSource; 
public MainPage() 
{ 
    this.InitializeComponent(); 

    imageWidth = (uint)this.MyImage.Width; 
    imageHeight = (uint)this.MyImage.Height; 
    myImageSource = new MyImageSource(imageWidth, imageHeight, true); 
    this.MyImage.Source = myImageSource; 
} 

private async void btnSave_Click(object sender, RoutedEventArgs e) 
{ 
    FileSavePicker savePicker = new FileSavePicker(); 
    savePicker.FileTypeChoices.Add("Png file", new List<string>() { ".png" }); 
    savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
    StorageFile file = await savePicker.PickSaveFileAsync(); 
    if (file != null) 
    { 
     IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite); 
     myImageSource.SaveSurfaceImageToFile(stream); 
    } 
} 

雖然這個示例是爲Windows 8.1,它也應該能夠與uwp應用程序一起工作。我幫助將示例轉換爲您可以參考的uwp app here。我創建了一個帶有Windows運行時組件的新的uwp應用程序,並引用了示例中的「MyImageSouceComponent」的代碼。然後添加運行時組件作爲uwp項目的參考。最後使用上面的代碼調用SaveSurfaceImageToFile方法。

+0

非常感謝您的回答。我的問題是:我已經有了一個SurfaceImageSource類型的圖像。在你的代碼中,你創建並繪製一個MyImageSource類型的新圖像。我應該將我的SurfaceImageSource圖像轉換爲MyImageSource,以便爲我的圖像使用方法SaveSurfaceImageToFile。但我沒有成功。 – Andrea485