读取像素,和一个阀值比较,大于就是黑,小于就是白

解决方案 »

  1.   

    假设你的字颜色是红的。if (bmpNew != null) bmpNew.Dispose ();
    int iWidth, iHeight;//打开图的到宽高        
    Bitmap bmpTmp = (Bitmap)System.Drawing.Image.FromFile(Request.PhysicalApplicationPath+ strFileNameMaze); 
    iWidth = bmpTmp.Width;
    iHeight = bmpTmp.Height; 
            
    //放到新图里。
    bmpNew = new Bitmap (bmpTmp.Width, bmpTmp.Height);
    Graphics g = Graphics.FromImage (bmpNew);
    g.DrawImage (bmpTmp, 0,0);//比红的小变成白,大变成黑
    Rectangle rc = new Rectangle (0,0,bmpNew.Width, bmpNew.Height);
    BitmapData d = bmpNew.LockBits (rc, ImageLockMode.ReadWrite, bmpNew.PixelFormat);
            
    unsafe 
    {     
    int iRow, iCol;
    int* p = (int*)d.Scan0;
    int numPixels = d.Width * d.Height;
    int[,] q = new int[d.Height,d.Width];
          
    for(iRow=0;iRow<d.Height;iRow++)
    for(iCol=0;iCol<d.Width;iCol++)

    if(((*p)&(0x00ffffff))<Color.Red.ToArgb())
    *p = Color.White.ToArgb();
    else if(((*p)&(0x00ffffff))>Color.Red.ToArgb())
    *p = Color.Black.ToArgb();
    p++;

    }     
            
    // 扫尾
    bmpNew.UnlockBits (d);
    bmpTmp.Dispose ();
    g.Dispose ();//存新图        
    bmpNew.Save(Request.PhysicalApplicationPath + strFileNameMazeNow,System.Drawing.Imaging.ImageFormat.Bmp);