Ink to memorystream problem

365 views Asked by At

I was trying to convert an ink from Microsoft.Ink namespace to memorystream, so to convert it to image, but I don't understand why it's getting an error in the memorystream. I kind of felt that it was an error from Convert.FromBase64String()

But I don't know what other choices I have to convert it to image.

Please help me

Here is my code:

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

There are 1 answers

0
Konrad Rudolph On BEST ANSWER

The documentation is very preliminary but I think you are probably using the wrong PersistenceFormat tag: you are using Base64 as the output format but you clearly want PersistenceFormat.Gif.

Apart from that, your conversion to and from a string is really not at all meaningful. Just use a private byte[] variable to store the ink data. Furthermore, your detour via a MemoryStream and a System.Graphics.Image makes no sense either.

// 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);
}