Microsoft ISF 上的精确 BoundingBox?

发布于 2024-08-04 09:20:28 字数 928 浏览 5 评论 0原文

我有一组 Microsoft ISF(墨水序列化格式)图像,我正在尝试将其转换为 PNG 以包含在网页中。我已成功使用 C# 包 Microsoft.Ink 将墨水绘制到位图并另存为 PNG:

byte[] data; // data has the raw bytes of the ISF file
Ink ink = new Ink();
Renderer renderer = new Renderer();
Stream outStream; // a Stream to hold the PNG data
ink.Load(data);
using (Strokes strokes = ink.Strokes) {
  // get bounds of ISF
  rect = strokes.GetBoundingBox(BoundingBoxMode.PointsOnly);
  // create bitmap matching bounds
  bm = new Bitmap(rect.Width, rect.Height);
  // draw the ISF onto the Bitmap
  using (Graphics g = Graphics.FromImage(bm)) {
    g.Clear(Color.Transparent);
    renderer.Draw(g, strokes);
  }
}
// save the Bitmap to PNG format
bm.Save(outStream, System.Drawing.Imaging.ImageFormat.Png);

...但是,图像尺寸似乎异常大。更改传递给 GetBoundingBox 方法的 BoundingBoxMode 枚举似乎不会改变任何内容,我得到的图像尺寸为 465px × 660px,并且仅包含手写字母“a”,在实际空间中占用的空间可能为 25px × 30px。

关于如何获得更准确的边界框有什么建议吗?

I have a set of Microsoft ISF (Ink Serialized Format) images, which I am attempting to convert to PNGs to include in a web page. I have successfully used the C# package Microsoft.Ink to draw the ink to a Bitmap and save as PNG:

byte[] data; // data has the raw bytes of the ISF file
Ink ink = new Ink();
Renderer renderer = new Renderer();
Stream outStream; // a Stream to hold the PNG data
ink.Load(data);
using (Strokes strokes = ink.Strokes) {
  // get bounds of ISF
  rect = strokes.GetBoundingBox(BoundingBoxMode.PointsOnly);
  // create bitmap matching bounds
  bm = new Bitmap(rect.Width, rect.Height);
  // draw the ISF onto the Bitmap
  using (Graphics g = Graphics.FromImage(bm)) {
    g.Clear(Color.Transparent);
    renderer.Draw(g, strokes);
  }
}
// save the Bitmap to PNG format
bm.Save(outStream, System.Drawing.Imaging.ImageFormat.Png);

... however, the image dimensions seem to be abnormally large. Changing the BoundingBoxMode enum passed in to the GetBoundingBox method does not appear to change anything, and I'm getting images that are 465px × 660px and contain only a handwritten letter 'a' taking up maybe 25px × 30px in actual space.

Any suggestions on how to get a more accurate bounding box?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2024-08-11 09:20:29

边界框矩形位于“Inkspace”坐标中 - 创建一次性位图和图形对象,然后使用 renderer.InkSpacetoPixel 转换矩形。

在 GetBoundingBox 调用和位图 bm 创建之间添加这些行,并且位图的大小应正确:

Bitmap bmTrash = new Bitmap(10, 10);
Graphics gTrash = Graphics.FromImage(bmTrash);
renderer.InkSpaceToPixel(gTrash, rect.Location);
renderer.InkSpaceToPixel(gTrash, rect.Size);

The bounding box rectangle is in "Inkspace" coordinates - create throwaway bitmap and graphics objects, then convert the rectangle using renderer.InkSpacetoPixel.

Add these lines between the GetBoundingBox call and the creation of the Bitmap bm, and the Bitmap should be sized correctly:

Bitmap bmTrash = new Bitmap(10, 10);
Graphics gTrash = Graphics.FromImage(bmTrash);
renderer.InkSpaceToPixel(gTrash, rect.Location);
renderer.InkSpaceToPixel(gTrash, rect.Size);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文