Thanks four your response.
I found a VB.NET code to transform from color image to bi-level image.
I try to transform to C# which I used, but there is always several
error I can't solve. Is anyone can see the error reason and tell me .
Thanks a lot.
-----------------------------------------------------------------------------------------------
Public Function GetBWBitmap(ByVal src As Bitmap) As Bitmap
Dim width, height, index, maskIndex As Integer
Dim dest As Bitmap
Dim row, col As Integer
Dim bmdDest As BitmapData
width = src.Width
height = src.Height
'Create a new bitmap of the proper format
dest = New Bitmap(width, height, PixelFormat.Format1bppIndexed)
bmdDest = dest.LockBits(New Rectangle(0, 0, width, height),
dest.Flags, dest.PixelFormat)
Dim myBytes(bmdDest.Stride * height - 1), temp(0) As Byte
'Copy the src bitmap to the array of bytes
For row = 0 To height - 1
For col = 0 To width - 1
index = row * bmdDest.Stride + ((col - 3.5) / 8)
maskIndex = Math.Abs((col Mod 8) - 7)
temp(0) = myBytes(index)
Dim myBits As New BitArray(temp)
Dim myMask As New BitArray(8, 0)
If (src.GetPixel(col, row).GetBrightness() > 0.5F) Then
myMask(maskIndex) = True
End If
myBits.Or(myMask)
myBits.CopyTo(myBytes, index)
Next
Next
'Copy array of bytes into destination bitmap
Marshal.Copy(myBytes, 0, bmdDest.Scan0, bmdDest.Stride * height
- 1)
dest.UnlockBits(bmdDest)
GetBWBitmap = dest
End Function
----------------------------------------------------------------------------------------------------
public Bitmap ImageConvert(Bitmap SourceImg)
{
//Declare Variable
int ImgWidth;
int ImgHeight;
Bitmap DestinationImg;
BitmapData DestinationData;
//Set SourceImg width and height to variables imgwidth and
imgheight
ImgWidth = SourceImg.Width;
ImgHeight = SourceImg.Height;
//Create a new bitmap of the proper format
//Set DestinationImg to the Black and White formate,
Format1bppindexed.
//Specifies that the pixel format is 1 bit per pixel and
that it uses indexed color.
//The color table therefore has two colors in it.
DestinationImg = new Bitmap(ImgWidth, ImgHeight,
PixelFormat.Format1bppIndexed);
//Locks a Bitmap into system memory.
DestinationData = DestinationImg.LockBits(new Rectangle(0,
0, ImgWidth, ImgWidth), DestinationImg.Flags,
DestinationImg.PixelFormat);
//Copy the SourceImg bitmap to the array of bytes
byte[] TempBytes;
byte[] ImgBytes = new
byte[DestinationData.Stride*ImgHeight-1];
int index;
int maskIndex;
for (int row = 0; row <= ImgHeight - 1; row++)
{
for (int col = 0; col <= ImgWidth - 1; col++)
{
index = row * DestinationData.Stride + (int)((col -
3.5) / 8);
maskIndex = Math.Abs((col % 8) - 7);
TempBytes = ImgBytes;
BitArray myBits = new BitArray(TempBytes);
BitArray myMask = new BitArray(8,true);
if ((SourceImg.GetPixel(col, row).GetBrightness() >
0.5F))
{
myMask[maskIndex]= true;
}
myBits.Or(myMask);
myBits.CopyTo(ImgBytes, index);
}
}
Marshal.Copy(ImgBytes, 0, DestinationData.Scan0,
DestinationData.Stride * ImgHeight - 1);
DestinationImg.UnlockBits(DestinationData);
return DestinationImg;
}
|