Hi again, i'm trying to put a background image in my button[i declare the button] ,
My database[ms access] , tbldishlist - name[text , Primary key], pic [ole object]

when the form is loading, the button's background is according to the name of the item.
here's my example:

Dim btn As New Button
btn.Name = "" & rd.Items("name").ToString()
btn.Text = btn.Name
btn.backgroundImage = "" & rd.Items("pic") 'error here
Me.Controls.Add(btn)

Recommended Answers

Hi

Unfortunately you cannot just set the background image property to the return value of reader("pic") whcih I assume at this point is a byte array (if you have saved the image to an Ole Object field).

Instead, you first need to convert the value of reader("pic") to a byte …

Jump to Post

Hi

Which part is invalid on line 3, is it the declaration (i.e Image)?

Jump to Post

Sorry. That should have been

If rdr.Read Then
    Dim imgdata() As Byte = rdr("pic")
    Dim str As New MemoryStream(imgdata)
    Button1.BackgroundImage = New Bitmap(Image.FromStream(str))
    str.Close()
End If
Jump to Post

All 9 Replies

Hi

Unfortunately you cannot just set the background image property to the return value of reader("pic") whcih I assume at this point is a byte array (if you have saved the image to an Ole Object field).

Instead, you first need to convert the value of reader("pic") to a byte array and then to an image. The following demonstrates this:

Dim imageBytes() As Byte = reader("pic")
Dim stream As New MemoryStream(imageBytes)
Dim buttonImage As Image = Image.FromStream(stream)

button.BackgroundImage = buttonImage

You will need to add Imports System.IO to access the MemoryStream.

HTH

@djjeavons, it says parameter is not valid @line 3, i already imports system.io

Hi

Which part is invalid on line 3, is it the declaration (i.e Image)?

Dim buttonImage As Image = Image.FromStream(stream)

Is the whole line underlined or a certain part? Can you provide a screen shot as I don't see why you should get any errors on that line.

heres the snapshot

Try something like

Button1.BackgroundImage = New Bitmap("d:\temp\test.jpg")

Hi

How was the image data stored in the database? I'm wondering if it is not a valid image (maybe corrupted somewhere).

Sorry. That should have been

If rdr.Read Then
    Dim imgdata() As Byte = rdr("pic")
    Dim str As New MemoryStream(imgdata)
    Button1.BackgroundImage = New Bitmap(Image.FromStream(str))
    str.Close()
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.