home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Covert To BW


          public static Bitmap get1BitPerPixelBitmap(System.Drawing.Image img)
          {
               
               System.Drawing.Bitmap img1 = (Bitmap)img; 
               if (img1.PixelFormat == PixelFormat.Format1bppIndexed)
                    return (Bitmap)img;

               System.Drawing.Bitmap bm = new  System.Drawing.Bitmap(
                    img1.Width,
                    img1.Height,
                    System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

               System.Drawing.Imaging.BitmapData bmd =
                    bm.LockBits(new  Rectangle(0, 0, bm.Width, bm.Height), 
                    ImageLockMode.ReadWrite, 
                    PixelFormat.Format1bppIndexed );

               for(int y=0;y<img1.Height;y++)
               {
                    for(int x=0;x<img1.Width;x++)
                    {
                         if(img1.GetPixel(x,y).GetBrightness()>0.5f)
                              SetIndexedPixel(x,y,bmd,true);
                    }
               }
               
               bm.UnlockBits(bmd);

               return bm;
          }


          private unsafe void SetIndexedPixel(int  x,int y,BitmapData bmd, bool pixel)
          {
               byte* p=(byte*)bmd.Scan0.ToPointer();
               int index=y*bmd.Stride+(x>>3);
               byte mask=(byte)(0x80>>(x&0x7));

               if(pixel)
                    p[index]|=mask;
               else
                    p[index]&=(byte)(mask^0xff);
          }


Last Modified 10/12/04 2:45 PM

Hide Tools