Link to home
Start Free TrialLog in
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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Avatar of richmorlca
richmorlca

ASKER

thanks, but how do i actually write the data to a bitmap file on the local machine?
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

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial