Search code examples
vb.netvb6dib

How to bring "System.Drawing.Image" to DIB


I have created a consumable COM Class Library.

The class library gets the image of a camera. It's of type Image / Bitmap.

I consume the class library in VB6, and I need to show this image in VB6.

How could I convert this System.Drawing.Image to something that VB6 can display?

Can I use a DIB (using CreateDIBSection, etc.)?

If yes, how exactely can a System.Drawing.Image be converted to a DIB?

Thank you!


Solution

  • Here's what I've done in the past. First, a couple prerequisites:

    • you get a Byte() from the COM Class Library
    • you set a reference to Microsoft Windows Image Aquisition Library

    With these in place, the code is pretty simple. Camera is a COM Class Library where the Retrieve method returns a Byte() that gets loaded into an Image control:

    Option Explicit
    
    Private Sub cmdLoad_Click()
       Dim cam As Camera.Image
       Set cam = New Camera.Image
       Image1.Picture = LoadPictureFromByteArray(cam.Retrieve())
    End Sub
    
    Private Function LoadPictureFromByteArray(Image() As Byte) As StdPicture
       Dim vec As WIA.Vector
       Set vec = New WIA.Vector
       vec.BinaryData = Image
       Set LoadPictureFromByteArray = vec.ImageFile.FileData.Picture
    End Function