将InkManager图像保存为字节数组

时间:2014-03-25 00:07:07

标签: windows-8 microsoft-metro windows-store-apps windows-8.1

我是win8应用程序编程的新手,但已被指派编写一个Windows应用程序来捕获客户的签名并将其保存到SQL Server。经过一些研究,我发现了一个很棒的教程 http://www.codeproject.com/Articles/416878/Metro-Paint显示如何在本地绘制和保存图像。我的问题是如何使用教程中的InkManager类将图像保存到字节数组,以便我可以将图像保存到SQLServer?谢谢!

    private async void btnSaveWritingAsImage_Click(object sender, RoutedEventArgs e)
    {
        if (MyInkManager.GetStrokes().Count > 0)
        {
            try
            {
                Windows.Storage.Pickers.FileSavePicker SavePicker = new Windows.Storage.Pickers.FileSavePicker();
                SavePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
                SavePicker.DefaultFileExtension = ".png";
                SavePicker.FileTypeChoices.Add("PNG", new string[] { ".png" });
                SavePicker.FileTypeChoices.Add("JPG", new string[] { ".jpg" });
                StorageFile filesave = await SavePicker.PickSaveFileAsync();
                IOutputStream ab = await filesave.OpenAsync(FileAccessMode.ReadWrite);
                if (ab != null)
                    await MyInkManager.SaveAsync(ab);
            }

            catch (Exception)
            {
                var MsgDlg = new MessageDialog("Only handwriting can be saved as image.", "Error while saving");
                MsgDlg.ShowAsync();
            }
        }
        else
        {
            var MsgDlg = new MessageDialog("Only handwriting can be saved as image.", "Error while saving");
            await MsgDlg.ShowAsync();
        }
    }

1 个答案:

答案 0 :(得分:0)

add:(IBuffer.ToArray()在WindowsRuntimeBufferExtensions中定义)

使用System.Runtime.InteropServices.WindowsRuntime;

然后就这样做:

var buffer = await FileIO.ReadBufferAsync(image);//replace ab instead of image
var bytes = buffer.ToArray();