墨水到记忆流的问题

时间:2011-04-12 08:32:15

标签: c# memorystream tablet microsoft.ink

我试图将墨水从Microsoft.Ink命名空间转换为内存流,因此要将其转换为图像,但我不明白为什么它会在内存流中出现错误。我觉得这是Convert.FromBase64String()

的错误

但我不知道还有什么其他选择可以将它转换为图像。

请帮帮我

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Ink;

namespace testPaint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        InkCollector ink;

        private void Form1_Load(object sender, EventArgs e)
        {
            ink = new InkCollector(pictureBox1);
            ink.Enabled = true;
            ink.AutoRedraw = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            ink.Enabled = false;

            string strInk = Convert.ToBase64String(ink.Ink.Save(PersistenceFormat.Base64InkSerializedFormat, CompressionMode.Maximum));
            textBox1.Text = strInk;
            ink.Enabled = true;
        }

        private void btnClr_Click(object sender, EventArgs e)
        {
            ink.Enabled = false;
            ink.Ink = new Microsoft.Ink.Ink();
            ink.Enabled = true;
            pictureBox1.Invalidate();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            byte[] byImage = Convert.FromBase64String(textBox1.Text);
            MemoryStream ms = new MemoryStream();
            ms.Write(byImage, 0, byImage.Length);
            Image img = Image.FromStream(ms);
            img.Save("test.gif", System.Drawing.Imaging.ImageFormat.Gif);
            ink.Enabled = true;


        }
    }
}

1 个答案:

答案 0 :(得分:1)

文档非常初步,但我认为您可能使用了错误的PersistenceFormat标记:您使用Base64作为输出格式,但您显然需要PersistenceFormat.Gif

除此之外,您在字符串中的转换实际上并没有任何意义。只需使用私有byte[]变量来存储墨迹数据。此外,通过MemoryStreamSystem.Graphics.Image进行绕行也没有任何意义。

// using System.IO;

private byte[] inkData;

private void btnSave_Click(object sender, EventArgs e)
{
    inkData = ink.Ink.Save(PersistenceFormat.Gif, CompressionMode.Maximum);
}

private void btnExport_Click(object sender, EventArgs e)
{
    // Data is already in GIF format, write directly to file!
    using (var stream = new FileStream("filename", FileMode.Create, FileAccess.Write))
         stream.Write(inkData, 0, inkData.Length);
}