Calculate Graphics Path From Bitmap - CSharp System.Drawing.Drawing2D

CSharp examples for System.Drawing.Drawing2D:GraphicsPath

Description

Calculate Graphics Path From Bitmap

Demo Code


using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System;/* w w  w  .  j ava  2 s  . c o  m*/

public class Main{
        // From http://edu.cnzz.cn/show_3281.html
      public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap, Color colorTransparent) 
      { 
         GraphicsPath graphicsPath = new GraphicsPath(); 
         if (colorTransparent == Color.Empty)
            colorTransparent = bitmap.GetPixel(0, 0); 

         for(int row = 0; row < bitmap.Height; row ++) 
         { 
            int colOpaquePixel = 0;
            for(int col = 0; col < bitmap.Width; col ++) 
            { 
               if(bitmap.GetPixel(col, row) != colorTransparent) 
               { 
                  colOpaquePixel = col; 
                  int colNext = col; 
                  for(colNext = colOpaquePixel; colNext < bitmap.Width; colNext ++) 
                     if(bitmap.GetPixel(colNext, row) == colorTransparent) 
                        break;
 
                  graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); 
                  col = colNext; 
               } 
            } 
         } 
         return graphicsPath; 
      }
        public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap)
      {
         return CalculateGraphicsPathFromBitmap(bitmap, Color.Empty);
      }
}

Related Tutorials