We help IT Professionals succeed at work.

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

richmorlca
richmorlca asked
on
595 Views
Last Modified: 2012-06-15
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

Commented:
thanks, but how do i actually write the data to a bitmap file on the local machine?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

Commented:
i keep getting the error 'parameter is not valid', ive tried a few different methods and its always the same. any ideas?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.