473,414 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

I'm having trouble opening a color .tiff.

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

Calling Bitmap.FromFile on the .tiff throws a "System.OutOfMemory" 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.
Mar 18 '10 #1
11 6031
tlhintoq
3,525 Expert 2GB
System.OutOfMemory 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?
Mar 18 '10 #2
I can't open any color tiff using Image.FromStream, Bitmap.FromStream 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.
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.  
  4.             StringBuilder sb = new StringBuilder();
  5.             AttemptOpenFile("BlackAndWhiteScan.tif",sb);
  6.             AttemptOpenFile("ColorScan.tif", sb);
  7.             Console.Write(sb.ToString());
  8.             Console.ReadLine();
  9.         }
  10.  
  11.         private static void AttemptOpenFile(string fileName, StringBuilder sb)
  12.         {
  13.             sb.AppendLine(fileName);
  14.             try
  15.             {
  16.                 using (FileStream fs = File.OpenRead(fileName))
  17.                 {
  18.                     using (Image img = Image.FromStream(fs)) { sb.AppendLine("One Success"); }
  19.  
  20.                 }
  21.             }
  22.             catch (Exception ex)
  23.             {
  24.                 sb.AppendLine("One Failed");
  25.                 sb.AppendLine(ex.ToString());
  26.             }
  27.  
  28.             try
  29.             {
  30.                 using (Image img = Bitmap.FromFile(fileName)) { sb.AppendLine("Two Success"); }
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 sb.AppendLine("Two Failed");
  35.                 sb.AppendLine(ex.ToString());
  36.             }
  37.  
  38.             try
  39.             {
  40.                 using (FileStream fs = File.OpenRead(fileName))
  41.                 {
  42.                     using (Image img = Bitmap.FromStream(fs, true, false)) { sb.AppendLine("Three Success"); }
  43.                 }
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 sb.AppendLine("Three Failed");
  48.                 sb.AppendLine(ex.ToString());
  49.             }
  50.         }
Mar 18 '10 #3
tlhintoq
3,525 Expert 2GB
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.
http://www.codeproject.com/KB/cs/mul...iffviewer.aspx
Mar 18 '10 #4
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
File Type: zip OpenColorTifProblem.zip (810.1 KB, 222 views)
Mar 18 '10 #5
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.
Mar 18 '10 #6
tlhintoq
3,525 Expert 2GB
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?
Mar 18 '10 #7
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.
Mar 18 '10 #8
tlhintoq
3,525 Expert 2GB
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.
http://www.lmgtfy.com/?q=C%23+open+t...UTF-8&oe=UTF-8


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.
Mar 18 '10 #9
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.
Mar 18 '10 #10
tlhintoq
3,525 Expert 2GB
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.
http://www.codeproject.com/KB/GDI-plus/tiffmanager.aspx

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"
Mar 18 '10 #11
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.
Mar 18 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Singh (UCSD) | last post by:
Hello all I get an image from the file in bitmap format(6 meg approx). What I want to do is first change the image to grayscale, reduce the dpi to like 150, and then change the format to tiff,...
0
by: dalabera | last post by:
Hi, I had the following snippet to save a tiff file with the property tag of fillorder wich I need for a fax application: Bitmap i = (Bitmap)Image.FromFile(dlg.FileName); PropertyItem proitem =...
3
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
0
by: Will Arrowsmith | last post by:
Hi All, I am trying to create a .tiff file to fax using the windows fax service FAXCOMLib. I have created an array of images (bitmaps) and converted them to 1pbb format in order to allow...
1
by: Stedak | last post by:
I have the following class I use to save Tiff's. The problem I have with it is that the final size of the images are very large. If we scan directly to a file the final tiff may be 600-900 kb.s but...
5
by: sascha.folville | last post by:
Hi, I've some trouble reading the containing frames of a multiframe tiff. First frame is a 1700X2400 px CCITT4, second in JPG 820x1200, third is a CCITT again ... and so on. My function...
10
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi, Please help me to write a dll in C# , that will read each pages of a tiff image from a file and a memory stream object ( need two ways) and creatre a new tiff image object.The dll should...
1
by: Rishi Boparai | last post by:
Hi, in Visual Studios there's this tool called PictureBox rite...but in Visual Web developer i can't seem to find it.. i'm using Visual Web Developer...VB language..to make a watermarking...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.