我有一个图像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); //.....// }
您链接的示例是“如何将SurfaceImageSource目标另存为通用应用程序中的图像”,这正是您想要的。 它创建一个名为“MyImageSourceComponent”的C ++ Windows运行时组件,并提供一个名为“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应用程序, 在这里你可以参考。 我创建了一个新的Windows运行时组件的uwp应用程序,并引用示例中的“MyImageSouceComponent”的代码。 然后添加运行时组件作为uwp项目的参考。 最后使用上面的代码调用SaveSurfaceImageToFile
方法。