Solved

How to show picture in picture box from array?

Posted on 2001-08-09
11
700 Views
Last Modified: 2008-01-09
If I have array which contain RGB of each pixel , how can I show it to Picture box?
0
Comment
Question by:rapeepak
11 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 6369241
One thing you can do is loop through the array and call picture1.PSet (x, y), lngColor(x,y)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6369256
A better way would be to use SetDIBIts, but you have to make sure your array is in the right order.  This is an example of the API call, but it needs modifying to your needs.  (I'm too lazy to do the work, right now, so if someone else wants the points...)

Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
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 RGBQUAD
        rgbBlue As Byte
        rgbGreen As Byte
        rgbRed As Byte
        rgbReserved As Byte
End Type
Private Type BITMAPINFO
        bmiHeader As BITMAPINFOHEADER
        bmiColors As RGBQUAD
End Type
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Dim iBitmap As Long, iDC As Long
Private Sub Form_Paint()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    '-> Compile this code for better performance
    Dim bi24BitInfo As BITMAPINFO, bBytes() As Byte, Cnt As Long
    With bi24BitInfo.bmiHeader
        .biBitCount = 24
        .biCompression = BI_RGB
        .biPlanes = 1
        .biSize = Len(bi24BitInfo.bmiHeader)
        .biWidth = 100
        .biHeight = 100
    End With
    ReDim bBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte
    iDC = CreateCompatibleDC(0)
    iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
    SelectObject iDC, iBitmap
    BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, GetDC(0), 0, 0, vbSrcCopy
    GetDIBits iDC, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, bBytes(1), bi24BitInfo, DIB_RGB_COLORS
    For Cnt = LBound(bBytes) To UBound(bBytes)
        If bBytes(Cnt) < 50 Then
            bBytes(Cnt) = 0
        Else
            bBytes(Cnt) = bBytes(Cnt) - 50
        End If
    Next Cnt
    SetDIBitsToDevice Me.hdc, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, bi24BitInfo.bmiHeader.biHeight, bBytes(1), bi24BitInfo, DIB_RGB_COLORS
    DeleteDC iDC
    DeleteObject iBitmap
End Sub

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6369510
Paul: no thanks ;)
0
 
LVL 11

Expert Comment

by:Otana
ID: 6371540
You could use the SetPixel API Call.

Loop through the pixels in your picture simultaneously with your array.

Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Dim i As Integer
Dim j As Integer
Dim test(5, 10) As Long

For i = 0 To 1
    For j = 0 To 10
        Call SetPixel(Picture1.hdc, i+1, j+1, test(i, j))
    Next j
Next i
0
 
LVL 2

Expert Comment

by:JanusFury
ID: 6376657
Even better! Instead of doing it the correct way with SetDIBits, you could do this:

Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal
crColor As Long) As Long

Dim i As Integer
Dim j As Integer
Dim test(5, 10) As Long
Dim blah as long, blah2 as long, blah3 as long

For i = 0 To 1
   For j = 0 To 10
       blah = test(i, j)
       blah2 = blah
       blah3 = blah2
       Call SetPixel(Picture1.hdc, i+1, j+1, blah3)
       DoEvents
       Me.Caption = "Writing pixel " + CStr(i) + ", " + CStr(j)
       Sleep 100
   Next j
Next i

If it's not worth doing right, don't do it.

Use SetDIBits, don't waste your time with inefficent SetPixel.

If nobody wants to write the code, I'll do it, even!
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Expert Comment

by:JanusFury
ID: 6376660
btw, I just noticed - His SetDIBits code is a tad complicated for filling a picturebox with an array, there's a way of doing it with SetDIBits that's simpler yet. I'll write up some code.
0
 
LVL 2

Accepted Solution

by:
JanusFury earned 150 total points
ID: 6376684
Here's a pair of functions that should work perfectly for you. (As long as your array is of Longs, that is.)
Paste it into a module:

' Array to Picture Box / Form
' Code by Janus (janusfury@citlink.net)

Public 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

Public Type RGBQUAD
        rgbBlue As Byte
        rgbGreen As Byte
        rgbRed As Byte
        rgbReserved As Byte
End Type

Public Type RGBTRIPLE
        rgbtBlue As Byte
        rgbtGreen As Byte
        rgbtRed As Byte
End Type

Public Type BITMAPINFO
        bmiHeader As BITMAPINFOHEADER
        bmiColors As RGBQUAD
End Type

Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal length As Long)
Public Declare Function SetDIBits Lib "gdi32" (ByVal hDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Public Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long

Sub ArrayToPictureBox(ByRef PictureBox As PictureBox, ByRef ImageArray() As Long)
On Error Resume Next
Dim MinX As Long, MinY As Long
Dim MaxX As Long, MaxY As Long
Dim SetX As Long, SetY As Long
Dim DestAddress As Long, YOffset As Long
Dim bmiDest As BITMAPINFO
Dim DIBitsDest() As RGBQUAD
    MinX = LBound(ImageArray, 1)
    MinY = LBound(ImageArray, 2)
    MaxX = UBound(ImageArray, 1)
    MaxY = UBound(ImageArray, 2)
   
    With bmiDest.bmiHeader
        .biSize = Len(bmiDest.bmiHeader)
        .biPlanes = 1
    End With
   
    If PictureBox.Image Is Nothing Then
        Exit Sub
    End If
   
   
    Set PictureBox.Picture = PictureBox.Image
   
    ' Get header information (interested mainly in size)
    If 0 = GetDIBits(PictureBox.hDC, PictureBox.Picture.Handle, 0, 0, ByVal 0&, bmiDest, DIB_RGB_COLORS) Then
        Exit Sub
    End If

    With bmiDest.bmiHeader
        .biBitCount = 32
        .biCompression = BI_RGB
    End With
   
    YOffset = (bmiDest.bmiHeader.biHeight - 1) - MaxY

    ' Allocate space according to retrieved size
    ReDim DIBitsDest(0 To bmiDest.bmiHeader.biWidth * bmiDest.bmiHeader.biHeight - 1)
   
    ' Now get the bits
    If 0 = GetDIBits(PictureBox.hDC, PictureBox.Picture.Handle, 0, Abs(bmiDest.bmiHeader.biHeight), DIBitsDest(0), bmiDest, DIB_RGB_COLORS) Then
        Exit Sub
    End If
   
    For SetY = MinY To MaxY
        For SetX = MinX To MaxX
            DestAddress = (SetX - MinX) + ((((SetY - MinY) + (YOffset))) * bmiDest.bmiHeader.biWidth)
            ' Copy the array data to the image
            CopyMemory DIBitsDest(DestAddress), ImageArray(SetX, (MaxY - SetY)), 4
        Next SetX
    Next SetY
   
    SelectObject PictureBox.hDC, 0
    Call SetDIBits(PictureBox.hDC, PictureBox.Picture.Handle, 0, Abs(bmiDest.bmiHeader.biHeight), DIBitsDest(0), bmiDest, DIB_RGB_COLORS)
    SelectObject PictureBox.hDC, PictureBox.Picture.Handle
   
    PictureBox.Refresh
End Sub

Sub ArrayToForm(ByRef Form As Form, ByRef ImageArray() As Long)
On Error Resume Next
Dim MinX As Long, MinY As Long
Dim MaxX As Long, MaxY As Long
Dim SetX As Long, SetY As Long
Dim DestAddress As Long, YOffset As Long
Dim bmiDest As BITMAPINFO
Dim DIBitsDest() As RGBQUAD
    MinX = LBound(ImageArray, 1)
    MinY = LBound(ImageArray, 2)
    MaxX = UBound(ImageArray, 1)
    MaxY = UBound(ImageArray, 2)
   
    With bmiDest.bmiHeader
        .biSize = Len(bmiDest.bmiHeader)
        .biPlanes = 1
    End With
   
    If Form.Image Is Nothing Then
        Exit Sub
    End If
   
   
    Set Form.Picture = Form.Image
   
    ' Get header information (interested mainly in size)
    If 0 = GetDIBits(Form.hDC, Form.Picture.Handle, 0, 0, ByVal 0&, bmiDest, DIB_RGB_COLORS) Then
        Exit Sub
    End If

    With bmiDest.bmiHeader
        .biBitCount = 32
        .biCompression = BI_RGB
    End With
   
    YOffset = (bmiDest.bmiHeader.biHeight - 1) - MaxY

    ' Allocate space according to retrieved size
    ReDim DIBitsDest(0 To bmiDest.bmiHeader.biWidth * bmiDest.bmiHeader.biHeight - 1)
   
    ' Now get the bits
    If 0 = GetDIBits(Form.hDC, Form.Picture.Handle, 0, Abs(bmiDest.bmiHeader.biHeight), DIBitsDest(0), bmiDest, DIB_RGB_COLORS) Then
        Exit Sub
    End If
   
    For SetY = MinY To MaxY
        For SetX = MinX To MaxX
            DestAddress = (SetX - MinX) + ((((SetY - MinY) + (YOffset))) * bmiDest.bmiHeader.biWidth)
            ' Copy the array data to the image
            CopyMemory DIBitsDest(DestAddress), ImageArray(SetX, (MaxY - SetY)), 4
        Next SetX
    Next SetY
   
    SelectObject Form.hDC, 0
    Call SetDIBits(Form.hDC, Form.Picture.Handle, 0, Abs(bmiDest.bmiHeader.biHeight), DIBitsDest(0), bmiDest, DIB_RGB_COLORS)
    SelectObject Form.hDC, Form.Picture.Handle
   
    Form.Refresh
End Sub

Simply call the appropriate function with a Form or PictureBox, and your array.

Email me with any questions, and if you want more control over the location of the image in the picturebox/form, or if you want to use a different data type, let me know and i'll modify the code.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6376766
Don't do this by email, we all want to see how this works out.  And remember that for the question database to be of any use to anyone, the answer has to be in the question.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6376768
Looks interesting, but wouldn't you have to have your picture box the right size first?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6376785
>>
   If PictureBox.Image Is Nothing Then
       Exit Sub
   End If
<<

Why do you assume there is a picture in the object first?  Also, why do you use On Error Resume Next?

0
 
LVL 2

Expert Comment

by:JanusFury
ID: 6378349
On error resume next prevents it from dying if something funky happens.
And it works on any size picturebox.

I've written TONS of Set/GetDIBits routines, trust me, I know how to work with it :)

I have a Set/GetDIBits routine that can alpha-blit a 16x16 image with masking to a GDI surface in less than a millisecond :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now