Advertisement

06.04.2008 at 06:46PM PDT, ID: 23458942
[x]
Attachment Details

Loading a tiff image in C#.NET, Converting it to a Bitmap, then releasing hold of the physical file

Asked by darkxenn in .NET, C# Programming Language

Tags: C#.NET, Visual Studio 2008

Hey,

===Preamble===
The title pretty much sums up what I am trying to do. I have created a class to help me streamline dealing with Tiff images in .NET. The problem I am having is showing that image, and acting on the file at the same time.

===Problem===
I'll explain what I am currently doing in Code Sample #1 provided. I'm loading the tiff file into an image object. I'm extracting various pieces of information about the file, then I'm loading individual pages/frames (I'm generally dealing in Tiffs with a bare minimum of at least 25 pages) into MemoryStreams which I then use to load Bitmap Objects from, which I then store in an ArrayList for later use (paging through the tiff).

The problem I need solved happens when Code Sample #2 runs. I load up the TiffImage object. I set the PictureBox's image to one of the bitmaps I have stored in the TiffImage, then I try to delete the file, or move the file. I get an error politely informing me that I can not access that object (The program still has a hold of it... I can do anything I want to it once the process is over, but during, that file is nailed down.

===Solutions I have already come up with and why I don't want to use them===
1)Make a copy of the file first before loading it, and have the form view the copy of the file wihle doing whatever is needed on the real file
<no>) Some of the tiffs I am dealing with get really big, and that not only adds extra bogus disk space, but it adds extra wait time.

2)Load the file and then ditch it all every time I need to access a new page or piece of information. This is what I am currently doing just so I can get the rest of my project up and limping along while I figure out what to do. This, obviously, is not Code Sample #1, but I don't want to post what I'm doing now because it's such a junky hack that I'm embarassed to even admit to doing it.
<no>) This is not acceptable, because every "next frame" and query on the image takes way too long (longer than an eyeblink for something that should be shorter than instant).

===Questions I need answered===
1) How do I go about showing a tiff in a windows form  while at the same time maintaining an ability to delete/move/etc the physical file itself, without having to first make a copy of the file.
2) When putting the file into memory like I have shown I do above, is there a way to compress the individual pages I'm getting off of the tiff into something like a jpg (does ImageFormat.Jpeg actually do the compression, or does it just make the same filesize readable as a jpeg?) without outside software or too much extra work?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
//
//
//Code Sample #1
//
//
        private string myPath;
        private Guid myGuid;
        private FrameDimension myDimension;
        private ArrayList myImages = new ArrayList();
        private int myPageCount;
 
        public TiffImage(string path)
        {
            MemoryStream ms;
            Image myImage;
 
            myPath = path;
            FileStream fs = new FileStream(myPath, FileMode.Open);
            myImage = Image.FromStream(fs);
            myGuid = myImage.FrameDimensionsList[0];
            myDimension = new FrameDimension(myGuid);
            myPageCount = myImage.GetFrameCount(myDimension);
            for (int i = 0; i < myPageCount; i++)
            {
                ms = new MemoryStream();
                myImage.SelectActiveFrame(myDimension, i);
                myImage.Save(ms, ImageFormat.Bmp);
                myBMP = new Bitmap(ms);
                myImages.Add(myBMP);
                ms.Close();
            }
            fs.Close();
        }
 
//
// Code Sample #2
 
TiffImage myTiff = new TiffImage(path);
//imageBox is a PictureBox control, and the [] operators pass back 
//the Bitmap stored at that position in the myImages ArrayList in the TiffImage
imageBox.Image = myTiff[0];
File.Delete(path);
[+][-]06.04.2008 at 07:30PM PDT, ID: 21715978

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, C# Programming Language
Tags: C#.NET, Visual Studio 2008
Sign Up Now!
Solution Provided By: Idle_Mind
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.05.2008 at 05:58AM PDT, ID: 21718806

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628