2017-01-26 55 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