By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,583 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,583 IT Pros & Developers. It's quick & easy.

Creating a B&W tiff file in vb 2003

P: n/a
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
Mar 13 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:uR**************@TK2MSFTNGP03.phx.gbl...
>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
Mar 14 '07 #2

P: n/a
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
"Michael C" <no****@nospam.comwrote in message
news:OX**************@TK2MSFTNGP06.phx.gbl...
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:uR**************@TK2MSFTNGP03.phx.gbl...
>>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

Mar 14 '07 #3

P: n/a
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
"Michael C" <no****@nospam.comwrote in message
news:OX**************@TK2MSFTNGP06.phx.gbl...
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:uR**************@TK2MSFTNGP03.phx.gbl...
>>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

Mar 14 '07 #4

P: n/a
Got it needed to do clear on the bitmap and set background to white before
writing the text.
So now what :-)
Bob

"Robert Dufour" <bd*****@sgiims.comwrote in message
news:eL**************@TK2MSFTNGP04.phx.gbl...
>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
"Michael C" <no****@nospam.comwrote in message
news:OX**************@TK2MSFTNGP06.phx.gbl...
>"Robert Dufour" <bd*****@sgiims.comwrote in message
news:uR**************@TK2MSFTNGP03.phx.gbl...
>>>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


Mar 14 '07 #5

P: n/a
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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
Mar 15 '07 #6

P: n/a
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

"Michael C" <no****@nospam.comwrote in message
news:uh**************@TK2MSFTNGP05.phx.gbl...
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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

Mar 16 '07 #7

P: n/a
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
"Michael C" <no****@nospam.comwrote in message
news:uh**************@TK2MSFTNGP05.phx.gbl...
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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

Mar 16 '07 #8

P: n/a
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

"Michael C" <no****@nospam.comwrote in message
news:uh**************@TK2MSFTNGP05.phx.gbl...
"Robert Dufour" <bd*****@sgiims.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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

Mar 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.