【问题标题】:Change red pixels to blue in a bitmap将位图中的红色像素更改为蓝色
【发布时间】:2019-02-07 21:34:47
【问题描述】:

我想将red 像素更改为blue。图像为 24 位 .bmp。我正在使用lockbits,因为它更快,但代码没有找到红色像素!

代码:

Dim bmp As Bitmap = New Bitmap("path")
Dim pos As Integer
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits _
        (rect, Drawing.Imaging.ImageLockMode.ReadWrite,
        bmp.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
Marshal.Copy(ptr, rgbValues, 0, bytes)

For y = 0 To bmp.Height - 1
    For x = 0 To bmp.Width - 1
        pos = y * bmp.Width * 3 + x * 3

        If rgbValues(pos) = 255 And rgbValues(pos + 1) = 0 And rgbValues(pos + 2) = 0 Then
            rgbValues(pos + 2) = 255
            rgbValues(pos) = 0
        End If
    Next
Next

Marshal.Copy(rgbValues, 0, ptr, bytes)
bmp.UnlockBits(bmpData)
bmp.Save("new path")

谢谢!

【问题讨论】:

    标签: vb.net bitmap


    【解决方案1】:

    rgbValues 中存储的值不是按此顺序

    R G B R G B.....
    

    但是

    B G R B G R.....
    

    所以你的循环中正确的代码是:

    '       B                      G                            R
    If rgbValues(pos) = 0 And rgbValues(pos + 1) = 0 And rgbValues(pos + 2) = 255 Then
        rgbValues(pos + 2) = 0 'R
        rgbValues(pos) = 255 'B
    End If
    

    【讨论】:

    • 哇!快速回答。让我检查一下
    猜你喜欢
    • 2017-08-19
    • 2022-12-19
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多