Insert Multi Page Tiff into Firebird database in vb.net

I'm trying to break apart  a multipage tiff and have each page inserted into a firebird db as a separate blob for each page.

I've found examples of how to loop through each page of the tiff but am unsure of how to convert the single page image already load into memory into a filestream which  is needed for the blob insert.

I cant use this
Dim fs As FileStream = New FileStream(curfilename, FileMode.OpenOrCreate, FileAccess.Read)
as the the file stream will be the entire tiff - not a single page of the file.

Also how do you discard the original tiff and individual images from memory once all the inserts have been completed

Thanks in advance


Sub Split_MultipageTiff(ByVal filename)
        Dim image As Image = image.FromFile(filename)
        Dim single_page_image As Image
        Dim guid = image.FrameDimensionsList(0)
        Dim dimension = New FrameDimension(guid)
        Dim totalFrame = image.GetFrameCount(dimension)
        Dim file_suffix As String = Path.GetExtension(filename)
        Dim file_Prefix As String = Path.GetFileNameWithoutExtension(filename)
        Dim Newfile_Name As String
 
        Dim sqlConnection1 As New FirebirdSql.Data.FirebirdClient.FbConnection(connectionstring)
        Dim cmd As New FirebirdSql.Data.FirebirdClient.FbCommand
        Dim fs As FileStream = new FileStream(??????
        Dim rawdata() As Byte = New Byte(fs.Length) {}
 
        For i As Integer = 0 To totalFrame - 1
            Newfile_Name = file_Prefix + "_" + Convert.ToString(i) + file_suffix
            image.SelectActiveFrame(dimension, i)
            single_page_image = image.Clone
            '??????Convert single_page_image into a rawdata()
 
            Try
                cmd.CommandText = "INSERT INTO ELMO (FILETIMESTAMP, FILENAME, IMAGE_SCAN) VALUES (@FILETIMESTAMP, @FILENAME, @BLOBDATA);"
                cmd.Parameters.Add("@BLOBDATA", rawdata)
 
etc

Open in new window

JonR01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
Hi Jon,
Sorry I can't help with how to page the .tiff, but have a look at the following code as an example on how you can "clean-up" (dispose), the used unmanaged resources.

Sub Split_MultipageTiff(ByVal filename)
        Using image As Drawing.Image = Drawing.Image.FromFile(filename)
            Dim guid = image.FrameDimensionsList(0)
            Dim dimension As New Drawing.Imaging.FrameDimension(guid)
            Dim totalFrame As Integer = image.GetFrameCount(dimension)
            Dim file_suffix As String = Path.GetExtension(filename)
            Dim file_Prefix As String = Path.GetFileNameWithoutExtension(filename)
            Dim Newfile_Name As String
            Using sqlConnection1 As New FirebirdSql.Data.FirebirdClient.FbConnection(connectionstring)
                Using cmd As New FirebirdSql.Data.FirebirdClient.FbCommand
                    Using fs As FileStream = new FileStream(??????
                        Dim rawdata() As Byte = New Byte(fs.Length) {}
                        For i As Integer = 0 To totalFrame - 1
                            Newfile_Name = file_Prefix + "_" + Convert.ToString(i) + file_suffix
                            image.SelectActiveFrame(dimension, i)
                            Using single_page_image As Drawing.Image = image.Clone
                                '??????Convert single_page_image into a rawdata()
                            End Using
                        Next
                    End Using
                End Using
            End Using
        End Using
    End Sub

Open in new window

0
Bob LearnedCommented:
I think that this problem is not about creating a FileStream, but creating a MemoryStream.  If you have an Image or Bitmap instance, then you can save it to a MemoryStream.

Image class:


Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
 
Public Class ImageConverter
 
    Public Shared Function ByteArrayToImage(ByVal buffer() As Byte) As Image
        Using ms As New MemoryStream(buffer)
            Dim img As Image = Image.FromStream(ms)
            Return img
        End Using
    End Function
 
    Public Shared Function ImageToByteArray(ByVal img As Image, ByVal format As ImageFormat) As Byte()
        Using ms As New MemoryStream()
            img.Save(ms, format)
            Return ms.ToArray()
        End Using
    End Function
 
    Public Shared Function ImageToBase64String(ByVal img As Image, ByVal format As ImageFormat) As String
        Return Convert.ToBase64String(ImageToByteArray(img, format))
    End Function
 
    Public Shared Function Base64StringToImage(ByVal imageString As String) As Image
        Return ByteArrayToImage(Convert.FromBase64String(imageString))
    End Function
 
    ''' <summary>
    ''' Convert an image to the desired pixel format.
    ''' </summary>
    ''' <returns>Converted image</returns>
    ''' <remarks>Create a Bitmap with the desired pixel format, and draw the image using
    ''' a temporary Graphics object.
    ''' </remarks>
    Public Shared Function ConvertPixelFormat(ByVal img As Bitmap, ByVal format As PixelFormat) As Image
        Dim bmp As New Bitmap(img.Width, img.Height, format)
        Using gr As Graphics = Graphics.FromImage(bmp)
            gr.DrawImage(img, 0, 0)
        End Using
        Return bmp
    End Function
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.