diff --git a/PixivFSUWP/BigImage.xaml b/PixivFSUWP/BigImage.xaml index db7516d..e9e0570 100644 --- a/PixivFSUWP/BigImage.xaml +++ b/PixivFSUWP/BigImage.xaml @@ -26,10 +26,10 @@ - + - + () { ".png" }); + picker.SuggestedFileName = parameter.Title; + var file = await picker.PickSaveFileAsync(); + if (file != null) + { + CachedFileManager.DeferUpdates(file); + using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) + { + var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); + var image = mainImg.Source as WriteableBitmap; + var imageStream = image.PixelBuffer.AsStream(); + byte[] raw = new byte[imageStream.Length]; + await imageStream.ReadAsync(raw, 0, raw.Length); + encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, + (uint)image.PixelWidth, (uint)image.PixelHeight, 96, 96, raw); + await encoder.FlushAsync(); + } + var updateStatus = await CachedFileManager.CompleteUpdatesAsync(file); + if (updateStatus != FileUpdateStatus.Complete) + { + var messageDialog = new MessageDialog("图片保存失败"); + messageDialog.Commands.Add(new UICommand("重试", async (a) => { await saveImage(); })); + messageDialog.Commands.Add(new UICommand("放弃")); + messageDialog.DefaultCommandIndex = 0; + messageDialog.CancelCommandIndex = 1; + await messageDialog.ShowAsync(); + } + else + { + var messageDialog = new MessageDialog("图片已保存"); + messageDialog.Commands.Add(new UICommand("好的")); + messageDialog.DefaultCommandIndex = 0; + await messageDialog.ShowAsync(); + } + } + } + + private async Task saveStrokes() + { + var strokes = mainCanvas.InkPresenter.StrokeContainer.GetStrokes(); + if (strokes.Count > 0) + { + FileSavePicker picker = new FileSavePicker(); + picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; + picker.FileTypeChoices.Add("图片文件", new List() { ".png" }); + picker.FileTypeChoices.Add("墨迹原始信息", new List() { ".gif" }); + picker.SuggestedFileName = "我的临摹"; + var file = await picker.PickSaveFileAsync(); + if (file != null) + { + CachedFileManager.DeferUpdates(file); + using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) + { + if (file.FileType == ".png") + { + var width = (int)mainCanvas.ActualWidth; + var height = (int)mainCanvas.ActualHeight; + var device = CanvasDevice.GetSharedDevice(); + var renderTarget = new CanvasRenderTarget(device, width, height, 96); + using (var ds = renderTarget.CreateDrawingSession()) + { + ds.Clear(Colors.White); + ds.DrawInk(strokes); + } + await renderTarget.SaveAsync(stream, CanvasBitmapFileFormat.Png); + } + else + { + using (var outputStream = stream.GetOutputStreamAt(0)) + { + await mainCanvas.InkPresenter.StrokeContainer.SaveAsync(outputStream); + await outputStream.FlushAsync(); + } + } + } + var updateStatus = await CachedFileManager.CompleteUpdatesAsync(file); + if (updateStatus != FileUpdateStatus.Complete) + { + var messageDialog = new MessageDialog("墨迹保存失败"); + messageDialog.Commands.Add(new UICommand("重试", async (a) => { await saveStrokes(); })); + messageDialog.Commands.Add(new UICommand("放弃")); + messageDialog.DefaultCommandIndex = 0; + messageDialog.CancelCommandIndex = 1; + await messageDialog.ShowAsync(); + } + else + { + var messageDialog = new MessageDialog("墨迹已保存"); + messageDialog.Commands.Add(new UICommand("好的")); + messageDialog.DefaultCommandIndex = 0; + await messageDialog.ShowAsync(); + } + } + } + else + { + var messageDialog = new MessageDialog("没有墨迹可以保存"); + messageDialog.Commands.Add(new UICommand("好的")); + messageDialog.DefaultCommandIndex = 0; + await messageDialog.ShowAsync(); + } + } } } diff --git a/PixivFSUWP/PixivFSUWP.csproj b/PixivFSUWP/PixivFSUWP.csproj index a29ef30..0ae98f7 100644 --- a/PixivFSUWP/PixivFSUWP.csproj +++ b/PixivFSUWP/PixivFSUWP.csproj @@ -209,6 +209,9 @@ 0.1.2 + + 1.23.0 +