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

コードを掘り下げて問題を確認しました。

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 に答える