Solved

How to Load Single Page From .tif file.

Posted on 2010-11-12
5
756 Views
Last Modified: 2012-06-27
I am currently trying to write a .Tif image viewer in .NET and am running into problems with loading individual pages from the file.

Current Issues/Comments:
1) I know how to load an image from disk using the System.Drawing.Image.FromFile funciton. From there, I can set the Active frame of the image to get to the specific pages - however, this method means I have to load the entire image before traversing the pages. What I want is to just load say, page 2 from a file without having to load the entire file into memory first.
2) I'm currently using .NET. I know this may not be the best solution. If you know of a way for me to do it in another language please speak up.
3) One of my requirements here is that I either must write this from scratch or use a completely royalty free (for commercial) third party tool.

So to summarize, is there a way to load a single page from a .tif file? Idealy, I would assume I need to read the file into a stream, seek to the appropriate place and create the image (page) from that subset of the stream.

Please help.
0
Comment
Question by:PaperlessEnvironments
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34123913
0
 

Author Comment

by:PaperlessEnvironments
ID: 34147817
This code is pretty helpful but not exactly what I'm looking for. In his examples he is still loading the Image using the FromFile method.. for which my understanding is that this loads the entire image into memory.

Does anyone know of a way to only read in specific pages of a file. Maybe by somehow reading the header and then seeking to the position of the page in the file??
0
 
LVL 8

Accepted Solution

by:
davesgonebananas earned 500 total points
ID: 34151141
You could use the TiffBitmapDecoder class in WPF http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffbitmapdecoder.aspx

Alternatively, you could use the third-party LibTiff - available in native .NET format http://bitmiracle.com/libtiff/
0
 

Author Comment

by:PaperlessEnvironments
ID: 34158332
OK, the TiffBitmapEncoder/TiffBitmapDecoder classes are awesome.

Here is what I'm showing in testing:
I have an 80 page tif file where I want to save out page 50 to a separate file...

Using GDI+ it takes 1.01 seconds
Using WPF it takes only 0.09 seconds

MUCH FASTER!!

Now for me to scrap GDI+ completely I need to figure out how to insert/delete pages from the source image. The code below is what I'm working with so far.. this is slow and prone to OOM error... there's got to be a better way. Any ideas?



Dim imageStreamSource As New FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

                Dim decoder As New TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)



                Dim encoder As New TiffBitmapEncoder()



                For i As Integer = 0 To decoder.Frames.Count - 1

                    encoder.Frames.Add(BitmapFrame.Create(CType(decoder.Frames(i), BitmapSource)))

                Next



                imageStreamSource.Close()



                Dim stream As New FileStream(FileName, FileMode.Open)

                Dim sm As New System.IO.MemoryStream

                ImageToInsert.Save(sm, System.Drawing.Imaging.ImageFormat.Tiff)

                encoder.Frames.Insert(2, BitmapFrame.Create(sm))



                encoder.Save(stream)

                stream.Close()

                sm.Close()

Open in new window

0
 
LVL 8

Expert Comment

by:davesgonebananas
ID: 34158686
It appears to be possible to create a BitmapSource without using a MemoryStream object.  

http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Framework versus framework 64 in IIS 8.5 5 81
Exception in Log4Net 1 19
Set form below another form 3 26
Showdialog 8 21
I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The goal of the tutorial is to teach the user how to use import presets downloaded from the internet into Adobe Lightroom. Once you downloaded the presets go into the preset folder and press import then import your preset and your set it to go.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now