Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleWrite a DLL in Visual Basic .NET that saves images in different formats (GIF, JPG, etc.) and call it from Visual Basic 6
DescriptionThis example shows how to write a DLL in Visual Basic .NET that saves images in different formats (GIF, JPG, etc.) and call it from Visual Basic 6.
KeywordsVB .NET, VB 6, Visual Basic 6, DLL, save image, GIF, JPG, JPEG
CategoriesFiles and Directories, Graphics, VB.NET
 
Visual Basic 6 itself can save image files only in a bitmap format, not in GIF, JPEG, or other formats. Visual Basic .NET can save images in those formats, however. This example builds a VB .NET DLL that a VB 6 program can call to save images in those other formats.

In VB .NET, make a new DLL project. Select the Project\Properties menu item. Open the Configuration Properties folder, click the Build page, and check the "Register for COM Interop" box. This exposes the project's public classes for use by Visual Basic 6.

The following code shows a very simple class that saves an image in a bitmap, GIF, or JPEG file. It defines a public Enum to tell the SaveImage routine the desired file format. SaveImage takes as its first parameter a handle to a bitmap. The code uses that handle to create a Bitmap object and calls its Save method. Compile the DLL.

 
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class ImageSaver
    Public Enum saverImageFormats
        bmp
        gif
        jpg
    End Enum

    Public Sub SaveImage(ByVal hbm As IntPtr, ByVal _
        file_name As String, ByVal image_format As _
        saverImageFormats)
        Dim bm As Bitmap = Bitmap.FromHbitmap(hbm)
        Select Case image_format
            Case saverImageFormats.bmp
                bm.Save(file_name, ImageFormat.Bmp)
            Case saverImageFormats.gif
                bm.Save(file_name, ImageFormat.Gif)
            Case saverImageFormats.jpg
                bm.Save(file_name, ImageFormat.Jpeg)
        End Select
    End Sub
End Class
 
In the Visual Basic 6 application, select Project\References. Click the Browse button and select the .tlb file created for the .NET DLL. Now the Visual Basic 6 program can use the public classes defined by the DLL.

The following code shows how the Visual Basic 6 program uses the DLL.

 
Private Sub cmdSave_Click()
Dim file_name As String
Dim saver As ImageSaver

    Set saver = New ImageSaver
    
    Select Case LCase$(Split(txtFile.Text, ".")(1))
        Case "bmp"
            saver.SaveImage Picture1.Image, txtFile.Text, _
                saverImageFormats_bmp
        Case "gif"
            saver.SaveImage Picture1.Image, txtFile.Text, _
                saverImageFormats_gif
        Case "jpg", "jpeg"
            saver.SaveImage Picture1.Image, txtFile.Text, _
                saverImageFormats_jpg
    End Select

    MsgBox "Done"
End Sub
 
While a Visual Basic 6 program can use this DLL to save images in new formats, it does require you to install the rather large .NET framework.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated