1

我看过一些文章,展示了可以将墨水与背景图像一起保存,以便将墨水叠加到图像上,然后将其保存为单个图像文件。但是,不幸的是,我看到的文章没有详细说明它是如何完成的。我可以使用以下语句轻松保存背景图像:

axInkPicture1.Picture.Save(@"\path\to\file.gif");

...但是背景图像上没有覆盖墨水

我不知道任何节省墨水的直接方法,但这就是我目前的做法:

string tempPath = Path.GetTempPath();
string tempFile = tempPath + @"\file.gif";
// save to a temp file
if (axInkPicture1.Ink.Strokes.Count > 0)
    {
        byte[] bytes = (byte[])axInkPicture1.Ink.Save(InkPersistenceFormat.IPF_GIF, InkPersistenceCompressionMode.IPCM_Default);

        using (MemoryStream ms = new MemoryStream(bytes))
        {
            using (Bitmap gif = new Bitmap(ms))
            {
                gif.Save(tempFile);
            }
        }

    }

这样可以节省墨水,但没有背景图像。

如何将墨水和图像保存到一个文件中?

任何帮助或方向指点表示赞赏...

### 编辑

这是我到目前为止还尝试过的其他方法

 InkRectangle inkBox = axInkPicture1.Ink.GetBoundingBox();
        byte[] gifbits = (byte[])axInkPicture1.Ink.Save(InkPersistenceFormat.IPF_GIF, InkPersistenceCompressionMode.IPCM_Default);
        using (System.IO.MemoryStream buffer = new System.IO.MemoryStream(gifbits))
        using (var inkImage = new Bitmap(buffer))
        using (var picture = new Bitmap(axInkPicture1.Picture))
        using (var g = Graphics.FromImage(picture))
        {
            g.DrawImage(inkImage, new Rectangle(inkBox.Left, inkBox.Right, axInkPicture1.Picture.Width, axInkPicture1.Height));
            picture.Save(tempFile, System.Drawing.Imaging.ImageFormat.Gif);
        }

...但我仍然没有任何运气。这仅保存背景图片而不保存墨水。任何想法我做错了什么?

提前致谢

4

2 回答 2

0

我想我没有足够的 50 声望来发表评论,所以我会用这个评论来回答其他可能在像我一样进行谷歌搜索后登陆这里的人:

这很有帮助。我翻译成VB,它工作。

经过一番折腾后,我在以下位置找到了 DLL:

C:\Program Files (x86)\Common Files\Microsoft Shared\Ink\Microsoft.Ink.dll

显然我不是唯一一个在定位 DLL 时遇到问题的人,因为有很多关于如何获取它的谷歌条目——很多都是错误的,而且大多数都不是那么简单。

如果您刚开始使用签名捕获,请记住将背景图像添加到您的控件,否则它将在此行失败:

使用 bmp = New Bitmap(InkPicture1.BackgroundImage)

于 2014-02-11T06:12:56.193 回答
0

Soooo....经过多次头部撞击和咬牙切齿后,我终于想出了解决问题的方法,尽管可能不是我预期的解决方案,也可能不是最好的解决方案,但它确实有效。我是 c# 和 .net 世界的新手,所以如果我在这里陈述任何愚蠢的事情,请随时纠正我。

我发现上面编辑中的代码实际上是有效的,我只是看不到墨水,因为它超出了矩形的边界。显然,使用 GetBoundingBox() 方法获取的矩形为您提供了与像素坐标大不相同的 inkSpace 坐标。

考虑到这一点,我的下一个目标是使用 inkSpaceToPixels 方法将 inkSpace 坐标转换为像素坐标。我为此苦苦挣扎了一段时间,永远无法弄清楚如何使用 activex 版本的 inkPicure 或 axInkPicture 来做到这一点。它需要一些对 hdcDisplay 的处理,我无法完全理解。

最后,我使用了来自 Microsoft.Ink.dll 的 .net 版本(Microsoft.Ink)。我开始使用 activeX 版本的唯一原因是由于某种原因我无法在我的开发机器上安装 Windows Tablet PC SDK。它一直抱怨我需要 WinXP Tablet PC OS 来安装它。我发现我可以将它安装在另一台(Win7)PC 上,然后从那台 PC 复制 .​​dll。

所以,这是现在正在运行的最终版本......哇!

        //set the temporary file paths
        string tempPath = Path.GetTempPath();
        string tempFile = tempPath + @"tmpFile.png";

        //get the bounding box and reference point for the ink strokes (in inkspace coordinates!)
        Rectangle inkBox = inkPicture1.Ink.GetBoundingBox();
        Point point = new Point(inkBox.Left, inkBox.Top);
        MessageBox.Show(point.X.ToString() + ", " + point.Y.ToString());//"before" coordinates

        // Convert inkspace coordinates to pixel coordinates using Renderer
        Graphics tempGraphics = CreateGraphics();
        Microsoft.Ink.Renderer inkRenderer = new Renderer();
        inkRenderer.InkSpaceToPixel(tempGraphics, ref point);;
        MessageBox.Show(point.X.ToString() + ", " + point.Y.ToString());//"after" coordinates
        // Clean up
        tempGraphics.Dispose();

        // save the ink and background image to a temp file
        if (inkPicture1.Ink.Strokes.Count > 0)
        {
            byte[] bytes = inkPicture1.Ink.Save(PersistenceFormat.Gif, CompressionMode.NoCompression);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                using (Bitmap gif = new Bitmap(ms))
                using (var bmp = new Bitmap(inkPicture1.BackgroundImage))
                using (var g = Graphics.FromImage(bmp))
                {
                    g.DrawImage(gif, new Rectangle(point.X, point.Y, gif.Width, gif.Height));
                    bmp.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);

                }
            }
        }

对于冗长的解释感到抱歉,但我觉得在没有充分解释的情况下简单地发布解决方案会留下太多悬而未决的问题......

于 2014-01-11T16:42:43.963 回答