What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a Visual Basic .NET library that returns a picture to a Visual Basic 6 program
DescriptionThis example shows how to make a Visual Basic .NET library that returns a picture to a Visual Basic 6 program.
Keywordslibrary, VB.NET, VB 6, Visual Basic 6, picture, DLL
CategoriesVB.NET, Miscellany, Graphics
Visual Basic 6 uses StdPicture objects to represent pictures while VB .NET uses Image objects. The two are fundamentally incompatible so writing a VB .NET library that can return a picture to a VB 6 proram is difficult.

The following code shows a VB .NET PictureMaker class inside a library named PictureMakerLibrary. Note that it requires references to the System.Drawing.dll and Microsoft.VisualBasic.Compatibility.dll libraries. Note also that you must register the library for COM Interop as described in the initial comments.

Most of the actual code is straightforward drawing onto a Bitmap. The function returns a geneic Object and the function's final line of uses the compatibility library's ImageToIPictureDisp function to convert the Image (Bitmap) into a picture format that Visual Basic 6 can understand.

' Add references to:
'   System.Drawing.dll
'   Microsoft.VisualBasic.Compatibility.dll
' Register for COM Interop:
'   Select Project\Properties.
'   Open the Configuration Properties folder.
'   Click the Build tab.
'   Check "Register for COM Interop"
Imports System.Runtime.InteropServices
Imports System.Drawing

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class PictureMaker
    ' Draw a picture.
    Public Function MakePicture() As Object
        Dim bm As New Bitmap(100, 100)
        Dim gr As Graphics = Graphics.FromImage(bm)

        gr.FillEllipse(Brushes.LightYellow, 1, 1, 98, 98)
        Dim face_pen As New Pen(Color.Black, 2)
        gr.DrawEllipse(face_pen, 1, 1, 98, 98)

        Dim smile_pen As New Pen(Color.Red, 3)
        gr.DrawArc(smile_pen, 20, 20, 60, 60, 0, 180)

        gr.FillEllipse(Brushes.White, 25, 25, 10, 20)
        gr.DrawEllipse(Pens.Black, 25, 25, 10, 20)
        gr.FillEllipse(Brushes.Black, 28, 28, 7, 14)

        gr.FillEllipse(Brushes.White, 65, 25, 10, 20)
        gr.DrawEllipse(Pens.Black, 65, 25, 10, 20)
        gr.FillEllipse(Brushes.Black, 68, 28, 7, 14)

        gr.FillEllipse(Brushes.Orange, 40, 40, 20, 30)
        Dim nose_pen As New Pen(Color.DarkGreen, 2)
        gr.DrawEllipse(nose_pen, 40, 40, 20, 30)


        Return Compatibility.VB6.ImageToIPictureDisp(bm)
    End Function
End Class
The following code shows how the Visual Basic 6 program uses the library. Note that you must give this program a reference to the library.

The program simply creates a new PictureMaker object and calls its MakePicture function.

' Add a reference to PictureMakerLibrary.

Private Sub Command1_Click()
Dim picture_maker As PictureMaker

    Set picture_maker = New PictureMaker
    Picture1.Picture = picture_maker.MakePicture()
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.