Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
完成墨迹和图片保存
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Apr 23, 2019
1 parent f224ff2 commit 01ca3d3
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
4 changes: 2 additions & 2 deletions PixivFSUWP/BigImage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<InkToolbarBallpointPenButton/>
<InkToolbarPencilButton/>
<InkToolbarEraserButton/>
<InkToolbarRulerButton/>
<InkToolbarStencilButton/>
</InkToolbar>
</CommandBar.Content>
<AppBarButton Icon="Save" Label="保存"/>
<AppBarButton x:Name="btnSaveImage" Icon="Save" Label="保存图片" Click="BtnSaveImage_Click"/>
<AppBarToggleButton x:Name="btnDraw" Icon="Edit" Label="临摹" Unchecked="BtnDraw_Unchecked" Checked="BtnDraw_Checked"/>
</CommandBar>
<ScrollViewer ZoomMode="Enabled" x:Name="scrollRoot"
Expand Down
129 changes: 128 additions & 1 deletion PixivFSUWP/BigImage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using System;
using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板
Expand All @@ -30,6 +38,8 @@ public sealed partial class BigImage : Page
public BigImage()
{
this.InitializeComponent();
mainCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse
| Windows.UI.Core.CoreInputDeviceTypes.Pen;
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
Expand Down Expand Up @@ -97,6 +107,7 @@ private void BtnDraw_Checked(object sender, RoutedEventArgs e)
mainCanvas.Visibility = Visibility.Visible;
inkToolbar.Visibility = Visibility.Visible;
paper.Visibility = Visibility.Visible;
btnSaveImage.Label = "保存墨迹";
lockView();
}

Expand All @@ -106,7 +117,123 @@ private void BtnDraw_Unchecked(object sender, RoutedEventArgs e)
inkToolbar.Visibility = Visibility.Collapsed;
paper.Visibility = Visibility.Collapsed;
mainCanvas.InkPresenter.StrokeContainer.Clear();
btnSaveImage.Label = "保存图片";
unlockView();
}

private async void BtnSaveImage_Click(object sender, RoutedEventArgs e)
{
if (_locked)
await saveStrokes();
else
await saveImage();
}

private async Task saveImage()
{
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeChoices.Add("图片文件", new List<string>() { ".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<string>() { ".png" });
picker.FileTypeChoices.Add("墨迹原始信息", new List<string>() { ".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();
}
}
}
}
3 changes: 3 additions & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@
<PackageReference Include="PixivFSCS">
<Version>0.1.2</Version>
</PackageReference>
<PackageReference Include="Win2D.uwp">
<Version>1.23.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup />
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
Expand Down

0 comments on commit 01ca3d3

Please sign in to comment.