1

I am inserting and retrieving an image from my database. I can now insert but I am having a hard time retrieving the file. I used varbinary(max) as datatype of the image.

This is my code for inserting:

Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = ms.ToArray()

cmd.CommandText = "insert into stud values ('" & studno.Text & "', '" & password.Text & "', '" & fname.Text & "', '" & mname.Text & "', '" & lname.Text & "', @img, '" & gender.Text & "', '" & mm.Text & "/" & dd.Text & "/" & yyyy.Text & "', '" & phone.Text & "', '" & address.Text & "', 'Student', '" & secquest.Text & "', '" & answersq.Text & "', '" & TextBox1.Text & "', '" & ComboBox1.Text & "')"

cmd.Parameters.Add("@img", SqlDbType.VarBinary).Value = img

and this is how i retrieve:

con.Open()
cmd.CommandText = "select * from stud where studentno = 'mnb'"
cmd.Connection = con
dr = cmd.ExecuteReader()

While dr.Read()

    studnum.Text = dr.Item("studentno")
    fname.Text = dr.Item("fname")
    mname.Text = dr.Item("mname")
    lname.Text = dr.Item("lname")
    gender.Text = dr.Item("gender")
    section.Text = dr.Item("seccode")
    bday.Text = dr.Item("bday")
    phone.Text = dr.Item("phoneno")
    address.Text = dr.Item("maddress")

    Dim imageData As Byte() = DirectCast(dr("pic"), Byte())
    If Not imageData Is Nothing Then
        Using ms As New MemoryStream(imageData, 0, imageData.Length)
            ms.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(ms, True)
        End Using
    End If

End While

My problem is, it says OUT OF MEMORY whenever i run my program. How to solve it? The size of the image i am retrieving is 2MB.

I am using memorystream and what I researched most used filestream. I believe it's different in some ways.

ABCDE
  • 131
  • 3
  • 6
  • 15
  • Possible duplicate of [out of memory Image.FromFile](http://stackoverflow.com/questions/3848132/out-of-memory-image-fromfile) – GSerg Nov 05 '16 at 16:07
  • For an image that large, consider archiving the images somewhere and just saving just the filename. Your reader is setup to read in a loop which means you could be creating multiple images - none of the previous ones are disposed – Ňɏssa Pøngjǣrdenlarp Nov 05 '16 at 16:11
  • Just add few RAM sticks to your box – T.S. Nov 05 '16 at 16:13
  • 1
    @GSerg i believe it doesn't. That thread unfortunately didn't help me solve my problem – ABCDE Nov 05 '16 at 16:20
  • @Plutonix is it reliable to save just the filename? could i not encounter problems by that method? – ABCDE Nov 05 '16 at 16:27
  • The problem with saving images is that it bloats the DB and takes more time to extract the data and rebuild an image from it than to just read it. IF you are going to save the image, you should build an image from the byte data. that `ms.Write` method isnt needed because the memstream already has all the data. – Ňɏssa Pøngjǣrdenlarp Nov 05 '16 at 16:34
  • (I dunno what kind of problems you anticipate) – Ňɏssa Pøngjǣrdenlarp Nov 05 '16 at 16:35
  • sir, are you referring to saving the path of the image instead of the image itself? – ABCDE Nov 05 '16 at 16:36
  • if that so, what if i relocate the image, i will encounter problems in retrieving, right? – ABCDE Nov 05 '16 at 16:37
  • 1
    Actually, when I do that I store the file to an AppData folder, prepend something to the name (like the PK), then save just the filename. The path location can be added back in order to load the file, that way you can move the images folder without breaking anything. – Ňɏssa Pøngjǣrdenlarp Nov 05 '16 at 16:45
  • GDI+ can give unhelpful error messages. To make sure the image data is what you expect, write the image data to a file and try opening it with an image viewer. (Although keeping the image as a separate file as Plutonix said would usually be better.) – Andrew Morton Nov 05 '16 at 19:00
  • @ABCDE That question lists reasons that can cause outofmemoryexception and reminds that you must not close the stream. To make sure it's not a duplicate, please rule out said cases (is your stored data intact? is your image in right format? Are you able to view it if you dump it to a file? What is the format and is it supported by GDI+?) and do not close the stream which needs to stay open while the image created from it exists. – GSerg Nov 05 '16 at 19:48
  • What version of sql server you are connected? – M.Hassan Nov 06 '16 at 04:34
  • @M.Hassan sql server 2012 – ABCDE Nov 06 '16 at 04:46

2 Answers2

1

The error is due to memoryStream should be open all the time.

To resolve this problem, use the following function to get image from byte array

    Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
        Dim img As Image = Nothing           
            Dim ms As New MemoryStream(byteArrayIn, 0, byteArrayIn.Length)
            ms.Write(byteArrayIn, 0, byteArrayIn.Length)
            img = Image.FromStream(ms, True)             
        Return img
    End Function

Call the function to fill PictureBox1:

  PictureBox1.Image = byteArrayToImage(imageData)
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
0

Try this to convert your byte array to image:

Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
   Using mStream As New MemoryStream(byteArrayIn)
       Return Image.FromStream(mStream)
   End Using
End Function
Maksim Sestic
  • 300
  • 2
  • 8