472,326 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Creating a B&W tiff file in vb 2003

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
8 1417
"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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shanergb | last post by:
Hi all. I'm creating a web-based application that uses PHP, MySQL & Apache on a Windows platform. What I need to do is create images on the fly...
2
by: Robin Becker | last post by:
Has anyone done transparency with PIL & TIFF? I'm using PIL to generate a preview TIFF for embedding into an eps file and am being asked for the...
0
by: Stephan Lux | last post by:
Hi all, I have developed a windows service with Visual Basic .NET which starts a file conversion tool via the commandline. The tool open the...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet...
7
by: Bob Day | last post by:
Using VS 2003... Is there a way to create a tif file on the fly to be faxed? I need a simply cover page (simple logo, to/from, etc.) and am...
1
by: aljosa | last post by:
i'm trying to convert python (image resizer script using PyQt4) script to exe but support for jpeg and tiff image formats is located in...
9
by: Robert Dufour | last post by:
I need to create a tif file and write some simple text to it, about 4 or 5 lines, Just some thing like "Hello this is mycompany" "We will make a...
4
by: G E W hittle | last post by:
I have a directory of tiff files. I need to loop thru the files and count the number of pages in each file. Can someone point me to where this info...
3
by: GiJeet | last post by:
Hello, we have an app that scans documents into TIFF format and we need to transfer them over the internet. If anyone knows of a SDK we can use...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.