To modift RGB value of bitmap Load the image using LoadPicture() then call GetDIBits() on it's handle to extract the image data and perform the edits you want, then finally SetDIBits() the data back and display where you wish: In our next article I will show you how to get pointer to 2D pixel array so you can directly modify values without using SetDIBits. This technique is called SAFEARRAY technique which gives you pointer to 2D array instead of one dimentional array which is hard to manipulate.
Just copy/paste the following code in to your form and make sure that you modify the path of bitmap |
Click here to copy the following block | Private Declare Function GetDIBits Lib "GDI32.dll" (ByVal aHDC As Long, _ ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, _ ByRef lpBits As Any, ByRef lpBI As BitmapInfo, ByVal wUsage As Long) As Long Private Declare Function SetDIBits Lib "GDI32.dll" (ByVal hDC As Long, _ ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, _ ByRef lpBits As Any, ByRef lpBI As BitmapInfo, ByVal wUsage As Long) As Long
Private Type BitmapInfoHeader biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End Type
Private Type BitmapInfo bmiHeader As BitmapInfoHeader bmiColors(255) As Long End Type
Private Sub Form_Load() Dim MyPic As StdPicture Dim BMInf As BitmapInfo Dim ClrUsed As Long Dim BMData() As Byte Dim LoopData As Long Me.AutoRedraw = True Set MyPic = LoadPicture("c:\8bit.bmp")
BMInf.bmiHeader.biSize = Len(BMInf.bmiHeader) If (GetDIBits(Form1.hDC, MyPic.Handle, 0, 0, ByVal 0&, BMInf, 0)) Then If (BMInf.bmiHeader.biBitCount <= 8) Then ClrUsed = BMInf.bmiHeader.biClrUsed Call GetDIBits(Form1.hDC, MyPic.Handle, 0, 0, ByVal 0&, BMInf, 0) BMInf.bmiHeader.biClrUsed = ClrUsed End If
ReDim BMData(BMInf.bmiHeader.biSizeImage - 1) As Byte Call GetDIBits(Form1.hDC, MyPic.Handle, 0, _ BMInf.bmiHeader.biHeight, BMData(0), BMInf, 0)
For LoopData = 0 To BMInf.bmiHeader.biSizeImage - 1 BMData(LoopData) = BMData(LoopData) Next LoopData
Call SetDIBits(Form1.hDC, MyPic.Handle, 0, _ BMInf.bmiHeader.biHeight, BMData(0), BMInf, 0)
Set Form1.Picture = MyPic End If
Set MyPic = Nothing End Sub |
|