1

我试图将墨迹从 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;


        }
    }
}
4

1 回答 1

1

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

除此之外,您与字符串之间的转换实际上根本没有意义。只需使用私有byte[]变量来存储墨迹数据。MemoryStream此外,您通过 a和 a绕道System.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);
}
于 2011-04-12T08:37:54.467 回答