1

我有以下代码例程...

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 位计数器一样出来吗?

这是我正在谈论的屏幕截图......(显然我还没有足够的代表来发布图片,所以这里是一个链接

在此处输入图像描述

4

2 回答 2

0

您可能必须先创建映像的 24bpp 副本。你试过吗?

我很久以前做过类似的事情(很久以前回忆细节),这篇文章很有帮助:双色调转换问题。MSDN论坛上也有一些信息。

编辑:看到这个条目了吗?

于 2013-09-23T16:12:28.917 回答
0

我挖掘了代码并看到了问题:

Dim BytesPerPixel As Integer = (Source.Format.BitsPerPixel + 7) / 8

删除+ 7.

也改变这个:

    pixels(cnt + 0) = targetColor

    pixels(cnt + 1) = targetColor

    pixels(cnt + 2) = targetColor

    pixels(cnt + 3) = targetColor

到包含该步骤的 FOR 循环.....即:

        Dim x%

        For x = cnt To cnt + BytesPerPixel - 1

            pixels(x) = targetColor

        Next

如果步长大于 4,则不会设置该值,并且会为您提供此条。

于 2016-12-31T02:04:36.850 回答