首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >墨迹到内存流问题

墨迹到内存流问题
EN

Stack Overflow用户
提问于 2011-04-12 16:32:15
回答 1查看 339关注 0票数 1

我试图将墨迹从Microsoft.Ink名称空间转换为内存流,以便将其转换为图像,但我不明白为什么它在内存流中出现错误。我觉得这是来自Convert.FromBase64String()的错误

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

请帮帮我

下面是我的代码:

代码语言:javascript
复制
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;


        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-12 16:37:54

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

除此之外,你在字符串之间的转换真的没有任何意义。只需使用私有byte[]变量来存储手写输入数据。此外,通过MemoryStreamSystem.Graphics.Image绕道也是没有意义的。

代码语言:javascript
复制
// 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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5632430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档