Unable to Open Color Tiff using Image.FromStream or Bitmap.FromFile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tregan3
    New Member
    • Aug 2006
    • 8

    Unable to Open Color Tiff using Image.FromStream or Bitmap.FromFile

    I'm having trouble opening a color .tiff.

    Calling Image.FromStrea m on the .tiff throws an "invalid parameter" error.

    Calling Bitmap.FromFile on the .tiff throws a "System.OutOfMe mory" error (the file size is 647k).

    The .tiff itself is not corrupt, I can open it using MS Picture Manager or MS Document Imaging (although when I open it using Windows Picture and Fax Viewer I get a "preview not available" message).

    The *.tiff comes from a scanned document; when I scan the document in black & white the .tiff opens fine, this certainly has to do with it being in color.

    I am using Atalasoft controls on the web page where the image is uploaded. I can create an ImageInfo object from the .tiff and can see from it that the .tiff is Image Type Tiff, Pixel24bppBgr, 300 dpi.

    If anyone has source code they use to succesfully open a color .tiff I would very much appreciate seeing it.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    System.OutOfMem ory is a crappy error. That is what gets thrown a lot of time for the wrong reason. If you try to open a file (from your application) that is still being written (by the scanning application) for example, you'll get "out of memory"

    Bitmap.FromFile should be avoided. MS says its not a bug, but that call will keep a an open link to the file on HDD for as long as you are using the picture - and then a while longer. Basically for as long as your application is still open the O.S. thinks it is linked to the file.

    That's as much help as I have... Anyone else?

    Comment

    • tregan3
      New Member
      • Aug 2006
      • 8

      #3
      I can't open any color tiff using Image.FromStrea m, Bitmap.FromStre am or Bitmap.FromFile .

      Here is a console app that demonsrates what I mean. The black and white tiff opens fine using all 3 methods. The color tiff fails on all 3 methods.

      I see no way on this forum to attach the tif files, but I imagine any color tif would generate the error.
      Code:
      static void Main(string[] args)
              {
      
                  StringBuilder sb = new StringBuilder();
                  AttemptOpenFile("BlackAndWhiteScan.tif",sb);
                  AttemptOpenFile("ColorScan.tif", sb);
                  Console.Write(sb.ToString());
                  Console.ReadLine();
              }
      
              private static void AttemptOpenFile(string fileName, StringBuilder sb)
              {
                  sb.AppendLine(fileName);
                  try
                  {
                      using (FileStream fs = File.OpenRead(fileName))
                      {
                          using (Image img = Image.FromStream(fs)) { sb.AppendLine("One Success"); }
      
                      }
                  }
                  catch (Exception ex)
                  {
                      sb.AppendLine("One Failed");
                      sb.AppendLine(ex.ToString());
                  }
      
                  try
                  {
                      using (Image img = Bitmap.FromFile(fileName)) { sb.AppendLine("Two Success"); }
                  }
                  catch (Exception ex)
                  {
                      sb.AppendLine("Two Failed");
                      sb.AppendLine(ex.ToString());
                  }
      
                  try
                  {
                      using (FileStream fs = File.OpenRead(fileName))
                      {
                          using (Image img = Bitmap.FromStream(fs, true, false)) { sb.AppendLine("Three Success"); }
                      }
                  }
                  catch (Exception ex)
                  {
                      sb.AppendLine("Three Failed");
                      sb.AppendLine(ex.ToString());
                  }
              }
      Last edited by Frinavale; Mar 18 '10, 02:45 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        TIFF has a few options such as LZW compression as well as multiple images within one file. You are probably bumping up against one of those issues.

        You should start with a TIFF of a known condition: One image, no LZW compression. Then test with a tiff of one image, yes compression. And so on. Try the various permutation to see exactly what is causing the failure.

        There are several TIFF classess available on the net.

        Opening up other source code is a great way to learn techniques.
        This project for a TIFF viewer would probably be a terrific source to learn from.

        Comment

        • tregan3
          New Member
          • Aug 2006
          • 8

          #5
          Attachment

          The attachment is a console application with 2 tiffs that demonstrates the problem. There does not seem to be any way to open a tiff with color in it using the MS Image or Bitmap objects.

          Would appreciate any code sample on opening a .tiff that has color.
          Attached Files

          Comment

          • tregan3
            New Member
            • Aug 2006
            • 8

            #6
            The sample project calls Image.FromFile to open the .tiff. I was aware of that method. The problem is that the method does not work when a .tiff contains color.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Have you tested it (as suggested) on TIFF's of varying content?
              Have you tested it on a TIFF with no LZW compression (which is only present in color TIFFs)
              Have you tested it on a TIFF of only one image?

              Have you tried looking at the source code of the project you were pointed at to see how it is being done in a working application?

              Lots of good suggestions were made but it looks like you haven't actually tried any of them. Are you really just waiting for someone to post the code for you to copy/paste into your application?

              Comment

              • tregan3
                New Member
                • Aug 2006
                • 8

                #8
                Yes, I looked at the project you pointed me to. That is how I was able to say that it uses the Image.FromFile method.

                I appreciate your suggestions, but they have nothing to do with the problem I am confronting. I need to do open a tif that has color in it. Yes, I am waiting for someone to post code for me to copy/paste into my application, if anyone has code that opens a tiff with color in it.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  Originally posted by Tregan3
                  Yes, I am waiting for someone to post code for me to copy/paste into my application,
                  If you can't be bothered to do some testing to find the exact nature of your problem (works without LZW compression, but fails if the file has LZW compression for example) then don't expect someone else to write your code for you.
                  For all those people who find it more convenient to bother you with their question rather than to Google it for themselves.



                  Originally posted by Original Poster
                  Can anybody send me code to [...]
                  The Bytes volunteers are not here to write your code for you. This is not a free homework service.
                  Bytes is very much a "Give me a fish I eat for a day. Teach me to fish I eat for a lifetime" kind of place. Just giving you the code doesn't help you learn near as effectively as good old-fashioned trial and error.

                  Do a little reading up and experimenting then if your trials aren't doing what you expect, post the code and relevant messages/errors and we'll see what we can do to point you in the right direction for making it work.

                  Try hitting Google with terms of your programming language and primary terms of what you want to do. For example "C# custom events" or "VB datagrid Excel". I've found this to be a very effective tool.

                  May I suggest
                  • First: Do some research. I heard about thing thing called Google, or books even.
                  • Second: Do some experiments yourself. You will learn so much from actually trying it before throwing up your hands in defeat before even starting.
                  • Third: Show the volunteers here the code that you created that was the closest to successful along with relevant errors.

                  Comment

                  • tregan3
                    New Member
                    • Aug 2006
                    • 8

                    #10
                    I do not understand your hectoring tone. I've done quite a bit of research on this and could not find a solution; that is why I posted the question.

                    You state that I did not post any code or do any experiments. That is false. I experimented and posted the code, a complete console application with two tiffs (one I can open; one I can't open).

                    Your comments about LZW compression are not relevant to the problem I am having. I am not compressing tiffs. I've no control over how the tiffs are compressed. The tiffs are given to me and I have to open them. I'm wondering if anyone knows how to open a .tif that has color in it.

                    Your comments are inaccurate and unfair. I do not understand why you are making these comments.

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Originally posted by Tregan3
                      I am not compressing tiffs. I've no control over how the tiffs are compressed.
                      Then you don't know if you are or aren't. You need to *make* a couple photos using a graphics program where you *do* have control so you have some known test images to work with. You can't diagnose a problem when you have no control over your test subjects.

                      The link I gave you provided numerous source codes for TIFF images including entire TIFF management classes such as this one.


                      I don't think you understand the complexity of the TIFF image format and all it's various options and permutations. It is not as simple as black and white versus color. It is not as simple as "here is the short method for a color image"

                      Comment

                      • tregan3
                        New Member
                        • Aug 2006
                        • 8

                        #12
                        I'm responding again only for the benefit of someone else who might be chasing this problem. Wrapped inside the sneers and condescending tone of the moderator were a few links to projects and code samples. Every one of those projects and code samples (which I had already found and looked at before I posted this question) use the Image.FromFile method or a derivation to load the tif. That call does not work on tiffs that contain color.

                        Comment

                        Working...