Avatar of richmorlca
 asked on

saving an SQL image field as a bitmap in vb.net

I have an image field in an sql database, and i need to get this out and write it into a bitmap file. Can anyone help? I am using vb.net.

.NET Programming

Avatar of undefined
Last Comment
Bob Learned

8/22/2022 - Mon
Bob Learned

As much as I don't like to just post a link to an article, I think this one is self-explanatory:

Working with SQL Server BLOB Data in .NET

Private Shared Sub StoreFile(filename As String)
	Dim connection As New SqlConnection("Server=(local) ; Initial Catalog = FileStore ; Integrated Security = SSPI")

	Dim command As New SqlCommand("INSERT INTO MyFiles VALUES (@Filename, @Data)", connection)

	command.Parameters.AddWithValue("@Filename", Path.GetFileName(filename))
	command.Parameters.AddWithValue("@Data", File.ReadAllBytes(filename))



End Sub

Open in new window

Private Shared Function RetrieveFile(filename As String) As Byte()
	Dim connection As New SqlConnection("Server=(local) ; Initial Catalog = FileStore ; Integrated Security = SSPI")

	Dim command As New SqlCommand("SELECT * FROM MyFiles WHERE Filename=@Filename", connection)

	command.Parameters.AddWithValue("@Filename", filename)


	Dim reader As SqlDataReader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)


	Dim memory As New MemoryStream()

	Dim startIndex As Long = 0
	Const  ChunkSize As Integer = 256
	While True
		Dim buffer As Byte() = New Byte(ChunkSize - 1) {}

		Dim retrievedBytes As Long = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize)

		memory.Write(buffer, 0, CInt(retrievedBytes))

		startIndex += retrievedBytes

		If retrievedBytes <> ChunkSize Then
			Exit While
		End If
	End While


	Dim data As Byte() = memory.ToArray()


	Return data
End Function

Open in new window


thanks, but how do i actually write the data to a bitmap file on the local machine?
Bob Learned

Instead of returning a byte array, use the MemoryStream from the RetrieveFile example, and generate an Image from it.



Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
    Dim ImageStream As MemoryStream
      If ByteArr.GetUpperBound(0) > 0 Then
        ImageStream = New MemoryStream(ByteArr)
        NewImage = Image.FromStream(ImageStream)
        NewImage = Nothing
      End If
    Catch ex As Exception
      NewImage = Nothing
    End Try
  End Sub

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

i keep getting the error 'parameter is not valid', ive tried a few different methods and its always the same. any ideas?
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question