Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to load bitmap data into array from file to perform image operation?
[ All Languages » VB »  Bitmap]

Total Hit ( 5317)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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 ' 40 bytes
  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
  ' Load image off disk
  Set MyPic = LoadPicture("c:\8bit.bmp")

  ' Set header size and query Bitmap information
  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  ' Read Bitmap palette
      Call GetDIBits(Form1.hDC, MyPic.Handle, 0, 0, ByVal 0&, BMInf, 0)
      BMInf.bmiHeader.biClrUsed = ClrUsed
    End If

    ' Scale image data array and extract image data
    ReDim BMData(BMInf.bmiHeader.biSizeImage - 1) As Byte
    Call GetDIBits(Form1.hDC, MyPic.Handle, 0, _
        BMInf.bmiHeader.biHeight, BMData(0), BMInf, 0)

    ' Perform image adjustment here - Simple invert for demonstration
    For LoopData = 0 To BMInf.bmiHeader.biSizeImage - 1
      BMData(LoopData) = BMData(LoopData)
    Next LoopData

    ' Inject data back into Bitmap
    Call SetDIBits(Form1.hDC, MyPic.Handle, 0, _
        BMInf.bmiHeader.biHeight, BMData(0), BMInf, 0)

    ' Display edited image on form's surface
    Set Form1.Picture = MyPic
  End If

  ' Clean up local reference
  Set MyPic = Nothing
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.