C# 将光栅转换为矢量?
我目前正在编写一个代码段,该代码段可以将光栅图像转换为矢量图像,而无需使用fill的任何跟踪方法。它可以工作,但对于960px*800px的图像,输出的大小可能会变得相当大(160MB)。我想找一个能给我一个想法的人,我们如何在每个对应的填充块中构建一个包含相同颜色的位置数组。我查过谷歌,但运气不好。下面是代码片段的样子。C# 将光栅转换为矢量?,c#,image-processing,vector,vector-graphics,raster,C#,Image Processing,Vector,Vector Graphics,Raster,我目前正在编写一个代码段,该代码段可以将光栅图像转换为矢量图像,而无需使用fill的任何跟踪方法。它可以工作,但对于960px*800px的图像,输出的大小可能会变得相当大(160MB)。我想找一个能给我一个想法的人,我们如何在每个对应的填充块中构建一个包含相同颜色的位置数组。我查过谷歌,但运气不好。下面是代码片段的样子。 public static unsafe void SaveVector(this Image @this, Stream outputStream) { int Pi
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>");
}
}
publicstaticsavevector(此图像@this,streamoutputstream)
{
int PixelSize=4;
矩形rect=新矩形(新点(0,0),@this.Size);
使用(StreamWriter SvgWriter=newstreamwriter(outputStream))
{
使用(位图位图=新位图(@this))
{
BitmapData BitmapData=位图.LockBits(rect、ImageLockMode.ReadOnly、PixelFormat.Format32bppArgb);
SvgWriter.WriteLine(“”);
SvgWriter.WriteLine(“”);
SvgWriter.WriteLine(“”);
SvgWriter.WriteLine(“”);
SvgWriter.WriteLine(“”);
int num=0;
对于(int y=0;y