链接到内存流问题
本文关键字:问题 内存 链接 | 更新日期: 2023-09-27 17:49:57
我正在尝试转换微软的墨水。墨水命名空间到内存流,所以把它转换成图像,但我不明白为什么它在内存流中得到一个错误。我觉得这是一个错误从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;
}
}
}
文档是非常初步的,但我认为你可能使用了错误的PersistenceFormat
标签:你使用Base64作为输出格式,但你显然想要PersistenceFormat.Gif
。
除此之外,与字符串之间的转换实际上没有任何意义。只需使用私有byte[]
变量来存储墨水数据。此外,绕道MemoryStream
和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);
}