Avatar of richmorlca
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.

thanks
.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
http://www.codeproject.com/Articles/66271/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))

	connection.Open()

	command.ExecuteNonQuery()

	connection.Close()
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)

	connection.Open()

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

	reader.Read()

	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

	connection.Close()

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

	memory.Dispose()

	Return data
End Function

Open in new window

richmorlca

ASKER
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.

Example:

http://www.daniweb.com/software-development/vbnet/threads/176169/how-to-do-byte-array-into-image

Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
    '
    Dim ImageStream As MemoryStream
    Try
      If ByteArr.GetUpperBound(0) > 0 Then
        ImageStream = New MemoryStream(ByteArr)
        NewImage = Image.FromStream(ImageStream)
      Else
        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
richmorlca

ASKER
i keep getting the error 'parameter is not valid', ive tried a few different methods and its always the same. any ideas?
ASKER CERTIFIED SOLUTION
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
or
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