Making a list of bitmaps

Devon Nullman 21 Reputation points
2021-02-21T18:54:11.27+00:00

I have a list of restaurant names that I want to convert into a list of bitmaps showing the name - Here's what I have, what works and what doesn't

If I don't save the bitmap and add it as shown, the list is populated but every image is invalid. Debug details shows every property of every image as follows: Width = '(New System.Collections.Generic.Mscorlib_CollectionDebugView(Of System.Drawing.Bitmap)(Images).Items(0)).Width' threw an exception of type 'System.ArgumentException'

![70386-bad-imagelist.jpg][1] I vastly prefer NOT to have to save as a file and then "import" - any ideas why adding a bitmap to a list fo bitmaps behaves like this ? [1]: /api/attachments/70386-bad-imagelist.jpg?platform=QnA

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Devon Nullman 21 Reputation points
    2021-02-21T18:57:33.6+00:00
    Imports System.IO
    Public Class Form1
        Private R As New Random
        Private Places As New List(Of String)
        Private Images As New List(Of Bitmap)
        Private TheFont As Font = New Font("Engravers MT", 18)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PictureBox1.Visible = False
            Dim FN As Integer = 1
            Using g As Graphics = Me.CreateGraphics
    
                For Each S As String In Places ' List of Restaurant Names
                    Dim SF As SizeF = g.MeasureString(S, TheFont)
                    TextBox1.AppendText(S & " = " & SF.Width & ", " & SF.Height & vbNewLine)
                    PictureBox1.Size = New Size(SF.Width, SF.Height)
    
                    Using BM As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
                        Using gg As Graphics = Graphics.FromImage(BM)
                            gg.Clear(Color.White)
                            gg.DrawString(S, TheFont, Brushes.Black, 0, 0)
                        End Using
                        ''BM.Save("D:\AAAAAA\" & S & ".jpg", Imaging.ImageFormat.Jpeg)
                        ''Images.Add(Bitmap.FromFile("D:\AAAAAA\" & S & ".jpg"))
                        ''The code above DOES WORK
                        'The code below DOES NOT WORK
                        'Images.Add(BM)
                    End Using
                    FN += 1
                Next
            End Using
        End Sub
    
    0 comments No comments

  2. Xingyu Zhao-MSFT 5,361 Reputation points
    2021-02-22T06:35:09.16+00:00

    Hi @Devon Nullman ,

    Change

    Using BM As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)

    to

    Dim BM As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)  
    

    Here's the code you can refer to.

        Private R As New Random  
        Private Places As New List(Of String)  
        Private Images As New List(Of Bitmap)  
        Private TheFont As Font = New Font("Engravers MT", 18)  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim FN As Integer = 1  
            Using g As Graphics = Me.CreateGraphics  
                For Each S As String In Places  
                    Dim SF As SizeF = g.MeasureString(S, TheFont)  
                    TextBox1.AppendText(S & " = " & SF.Width & ", " & SF.Height & vbNewLine)  
                    PictureBox1.Size = New Size(SF.Width, SF.Height)  
      
                    Dim BM As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)  
                    Using gg As Graphics = Graphics.FromImage(BM)  
                        gg.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel  
                        gg.DrawString(S, TheFont, Brushes.Black, New Point(2, 2))  
                        Images.Add(BM)  
                    End Using  
                    FN += 1  
                Next  
            End Using  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments