I resolved this issue by simply not using buttons. Instead I used pictureboxes as buttons and that resolved the issue. Im guesssing the problem is that buttons don't allow as much memory as pictureboxes. Can anyone confirm this?
"System.OutOfMemoryException: 'Out of memory.'" when reading image from SQL Server
jumexmango
136
Reputation points
I have images assigned to every button in my VB.NET form, the images come from SQL Server. The data type is varbinary(MAX). The platform is 64bit. I'm thinking it might have to do with not disposing properly, but I'm not sure since I'm new to coding. The error only appears when more than one image is read. The weird part is that I have a different form in the same project with similar code that does work.
This is my code:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using con As New SqlConnection("con string")
Dim sql As String = "SELECT * FROM Inventario WHERE ID=@ID"
Using cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 3
con.Open()
Using myreader As SqlDataReader = cmd.ExecuteReader()
If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then
Boton3.Text = myreader("Articulo")
Boton3.Enabled = myreader("ONOFF")
Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())
Using ms As New MemoryStream(ImgSql)
Boton3.BackgroundImage = Image.FromStream(ms)
con.Close()
End Using
Else
Boton3.Text = myreader("Articulo")
Boton3.BackgroundImage = Nothing
Boton3.Enabled = myreader("ONOFF")
End If
End Using
End Using
End Using
Using con As New SqlConnection("con string;")
Dim sql As String = "SELECT * FROM Inventario WHERE ID=@ID"
Using cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 4
con.Open()
Using myreader As SqlDataReader = cmd.ExecuteReader()
If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then
Boton4.Text = myreader("Articulo")
Boton4.Enabled = myreader("ONOFF")
Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())
Using ms As New MemoryStream(ImgSql)
Boton4.BackgroundImage = Image.FromStream(ms)
con.Close()
End Using
Else
Boton4.Text = myreader("Articulo")
Boton4.BackgroundImage = Nothing
Boton4.Enabled = myreader("ONOFF")
End If
End Using
End Using
End Using