Saturday, June 6, 2009

Retrieve Images that are saved in your Oracle tables as Blog

Ok. If you have images that are saved in your blob field. You can download then into your code as shown below.

Please don't forget visit my website at: www.lendmyspace.com and www.lookfordeal.com

Public Function saveBlobImages() As Single
Dim sqlStr1 As String
Dim cmd As New OracleClient.OracleCommand
Dim oraDa As OracleDataAdapter
Dim conn As OracleConnection = Nothing
Dim ms As MemoryStream = Nothing
Dim dsImage As Data.DataSet = Nothing
Dim myBytes() As Byte = Nothing
Dim imgJPG As System.Drawing.Image = Nothing
Dim msOut As MemoryStream = Nothing

Try

sqlStr1 = "select Image_Name, image_ID, blob_image from image_table where rownum < 100"

' Initialize connection
oraDa = New OracleDataAdapter(sqlStr1, My.Settings.sConnectionString)
conn = oraDa.SelectCommand.Connection
dsImage = New Data.DataSet()
oraDa.Fill(dsImage, "ImageTifImages")

If dsImage.Tables(0).Rows.Count = 0 Then
Throw New Exception("No results returned for rows")
Else

For i As Integer = 0 To dsImage.Tables(0).Rows.Count - 1

myBytes = dsImage.Tables(0).Rows(i)("blob_image")
ms = New MemoryStream
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = Image.FromStream(ms)

'Export to TIF Stream
msOut = New MemoryStream
imgJPG.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg)
imgJPG.Dispose()
imgJPG = Nothing
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = System.Drawing.Image.FromStream(ms)
Dim filename As String = dsImage.Tables(0).Rows(i)("Image_Name").ToString + "_" + dsImage.Tables(0).Rows(i)("Image_id").ToString
filename = filename.Replace(".txt", "")
imgJPG.Save(filename + ".Tif")
ms.Close()
Next
End If

Catch ex As Exception
Throw
End Try

End Function

No comments:

Post a Comment