Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to display the image in VB 6.0

How to display the image in VB 6.0

Scheduled Pinned Locked Moved Visual Basic
questiontutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AR Reddy
    wrote on last edited by
    #1

    Hi, I want to display the image in Picture Box in VB 6.0. I have the output in ByteArray. How can I display the image in picturebox? Thanks in advance,

    AR Reddy

    D B 2 Replies Last reply
    0
    • A AR Reddy

      Hi, I want to display the image in Picture Box in VB 6.0. I have the output in ByteArray. How can I display the image in picturebox? Thanks in advance,

      AR Reddy

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      A quick Google for "VB6 convert byte array to bitmap" came up with this[^].

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      • A AR Reddy

        Hi, I want to display the image in Picture Box in VB 6.0. I have the output in ByteArray. How can I display the image in picturebox? Thanks in advance,

        AR Reddy

        B Offline
        B Offline
        Billypon
        wrote on last edited by
        #3

        Example like this: Create a form within a PictureBox control. Type the code: Dim arrayPic() As Byte Open "testpic.jpg" For Binary As #1 ReDim arrayPic(LOF(1)) Get #1, , arrayPic Close #1 Picture1.Picture = PictureFromBits(a) Create a module and type the code: Public Enum CBoolean CFalse = 0 CTrue = 1 End Enum Public Const S_OK = 0 Declare Function CreateStreamOnHGlobal Lib "ole32" _ (ByVal hGlobal As Long, _ ByVal fDeleteOnRelease As CBoolean, _ ppstm As Any) As Long Declare Function OleLoadPicture Lib "olepro32" _ (pStream As Any, _ ByVal lSize As Long, _ ByVal fRunmode As CBoolean, _ riid As GUID, _ ppvObj As Any) As Long Public Type GUID dwData1 As Long wData2 As Integer wData3 As Integer abData4(7) As Byte End Type Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As GUID) As Long Public Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}" Public Const GMEM_MOVEABLE = &H2 Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long) Public Function PictureFromBits(abPic() As Byte) As IPicture Dim nLow As Long Dim cbMem As Long Dim hMem As Long Dim lpMem As Long Dim IID_IPicture As GUID Dim istm As stdole.IUnknown Dim ipic As IPicture On Error GoTo Out nLow = LBound(abPic) On Error GoTo 0 cbMem = (UBound(abPic) - nLow) + 1 hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem) If hMem Then lpMem = GlobalLock(hMem) If lpMem Then MoveMemory ByVal lpMem, abPic(nLow), cbMem Call GlobalUnlock(hMem) If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, CFalse, IID_IPicture, PictureFromBits) End If End If End If End If Out: En

        A 1 Reply Last reply
        0
        • B Billypon

          Example like this: Create a form within a PictureBox control. Type the code: Dim arrayPic() As Byte Open "testpic.jpg" For Binary As #1 ReDim arrayPic(LOF(1)) Get #1, , arrayPic Close #1 Picture1.Picture = PictureFromBits(a) Create a module and type the code: Public Enum CBoolean CFalse = 0 CTrue = 1 End Enum Public Const S_OK = 0 Declare Function CreateStreamOnHGlobal Lib "ole32" _ (ByVal hGlobal As Long, _ ByVal fDeleteOnRelease As CBoolean, _ ppstm As Any) As Long Declare Function OleLoadPicture Lib "olepro32" _ (pStream As Any, _ ByVal lSize As Long, _ ByVal fRunmode As CBoolean, _ riid As GUID, _ ppvObj As Any) As Long Public Type GUID dwData1 As Long wData2 As Integer wData3 As Integer abData4(7) As Byte End Type Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As GUID) As Long Public Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}" Public Const GMEM_MOVEABLE = &H2 Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long) Public Function PictureFromBits(abPic() As Byte) As IPicture Dim nLow As Long Dim cbMem As Long Dim hMem As Long Dim lpMem As Long Dim IID_IPicture As GUID Dim istm As stdole.IUnknown Dim ipic As IPicture On Error GoTo Out nLow = LBound(abPic) On Error GoTo 0 cbMem = (UBound(abPic) - nLow) + 1 hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem) If hMem Then lpMem = GlobalLock(hMem) If lpMem Then MoveMemory ByVal lpMem, abPic(nLow), cbMem Call GlobalUnlock(hMem) If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, CFalse, IID_IPicture, PictureFromBits) End If End If End If End If Out: En

          A Offline
          A Offline
          AR Reddy
          wrote on last edited by
          #4

          Hi Billypon, Thanks a Lot. Your code is working fine and I am able to display the image. Now I have one more problem is: I have already image loaded into the memory and I have that memory address pointer. How can I convert that into bytes or bytearray, so that I can use your previous code to display into a picture box. Thanks in advance.

          AR Reddy

          B 1 Reply Last reply
          0
          • A AR Reddy

            Hi Billypon, Thanks a Lot. Your code is working fine and I am able to display the image. Now I have one more problem is: I have already image loaded into the memory and I have that memory address pointer. How can I convert that into bytes or bytearray, so that I can use your previous code to display into a picture box. Thanks in advance.

            AR Reddy

            B Offline
            B Offline
            Billypon
            wrote on last edited by
            #5

            Add this API declare to the Module which I told you. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) And the write down this function into the Module. Public Function PictureFromPointer(Pointer As Long, Length As Long) As IPicture     Dim hMem As Long     Dim lpMem As Long     Dim IID_IPicture As GUID     Dim istm As stdole.IUnknown     Dim ipic As IPicture     On Error GoTo Out     If Pointer > 0 And Length > 0 Then         hMem = GlobalAlloc(GMEM_MOVEABLE, Length)         If hMem Then             lpMem = GlobalLock(hMem)             If lpMem Then                 CopyMemory ByVal lpMem, Pointer, Length                 Call GlobalUnlock(hMem)                 If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then                     If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then                         Call OleLoadPicture(ByVal ObjPtr(istm), Length, CFalse, IID_IPicture, PictureFromPointer)                     End If                 End If             End If         End If     End If Out: End Function Now you can use the function like this: PictureFromPointer(YourPointer,LengthOfPointer) I was test nothing about this function,so I can't promise it will work fine.. Luckly!!! -- modified at 22:31 Friday 1st June, 2007

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups