Saving Group IV Tiff images as black and white

J

Jeff Gress

Thanks to help from a few people this very economical
amount of code creates a copy of a tif images, writes
data to it and saves as with a new name. It does almost
everything I want it to do and in an acceptable amount of
time.

My problem is that the resulting TIF image is not saved
as a black and white, grayscaled image, but with colors
and I don't know how to turn this off. Also the
resulting tif comes out to over 100K instead of around 10-
15K.

If I overload the bitmap.Save method with
System.Drawing.Imaging.ImageFormat.Tiff not only does the
size jump to nearly 350K, but I then end up with a TIF
image that requires LZW compression instead of the Group
IV TIF I want.

I have seen examples that use unsafe code to accomplish
this, but the example is slower than I'd like and is
several hundreds of lines of code when it seems to me
like I should be able to add a line or two of code to the
following dozen or so lines I have and be done.

What am I missing?

Thanks,

Jeff Gress


private Font textFont;
private FontFamily serifFontFamily;

serifFontFamily = new FontFamily
(GenericFontFamilies.Serif);
textFont = new Font(serifFontFamily, 24);

string textToDraw;

Image image = Image.FromFile(@"c:\apps\amod2.tif");
Bitmap bitmap = new Bitmap(image);
Graphics g = Graphics.FromImage(bitmap);

g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
g.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

textToDraw = "Welcome back my friend";

g.DrawString(textToDraw, textFont, new SolidBrush
(Color.Black), 184, 342);

// Set the resolution and save the bitmap object
bitmap.SetResolution(200,200);
bitmap.Save(@"c:\apps\amod3.tif");
 
J

Jeff Gress

Does that matter? When I do I does not save as a Group
IV tif anyway and since I save the image with the .tif
extension it appears to be a tif image. I'm thinking if
it looks like a duck and acts like a duck . . . :)

Honestly I think it is saving as a tif okay, my real
problem is the black and white vs colors problem. That's
what is killing me.

I don't want to have to add 400-500 lines of unsafe code
to change each pixel, which is the only option I've seen
work.

I feel like I've taken some sort of shortcut and that I
should be looking at colormatrix or encoder stuff or
something, but I'm lost and no one seems to be able to
give me a simple answer.

The resolution defaulted to 96 X 96. I used the line
bitmap.SetResolution(200,200); and that gave me my 200 X
200 resolution. I've been searching for the same kind of
method to change to black and white, thinking I was one
line of code away from finishing this. I think you see
why I'm getting frustrated.

Like I said in my original post "If I overload the
bitmap.Save method with System.Drawing.Imaging.ImageFormat
..Tiff not only does the size jump to nearly 350K, but I
then end up with a TIF image that requires LZW
compression instead of the Group IV TIF I want".

Maybe I have a lot more work to do, but I build a viewer
that is streamlined to view Group IV Tiff images and when
I take this large image into Paint, change the attributes
there to black and white my 100K image drops down to 14K
and looks great in my viewer, making me think I'm dealing
with a group IV Tiff image.

I'd hate to have to try to figure out how to call Paint
in the background to change the attributes of the image.
I can't think of anything dumber than that, creating a
100K image and calling a MSPaint API (if there is one) to
reduce the image down to black and white.

But by the same token it might be faster than anything
else I can do.

Thanks,

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top