我有以下代码例程...
Public Shared Sub ConvertToBitonal(ByRef Source As System.Windows.Media.Imaging.WriteableBitmap)
If Source.Format = System.Windows.Media.PixelFormats.BlackWhite Then
Exit Sub
End If
Dim BytesPerPixel As Integer = (Source.Format.BitsPerPixel + 7) / 8
Dim Stride As Integer = Source.PixelWidth * BytesPerPixel
Dim NumBytes As Long = Source.PixelHeight * Stride
Dim pixels As Byte() = New Byte(NumBytes - 1) {}
Source.CopyPixels(pixels, Stride, 0)
For cnt As Integer = 0 To pixels.Length - 1 Step BytesPerPixel
Dim blue As Byte = pixels(cnt + 0)
Dim green As Byte = pixels(cnt + 1)
Dim red As Byte = pixels(cnt + 2)
Dim intensity As Integer = CType(red, Integer) + CType(green, Integer) + CType(blue, Integer)
Dim targetColor As Byte
If intensity > 400 Then
targetColor = 255
Else
targetColor = 0
End If
pixels(cnt + 0) = targetColor
pixels(cnt + 1) = targetColor
pixels(cnt + 2) = targetColor
pixels(cnt + 3) = targetColor
Next
Source.WritePixels(New System.Windows.Int32Rect(0, 0, Source.PixelWidth, Source.PixelHeight), pixels, Stride, 0)
End Sub
当源图像为每像素 24 位时,输出完全符合我的要求,但当源图像为 32 位时,颜色不会呈现为实心,我会在整个图像中看到垂直线。有人可以告诉我如何修改例程以使 32 位图像像他们的 24 位计数器一样出来吗?
这是我正在谈论的屏幕截图......(显然我还没有足够的代表来发布图片,所以这里是一个链接