Creating a B&W tiff file in vb 2003

R

Robert Dufour

I been trying to look for samplecodes on how to create a simple tif file. I
found a lot of commercial and stuff but I would prefer to roll my own. I
only need to put some formatted text in a tif file so it can be faxed out.
Does anyone now where I could find some sample source code. It can`t be all
that big a secret :)

Regards,
Bob
 
M

Michael C

Robert Dufour said:
I been trying to look for samplecodes on how to create a simple tif file. I
found a lot of commercial and stuff but I would prefer to roll my own. I
only need to put some formatted text in a tif file so it can be faxed out.
Does anyone now where I could find some sample source code. It can`t be all
that big a secret :)

You can't draw to a 2 color bitmap, so you'll need to create a 24 bit
bitmap, draw to it and then convert to 2 color.

Bitmap bitmap = new bitmap(100,100,PixelFormat.Format24bppRgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawStuff(....);
graphics.Dispose

Converting it to 2 color will be a little more complicated. When you get to
that stage let me know and I'll post something (I just want to check you're
still interested first :)

Michael
 
R

Robert Dufour

Thanks Michael,
Oh yes, I'm still VERY interested.
Taking your code as lead I come up with the following
Dim MyBitmap As Bitmap = New Bitmap(300, 300,
MyBitmap.PixelFormat.Format24bppRgb)

Dim MyGraphic As Graphics = Graphics.FromImage(MyBitmap)

Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold,
GraphicsUnit.Pixel)

MyGraphic.DrawString("This is a test!", font1, Brushes.Black, 100, 25)

MyGraphic.Dispose()

From what I understand that should give me a bitmap with text "This is a
test", located 100 from left and 25 from top - Right?

Now I have to end up changing that to a tif file and saving it with
dpiX = 240, dpiY = 196, color = 1, width = 1728 pixels and compression =
CCIT Group 3 Fax
So I assume it happens after the drawstring, question is, how?
Thanks for your help
Bob
 
R

Robert Dufour

I tried saving the bitmap but opening it up with a viewer or paintbrush I
just get a black square
What do I do wrong?
I'm really a newbie at this so please bear with me,
Thanks,
Bob
 
R

Robert Dufour

Got it needed to do clear on the bitmap and set background to white before
writing the text.
So now what :)
Bob
 
M

Michael C

Robert Dufour said:
Got it needed to do clear on the bitmap and set background to white before
writing the text.
So now what :)

The first thing I would do is save it as 24 bit and see if your fax program
accepts it. It might be better (and easier for you) to have it convert it as
it may be optimised for faxing. If not you'll need to convert it yourself.
To do this you would create a second bitmap of 1bit and use LockBits to copy
the data quickly. Something like this

dim bitmap2 as Bitmap = new bitmap(bitmap1.Width, bitmap1.Height,
PixelFormat.1Bit)
dim data2 as BitmapData = bitmap2.LockBits(1bit, WriteOnly)
dim data1 as BitmapData = bitmap1.LockBits(24bit, ReadOnly)
dim ptr2 as IntPtr = data2.Scan0
dim ptr1 as IntPtr = data1.Scan0
dim x as int, y as int
dim array1(0 to data1.width * 3 - 1) as byte
dim array2(0 to data2.width / 8 - 1) as byte
for y = 0 to data2.height - 1
Marshal.Copy(ptr1, array1)
for x = 0 to data2.width - 1
'*********
next
Marshal.Copy(array2, ptr2)
ptr1 = ptr1 + data1.Stride
ptr2 = ptr2 + data2.Stride
next
bitmap1.UnlockBits(data1)
bitmap2.UnlockBits(data2)

the **** bit is the tricky part. You need to decide how to convert from 16
million color to 2 color. The simplest would be to convert the color to
grayscale and then anything over a level of 127 would be considered black.
So something like this:

dim r as int = array1(x * 3)
dim g as int = array1(x * 3 + 1)
dim b as int = array1(x * 3 + 2)
dim c as int = CInt(r * 0.2125 + g * 0.7154 + b * 0.0721)
array2(x / 8) = array2(x / 8) * 2
if c > 127 then array2(x / 8) = array2(x / 8) + 1

What this is doing is getting the red, green, blue out of the first array,
then converting it to a grayscale equivelant. Then because the next bitmap
has 8 pixels per byte we need to keep rolling the byte to the left while
adding a pixel to the right most bit (if it is above 127).

This is of the top of my head so likely there'll be some bits you'll need to
fix. THe lockbits function takes a param of a rect the size of the bitmap,
the pixelformat, and whether to read/write etc.

Michael
 
R

Robert Dufour

Thanks I got some code but running in a problem, maybe you could tell me
why.
Just at the beginning I coded (in a button click event on a form)
Dim MyBitmapRgb As Bitmap

'Its always going to be this size in pixels

MyBitmapRgb = New Bitmap(1728, 2261, MyBitmapRgb.PixelFormat.Format24bppRgb)

Dim MyGraphicRGB As Graphics = Graphics.FromImage(MyBitmapRgb)

MyGraphicRGB.Clear(System.Drawing.Color.White)

Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold,
GraphicsUnit.Pixel)

MyGraphicRGB.DrawString("This is a test!", font1, Brushes.Black, 100, 25)

''''DO ALL OF THE OTHER WRITING TO THE RGB bitmap

'then create the b&w bitmap using same settings for its size in pixels as
rgb bitmap

Dim MyBitmapBW As Bitmap = New Bitmap(MyBitmapRgb.Width, MyBitmapRgb.Height,
PixelFormat.Format1bppIndexed)



''Lock the rgb and B&W bitmaps

Dim dataBW As BitmapData = MyBitmapBW.LockBits(New Rectangle(0, 0,
MyBitmapRgb.Width, MyBitmapRgb.Height), ImageLockMode.WriteOnly,
PixelFormat.Format1bppIndexed)

When I do this lockbits, the stride of the dataBW array is 216, which means
that as I run through the code in the for X loop I run out of bytes and end
up with a system overflow exception because it runs out of array items in
the databw byte array.

I've been looking through the docs trying to understand whats going on but I
can't seem to find anything that explains whats happening.

Thanks for any help.

Bob
 
R

Robert Dufour

Well, the dialogic fax boards absolutely require either txt files like
Notepad files or tif files in a VERY specific format
they have to be Color Depth = 1, 1728 pixels wide, horX 204,verY 196,
Multipage tiff, compression CCIT GRoup3 Fax and single striped segmentation,
anything else just will not get transmitted. I been testing this for over
two weeks now with different commercial conversion tools and the one that I
could use best was via a printer driver that saved an output file in the
correct format. However, since I only have to send a single page always the
same except for some text that is being changed from this program I want to
keep the setup and configuration really simple and want to be able to trap
errors in the creation of the document in my code and kee p it light for
execution and maintenace. Therefore I thought it better to roll my own for
this specific use.
To start off I only have two colors anyways, white background and black
text.

Thanks you for your help I'll give it a try and keep you informed.

Regards,
Bob
 
R

Robert Dufour

Finally got the conversion OK to a monochrome bitmap, now all I gotta do is
encode the bitmap to a tiff format thats gonna be compatible with Dialogic
fax boards which is 1728 width , Bits per pixel = 1, Xres = 204, Yres 196,
bccitgroup 3 fax compression. I gues I will have to use the encoder to save
it in the correct format.

Bob
 

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