Welcome Guest! You need to login or register to make posts.



Go to last post Go to first unread
#1 Posted : Tuesday, April 04, 2006 6:33:03 PM(UTC)

Rank: Member

Groups: Member
Joined: 4/3/2006(UTC)
Posts: 4

Hi there

We bulk-scan documents into single page 1 bpp TIFF files that then need to be grouped and merged into multi-page TIFF files before being saved to a SQL dbase (preferably with CCITT4 compression). My code appears to be merging and saving the files but when I attempt to retrieve them I receive the following ;

"An unhandled exception of type 'Aurigma.GraphicsMill.Codecs.MediaUnsupportedException' occurred in aurigma.graphicsmill.dll
Additional information: The media format is not supported"

I believe the error must lie with the way in which I am creating or saving the file(s). I would prefer to be able to work with the multi-page file in memory rather than creating a physical file before saving to the dbase. I would appreciate any comments or advice on my code below:

Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click

        Dim m_uebGroup As UltraExplorerBarGroup
   	Dim m_pages As Integer
   	Dim m_fi As FileInfo
   	Dim m_di As DirectoryInfo
   	Dim m_srcDoc, m_srcSubDoc As String
   	Dim m_arrFiles As New ArrayList
   	Dim m_tifWriter As TiffWriter
   	Dim m_tifReader As TiffReader
   	Dim m_stream As MemoryStream
   	Dim m_bm As New GraphicsMill.Bitmap
   	Dim m_frame As TiffFrame
   	For Each m_uebGroup In uebImgListBar.Groups
   	    If m_uebGroup.Items.Count() > 0 Then
   	        m_srcDoc = Path.Combine(m_srcDirectoryPath, m_uebGroup.Items(0).Tag.ToString())
   	        If File.Exists(m_srcDoc) Then
   	           m_stream = New MemoryStream
   	            'ensure that all frames in first document are loaded into stream
   	            m_tifWriter = New TiffWriter(m_stream)
   	            AddFrames(m_tifWriter, m_srcDoc)
   	            'Now check that all frames in documents to be merged with 
   	            'first document are loaded into stream
   	            m_pages = m_uebGroup.Items.Count
   	            For i As Integer = 1 To m_pages - 1
   	                m_srcSubDoc = Path.Combine(m_srcDirectoryPath, m_uebGroup.Items(i).Tag.ToString())
   	                If File.Exists(m_srcSubDoc) Then
   	                    AddFrames(m_tifWriter, m_srcSubDoc)
   	                End If
   							'test write to file
                                                        'ERROR occurs on following line
   	            Dim bm_test As New GraphicsMill.Bitmap(m_stream)
   	            bm_test.Save(Path.Combine(m_targetDirectoryPath, Path.ChangeExtension(m_srcDoc, "tif")))
   	            'TODO: use m_stream.GetBuffer() to save to dbase as BLOB
End Sub

Private Sub AddFrames(ByRef tw As TiffWriter, ByVal src As String)
    Dim m_tifReader As New TiffReader(src)
    Dim m_bm As New GraphicsMill.Bitmap

    For j As Integer = 0 To m_tifReader.FrameCount - 1
        Dim m_frame As New TiffFrame
        m_frame.Compression = CompressionType.Ccitt4

End Sub

Edited by user Thursday, December 20, 2007 4:53:19 PM(UTC)  | Reason: Not specified

Alex Kon  
#2 Posted : Tuesday, April 04, 2006 9:09:01 PM(UTC)
Alex Kon

Rank: Advanced Member

Joined: 1/31/2005(UTC)
Posts: 458

Was thanked: 5 time(s) in 5 post(s)

It is hard to determine error reliably (because I cannot compile posted sources), but it seems that the problem is in missing seek instruction for destination memory stream. Try to insert the following line after saving but before reading:
memStream.Seek(0, IO.SeekOrigin.Begin)

I have written simple code snippet which performs the following actions (as far as I understand you implement something like that):
  1. Load separate tiffs from files;
  2. Save them in a single tiff in memory;
  3. Load this single multipage tiff from memory stream;
  4. Split it into separate frames and save them to files (just for test, you can display them to user, etc).
Hope this will help to solve your issue.
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim filenames() As String = {"y:/0.tif", "y:/1.tif", "y:/2.tif"}
		Dim memStream As New System.IO.MemoryStream
		Dim tiffWriter As New Aurigma.GraphicsMill.Codecs.TiffWriter(memStream)

		'Merging separate tiff files into one big in memory stream
		LoadTiffsIntoMemoryStreamAsSingleTiff(tiffWriter, filenames)

		'Saving frames from one big tiff in memory stream into separate files - to check that frames can be loaded correctly
		memStream.Seek(0, IO.SeekOrigin.Begin)
		SaveFramesFromStreamToSeparateFiles(memStream, "y:/resavedFrames_")

		'To check that stream contents is ok
		memStream.Seek(0, IO.SeekOrigin.Begin)
		SaveWholeStreamToSingleFile(memStream, "y:/memStream.tif")

	End Sub

	Private Sub LoadTiffsIntoMemoryStreamAsSingleTiff(ByVal dstWriter As Aurigma.GraphicsMill.Codecs.TiffWriter, ByVal filenames() As String)
		For Each filename As String In filenames
			Dim srcReader As New Aurigma.GraphicsMill.Codecs.TiffReader(filename)
			Dim tiffFrame As Aurigma.GraphicsMill.Codecs.TiffFrame = CType(srcReader.LoadFrame(0), Aurigma.GraphicsMill.Codecs.TiffFrame)

			tiffFrame.Compression = Aurigma.GraphicsMill.Codecs.CompressionType.Ccitt4
	End Sub

	Private Sub SaveFramesFromStreamToSeparateFiles(ByVal srcStream As System.IO.Stream, ByVal filenamePrefix As String)
		Dim srcReader As New Aurigma.GraphicsMill.Codecs.TiffReader(srcStream)

		For i As Integer = 0 To srcReader.FrameCount - 1
			Dim frame As Aurigma.GraphicsMill.Codecs.IFrame = srcReader.LoadFrame(i)
			Dim frameImage As New Aurigma.GraphicsMill.Bitmap
			frameImage.Save(String.Format("{0}{1}.tif", filenamePrefix, i))


	End Sub

Edited by user Thursday, December 20, 2007 4:54:59 PM(UTC)  | Reason: Not specified

#3 Posted : Thursday, April 06, 2006 3:57:04 PM(UTC)

Rank: Member

Groups: Member
Joined: 4/3/2006(UTC)
Posts: 4


Thanks a lot for your suggestions, they have helped! I was definitely missing the "MStream.Seek(0, IO.SeekOrigin.Begin)" line (in my code to merge and save as well as in the code to display), and your method for adding frames was also a lot simpler and cleaner.

Once again , thank you.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.