Read and write an image's pixels using GetDIBits and SetDIBits

Initialize a BITMAPINFO structure to describe the image. Redimension an array of Byte to hold pixel data. The array's entries will contain:

 pixels(1, X, Y)Blue component of pixel (X, Y)
 pixels(2, X, Y)Green component of pixel (X, Y)
 pixels(3, X, Y)Red component of pixel (X, Y)
 pixels(4, X, Y)Padding

 

The code then calls GetDIBits to fetch the data from a picture. It loops over all of the pixels, setting their blue color components to 255. This generally makes the image bluer.

It then loops over the pixels along a diagonal in the upper left quarter of the image. It sets the red, green, and blue components of those pixels to 0 to make them black. The result shows that the pixel indexing matches the normal PictureBox mapping where X increases from left to right and Y increases from top to bottom.

After it modifies the pixels, the code uses SetDIBits to copy the data into the output PictureBox. It finishes by setting picTo.Picture = picTo.Image to make the results visible.

Private Sub cmdGo_Click()
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer

    ' Prepare the bitmap description.
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = picFrom.ScaleWidth
        ' Use negative height to scan top-down.
        .biHeight = -picFrom.ScaleHeight
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
            31) \ 32) * 4)
        pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
            * .biBitCount) + 7) \ 8)
        .biSizeImage = bytes_per_scanLine * Abs(.biHeight)
    End With

    ' Load the bitmap's data.
    ReDim pixels(1 To 4, 1 To picFrom.ScaleWidth, 1 To _
        picFrom.ScaleHeight)
    GetDIBits picFrom.hdc, picFrom.Image, _
        0, picFrom.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS

    ' Modify the pixels.
    For Y = 1 To picFrom.ScaleHeight
        For X = 1 To picFrom.ScaleWidth
            ' Set the blue component to 255.
            pixels(pixB, X, Y) = 255
        Next X
    Next Y

    For X = 1 To picFrom.ScaleHeight \ 2
        ' Make this pixel black.
        pixels(pixR, X, X) = 0
        pixels(pixG, X, X) = 0
        pixels(pixB, X, X) = 0
    Next X

    ' Display the result.
    SetDIBits picTo.hdc, picTo.Image, _
        0, picFrom.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS
    picTo.Picture = picTo.Image
End Sub

转载于:https://www.cnblogs.com/Blessing/archive/2012/09/08/2676717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值