【发布时间】:2018-02-14 17:48:47
【问题描述】:
我正在尝试将 SVG 转换为 PNG 文件。我的主要问题不是转换本身(我可以用几行代码完成),而是我想要发送到渲染的“窗口”中的 SVG 元素的居中。
我使用 SVG 引擎 (https://github.com/vvvv/SVG),我的想法是获取要渲染的元素,然后将其重新定位在原点,然后将 svg 文档的宽度和高度设置为元素的宽度/高度。 这是代码:
var svgDocument = SvgDocument.Open(filename);
var el =(SvgVisualElement) svgDocument.GetElementById("MyId");
// set in the origin
el.Transforms.Add(new SvgTranslate(-el.Bounds.X, -el.Bounds.Y));
// set doc dimensions
svgDocument.Width = el.Bounds.Width;
svgDocument.Height = el.Bounds.Height;
bitmap.Save("PNGFile", ImageFormat.Png);
不幸的是,它不是那样工作的。 这是一个不起作用的 svg 示例(它很冗长,所以我剪掉了相关部分):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="70.125473"
height="190.98564"
id="svg6678"
version="1.1">
<g id="MyId">
<g
transform="translate(-2375.7448,475.88408)"
id="Texture:door_single_rocks_gear">
<path
id="path9179-8-2-8-71-4-2-7"
d="..." style="color:#000000;display:inline;overflow:visible;visibility:visible;fill:#5b3636;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1.53245842;marker:none;enable-background:accumulate" />
<path
id="path9179-8-2-8-71-4-6"
d="..." style="color:#000000;display:inline;overflow:visible;visibility:visible;fill:#815656;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1.53245842;marker:none;enable-background:accumulate" />
<path
id="path4080-5-2"
d="..."
style="fill:#866262;fill-opacity:1;stroke:none" />
<path
id="path7662-1-1-4-9"
d="..."
style="fill:#5b3636;fill-opacity:1;stroke:none" />
<path
id="path7662-1-1-4-5-5"
d="..."
style="fill:#5b3636;fill-opacity:1;stroke:none" />
</g>
<g
id="Texture:door_single_rocks"
inkscape:label="#g5299"
inkscape:export-filename="C:\Users\stefano.liboni\Dropbox\AndroidStudio\AsteroidRage\scenes_source\door_single_rocks.png"
inkscape:export-xdpi="199.88191"
inkscape:export-ydpi="199.88191">
<path style="color:#000000;display:inline;overflow:visible;visibility:visible;fill:#5b3636;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:3;marker:none;enable-background:accumulate"
d="..."
id="rect4972-3-8-9" />
<path
id="rect4972-9"
d="..." style="color:#000000;display:inline;overflow:visible;visibility:visible;fill:#815656;fill-opacity:1;fill-rule:nonzero;stroke:#5b3636;stroke-width:1.0026859;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker:none;enable-background:accumulate" />
<path
style="fill:#5b3636;fill-opacity:1;stroke:none"
d="..."
id="path7662-1-1-4-5-5-0" />
<path
style="fill:#5b3636;fill-opacity:1;stroke:none"
d="..."
id="path7662-1-1-4-5-5-03-9" />
<path
style="fill:#5b3636;fill-opacity:1;stroke:none"
d="..."
id="path7662-1-1-4-5-5-03" />
</g>
<g
id="AnimatedTexture:door_single_rocks_led"
inkscape:label="#g5295">
<path
id="path9095-7"
d="..."
style="fill:#9486ff;fill-opacity:1;stroke:#000000;stroke-width:1.07693017;stroke-opacity:1" />
<path
id="path9095-7-5-1"
d="..."
style="fill:#9486ff;fill-opacity:1;stroke:#000000;stroke-width:1.07693017;stroke-opacity:1" />
</g>
</g>
</svg>
如您所见,有一个 g 元素 MyId 充满了其他 g 元素和一些路径。我添加了它在inkscape中渲染的图像。
一旦我在我的代码中获得了元素的边界(即左下角的那个),我会得到: {X = -1502.69763 Y = 668.7914 宽度 = 2514.11938 高度 = 870.0564}
这似乎是错误的,因为元素只有 141 像素的宽度和 395 像素的高度...... 如果我在该文件上运行代码,则输出为:
所以PNG有很多未使用的空间,png的y,宽度和高度都是错误的。正确的应该是:
svgDocument.X = 0;
svgDocument.Y = -476;
svgDocument.Width = 141;
svgDocument.Height = 395;
知道我做错了什么吗?
注意:我知道 inkscape 会做对(我曾经使用它),但是至少有 3 年以来他们没有费心去解决一个错误,如果你指定“动词”,它会阻止 inkscape 在静默模式下运行" 在命令行中。所以取消它是一件痛苦的事情,因为对于每个文件,它都会打开 UI 并需要 3-4 秒才能完成......
【问题讨论】:
-
------------------------------------------ - - - - - - - - - - - - - - - - - - - 更新 - - - - - - ----------------------------------------- 正如我认为的那样,它与我要渲染的元素内的 g 元素的 transfrom 属性。如果我去掉所有的变换属性,它就可以正常工作,不仅适用于这个例子,而且适用于更复杂的几何图形。如何考虑这些转换?
-
您能详细解释一下您遇到的inkscape CLI 问题吗?我正在成功地使用它来完成您描述的或多或少相同的任务。
标签: c# svg bounding-box inkscape