我目前正在编写一个代码片段,通过使用填充将栅格图像转换为矢量图像而不使用任何跟踪方法。它的工作原理,但对于960px * 800px图像,输出的大小可能会变得非常大(160 MB)。寻找能够让我了解如何构建某种位置数组的人,这些位置在每个相应的填充块中包含相同的颜色。我检查谷歌没有运气。以下是代码段的样子。
public static unsafe void SaveVector(this Image @this, Stream outputStream)
{
int PixelSize = 4;
Rectangle rect = new Rectangle(new Point(0,0), @this.Size);
using(StreamWriter SvgWriter = new StreamWriter(outputStream))
{
using(Bitmap bitmap = new Bitmap(@this))
{
BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
SvgWriter.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
SvgWriter.WriteLine("<svg");
SvgWriter.WriteLine(" xmlns:svg=\"http://www.w3.org/2000/svg\"");
SvgWriter.WriteLine(" xmlns=\"http://www.w3.org/2000/svg\"");
SvgWriter.WriteLine(string.Format(" width=\"{0}\"", @this.Width));
SvgWriter.WriteLine(string.Format(" height=\"{0}\"", @this.Height));
SvgWriter.WriteLine(" id=\"svg2\"");
SvgWriter.WriteLine(" version=\"1.1\">");
SvgWriter.WriteLine(" <defs");
SvgWriter.WriteLine(" id=\"defs4\" />");
SvgWriter.WriteLine(" <metadata");
SvgWriter.WriteLine(" id=\"metadata7\" />");
SvgWriter.WriteLine(" <g");
SvgWriter.WriteLine(" id=\"layer1\">");
int num = 0;
for(int y = 0; y < bitmapData.Height; y++)
{
byte* row=(byte *)bitmapData.Scan0+(y*bitmapData.Stride);
for(int x = 0; x < bitmapData.Width; x++)
{
SvgWriter.WriteLine(" <rect");
SvgWriter.WriteLine(string.Format(" style=\"fill:{0};fill-opacity:1;stroke-width:0.43599999000000000;stroke-miterlimit:4;stroke-dasharray:none\"",
ColorTranslator.ToHtml(Color.FromArgb(
row[(x * PixelSize) + 3],
row[(x * PixelSize) + 2],
row[(x * PixelSize) + 1],
row[x * PixelSize]))));
SvgWriter.WriteLine(string.Format(" id=\"rect{0}\"", num));
SvgWriter.WriteLine(" width=\"1\"");
SvgWriter.WriteLine(" height=\"1\"");
SvgWriter.WriteLine(string.Format(" x=\"{0}\"", x));
SvgWriter.WriteLine(string.Format(" y=\"{0}\" />", y));
num++;
}
}
bitmap.UnlockBits(bitmapData);
}
SvgWriter.WriteLine(" </g>");
SvgWriter.WriteLine("</svg>");
}
}