Pregunta

I have the following code routine...

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

When the source image is of the 24 bit per pixel variety the output comes out exactly how I want it, but when the source image is 32 bits the colors do not come out solid and I get vertical lines running throughout the image. Can someone show me how to modify the routine so that the 32 bit images come out like their 24 bit counter parts?

Here is a screen shot of what I'm talking about... (Apparently I don't have enough rep yet to post images so here is a link instead

enter image description here

¿Fue útil?

Solución

You might have to create a 24bpp copy of the image first. Have you tried that?

I did something similar long ago (too long ago to recall details), this article was helpful: Bitonal conversion issues. There was also some info on MSDN forum.

EDIT: seen this entry?

Otros consejos

I dug around the code and see the problem:

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

Remove the + 7.

Also change this:

    pixels(cnt + 0) = targetColor

    pixels(cnt + 1) = targetColor

    pixels(cnt + 2) = targetColor

    pixels(cnt + 3) = targetColor

to a FOR loop encompassing the step..... i.e.:

        Dim x%

        For x = cnt To cnt + BytesPerPixel - 1

            pixels(x) = targetColor

        Next

If the step is > 4, it's not going to set the value and will give you this bar going down it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top