将bmp加载到InkCanvas时,InkSerializedFormat操作失败

时间:2014-09-20 06:08:35

标签: .net wpf inkcanvas

我尝试将bmp文件加载到画布中,因此我执行以下操作:

CanvasStrokeCollection = new StrokeCollection(new FileStream(fileNameFromOpenFileDialog, FileMode.Open, FileAccess.Read));

FileStream正确打开但是当它传递给StrokeCollection我收到:

InkSerializedFormat operation failed.\r\nParameter name: stream

知道我不对吗?我尝试了不同质量的bmp,​​结果相同。

2 个答案:

答案 0 :(得分:2)

好像你正在尝试加载StrokeCollection构造函数不支持的bmp个文件。

错误InkSerializedFormat operation failed.确认您不支持您尝试加载的文件。

StrokeCollection只能使用Ink Serialized Format (ISF)文件流初始化,通常这是您之前使用StrokeCollection.Save方法保存的文件。

Bitmap (bmp)另一方面是栅格格式Ink Serialized Format (ISF)是矢量格式,因此这两种格式互不兼容。

来自MSDN: StrokeCollection Constructor (Stream)

的样本
private void SaveStrokes_Click(object sender, RoutedEventArgs e)
{
    FileStream fs = null;

    try
    {
        fs = new FileStream(inkFileName, FileMode.Create);
        inkCanvas1.Strokes.Save(fs);
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
        }
    }
}

...

private void LoadStrokes_Click(object sender, RoutedEventArgs e)
{
    FileStream fs = null;

    if (!File.Exists(inkFileName))
    {
        MessageBox.Show("The file you requested does not exist." +
            " Save the StrokeCollection before loading it.");
        return;
    }

    try
    {
        fs = new FileStream(inkFileName,
            FileMode.Open, FileAccess.Read);
        StrokeCollection strokes = new StrokeCollection(fs);
        inkCanvas1.Strokes = strokes;
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
        }
    }
}

答案 1 :(得分:0)

最后我以其他方式做到了。我将位图加载到wpf Image对象,然后将其作为子项添加:

canvas.Children.Add(image);