title: Converting Raster to Vector?
body: <p>I'm currently in the process of writing a code snippet that converts a raster image to a vector image without using any tracing method by using fill. It works but the size of the out put can become quite large(160 MB) for 960px * 800px image. Looking for someone who may be able to give me an idea how we can build some sort of an array of positions that contains the same colors within each corresponding fill blocks. I've checked google with no luck. Here is what the code snippet looks like.<br></p>
  
  <pre><code>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("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?&gt;");
        SvgWriter.WriteLine("&lt;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\"&gt;");
        SvgWriter.WriteLine("  &lt;defs");
        SvgWriter.WriteLine("     id=\"defs4\" /&gt;");
        SvgWriter.WriteLine("  &lt;metadata");
        SvgWriter.WriteLine("     id=\"metadata7\" /&gt;");
        SvgWriter.WriteLine("  &lt;g");
        SvgWriter.WriteLine("     id=\"layer1\"&gt;");
        int num = 0;              
        for(int y = 0; y &lt; bitmapData.Height; y++)
        {
          byte* row=(byte *)bitmapData.Scan0+(y*bitmapData.Stride);
          for(int x = 0; x &lt; bitmapData.Width; x++)
          {
            SvgWriter.WriteLine("    &lt;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}\" /&gt;", y));                         
            num++;
          }                       
        }
        bitmap.UnlockBits(bitmapData);
      }
      SvgWriter.WriteLine("  &lt;/g&gt;");
      SvgWriter.WriteLine("&lt;/svg&gt;");
    }
  }
  </code></pre>