Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

Need help converting bitmap to image in VB.Net

How can I convert a bitmap to an image in VB.Net?

I get the following error:
Value of type 'Bitmap' cannot be converted to 'Image'

Here is the code that is giving me that error (2nd line:
                Dim b As Bitmap = BitMapImage2BitMap(ImageArr.Item(i))
                Dim nv As Image = CType(b, Image)

Open in new window


Please tell me how to accomplish this.  Thank you in advance for your help.

Thank you,
mrotor
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

A Bitmap is an Image. It inherits from the Image class. Why do you specifically need an Image?
Avatar of mainrotor
mainrotor

ASKER

I need to pass the Bitmap to a function that takes an array of Images.
How can I convert a Bitmap to an Image?  Bitmap may inherit from the Image class, but my current code is giving me an error.

Here is the function that I need to eventually pass my Bitmap to:

Public Shared Function ConvertImageArrayToImage(ByVal images As Image()) As Image

Thanks for your help
mrotor
As Wayne stated, a Bitmap *is* an Image.  It derives from it; e.g. -
Imports System.Drawing

Module Module1
    Sub Main()
        Dim [array] As Image() = {}
        For i = 0 To 4
            ReDim Preserve [array](i)
            [array](i) = New Bitmap(5, 5)
        Next
        Console.WriteLine("Array of bitmaps -")
        PrintContents([array])

        Dim [list] As New List(Of Image)
        For i = 0 To 4
            list.Add(New Bitmap(5, 5))
        Next
        Console.WriteLine("List of bitmaps -")
        PrintContents([list].ToArray())

        Console.ReadLine()
    End Sub

    Sub PrintContents(source As Image())
        For Each [image] In source
            Console.WriteLine("Type: {0}", [image].GetType())
        Next
    End Sub

Open in new window

Which produces the following output -User generated imageAs you can see I can construct both an Image Array and an Image List.  Each construct allows for me to add bitmaps without needing to do a conversion/cast.  I can also pass these constructs to a method that accepts an Image Array and enumerate the contents of the array.

So the real issue is what kind of error are you getting when you try to pass your array of Bitmap's?  What in your called method is causing you grief.

-saige-
Are you speaking about Bitmap (System.Drawing) or BitmapImage (WPF)?
Hi Ark,
It's both. I use both Bitmap (System.Drawing) and BitmapImage(WPF).

They both give me the same error:
Value of type 'Bitmap' cannot be converted to 'Image'

NOTE: That is one thing I forgot to mention.  That this is a .xaml form in my VB.Net winform application.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ark.  Thank you for your suggestion.  I will go ahead and test your suggestion.

mrotor
Thank you for the suggestion.  Fully qualifying the namespaces worked great.