Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

How to show picture in picture box from array?

If I have array which contain RGB of each pixel , how can I show it to Picture box?
0
rapeepak
Asked:
rapeepak
1 Solution
 
PaulHewsCommented:
One thing you can do is loop through the array and call picture1.PSet (x, y), lngColor(x,y)
0
 
PaulHewsCommented:
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
 
bobbit31Commented:
Paul: no thanks ;)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
OtanaCommented:
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
 
JanusFuryCommented:
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
 
JanusFuryCommented:
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
 
JanusFuryCommented:
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
 
PaulHewsCommented:
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
 
PaulHewsCommented:
Looks interesting, but wouldn't you have to have your picture box the right size first?
0
 
PaulHewsCommented:
>>
   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
 
JanusFuryCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now