Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1062
  • Last Modified:

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

0
JonR01
Asked:
JonR01
2 Solutions
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now