【问题标题】:Ink to memorystream problem墨水到内存流问题
【发布时间】:2011-08-03 16:43:21
【问题描述】:

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


        }
    }
}

【问题讨论】:

    标签: c# memorystream tablet microsoft.ink


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2011-08-17
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多