Question Store images in the best way

Junderdog

New member
Joined
Jun 24, 2010
Messages
1
Programming Experience
Beginner
  1. Is it possible to store images in the variables or something like that, then store the images of things that are not buttons, picture boxes or panels?
  2. What is the fastest way to store images in Visual Basic?
  3. Is it possible to create a list of the class Bitmap and post a picture in each bitmap in a forloop? If it works I would like to see some examplecode, please.
 
Junderdog,

The way I believe is the best way to store bitmaps is to collect them in to arrays. You don't loose image quality, and they load fairly quick. Please follow the steps provided below, and I hope this is what your are looking for :)

STEP 1: BitmapArray Class
Create a new Class. Paste the following code in to this new class.

VB.NET:
Public Class BitmapArray
    Inherits System.Collections.CollectionBase
    Private HostObject As Object

    Public Sub New(ByVal host As Object)
        HostObject = host
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As Bitmap
        Get
            Return CType(Me.List.Item(Index), Bitmap)
        End Get
    End Property

    Public Function AddBitmap(ByVal Index As Integer, ByVal thisImage As Drawing.Image) As Bitmap
        Dim aBitmap As Bitmap = thisImage.Clone
        Me.List.Add(aBitmap)
        Return aBitmap
    End Function

    Public Sub RemoveBitmap(ByVal BitmapIndex As Integer)
        If Me.Count > 0 And BitmapIndex < Me.Count Then
            HostObject.Controls.Remove(Me(BitmapIndex))
            Me.List.RemoveAt(BitmapIndex)
        Else
            MsgBox("Failed to remove bitmap. No such BitmapIndex exists!", MsgBoxStyle.Exclamation, "No Such Index")
        End If
    End Sub
End Class


STEP 2: Applying the Class
In each of the Forms that will be storing and extracting the Bitmaps, enter the following line of code just under Public Class FormName:

VB.NET:
Private BitmapCollections As New BitmapArray()

STEP 3: Using the Code
Using bitmap collections is really simple;

To add a bitmap to the collection:
VB.NET:
[I]BitmapCollections.AddBitmap([COLOR="blue"]Index[/COLOR], [COLOR="red"]Image[/COLOR])[/I]

Where;
- Index is the position in which you want to store the newly added bitmap. If there is already a bitmap at the Index of 0, and the bitmap your adding is given the Index of 0, then the already existing bitmap should move to Index 1.
- Image is your source image that you are adding to the collection. Could be any supported image format. If you are wanting to add a bitmap from a file to the collection, just use Image.FromFile(FileName) in place of Image. You could even use an existing image from a PictureBox (PictureBox1.Image), create a new Bitmap (New Bitmap(FileName)), and practically any other way of replacing Image with a valid image.

To grab a bitmap from the collection:
VB.NET:
[I]BitmapCollections.Item([COLOR="red"]Index[/COLOR])[/I]

Where; Index would be one of the indexes you specified when you added the bitmap(s).

There are many ways to grab bitmaps, some of these are;
VB.NET:
[B]PictureBox1.Image =[/B] BitmapCollections.Item(0)
[B]Dim imageHolder As Image =[/B] BitmapCollections.Item(0)


To remove a bitmap from the collection:
VB.NET:
[I]BitmapCollections.RemoveBitmap([COLOR="red"]Index[/COLOR])[/I]

Where; Index would be one of the indexes you specified when you added the bitmap(s).


There are many ways to mess around with the collected bitmaps. The reason this is the best way is that the bitmaps remain in the collection, no matter how many times you use them, the bitmaps can be added and removed easily, and many different controls and forms can interact with the BitmapArray class.

Let me know how you go with this, and let me know if theres anything else I can do for you. :)
 
Junderdog said:
Is it possible to create a list of the class Bitmap
VB.NET:
Dim images As New List(Of Bitmap)
 
Back
Top