Click here to Skip to main content
12,398,557 members (58,985 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

Bitmap to Tiff conversion using Libtiff

, 6 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Bitmap to Tiff conversion using Libtiff


What the article/code snippet does? : It converts 256 color bitmap file to Black & white tiff file using Libtiff.

Why it's useful? : This Article provides a method to convert Bitmap Image to Tiff Image. The main target of this demo project is to tell you about writing a tiff file. I was trying from so long time to get example code that can write tiff file. But no one can provide one as simple as I wished it to be. Now this code would at least help you. I've included the Bitmap Image used in this demo.

Where is Libtiff? : You can get Libtiff Library free from Also, I've included Library files in my demo project. All library contents are as it is provided & with their owner rights. I have no concern with that Library's bugs. I am just using this library to show how to convert a bmp to a tiff.

How to use this code

I've collected all conversion code in a single function which converts bmp to tiff, Function is given below or you can fin this code in demo project.

//load Bitmap

HBITMAP hImage = (HBITMAP)LoadImage(NULL, "C:\\MyBit.bmp", IMAGE_BITMAP,

CBitmap* m_Bitmap = CBitmap::FromHandle(hImage);

//Memory allocation is still 600x600 in your code..
BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, 600*600);//allocate memory

// Size of bitmap as I draw by using x,y points...
m_Bitmap->GetBitmapBits(600*600 ,bmpBuffer);

TIFF *image;

  // Open the TIFF file
  if((image = TIFFOpen("C:\\output.tif", "w")) == NULL)
    printf("Could not open output.tif for writing\n");

  TIFFSetField(image, TIFFTAG_IMAGEWIDTH,600);

  uint32 rowsperstrip = TIFFDefaultStripSize(image, -1); 
  //<REC> gives better compression

  TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);

// Start CCITTFAX3 setting
  TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options);

// End CCITTFAX3 setting


  TIFFSetField(image, TIFFTAG_XRESOLUTION, 100.0);
  TIFFSetField(image, TIFFTAG_YRESOLUTION, 100.0);

   char page_number[20];
    sprintf(page_number, "Page %d", 1);

    TIFFSetField(image, TIFFTAG_PAGENUMBER, 1, 1);
    TIFFSetField(image, TIFFTAG_PAGENAME, page_number);

  // Write the information to the file
BYTE *bits;
for (int y = 0; y < 600; y++)
    bits= bmpBuffer + y*600;
    if (TIFFWriteScanline(image,bits, y, 0)==-1) MessageBox("Complete or error");

  // Close the file

What's New in this update:

  1. Improved Compression
  2. Multi-page Tiff (Method to add pages in tiff)
  3. 256 colors of bmp

What is Next?

  1. Color in tiff file
  2. Other format to tiff

My wish was to provide you one independent function that can be used anywhere. That's what I did in this article.

What am I expecting from you all?

As I think, I'm not an expert like you, long way yet remain to take over. Please point out my bugs in this article so I can improve those. Thanks, OK! Good Bye, Have a nice life...


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sumit Kapoor
Software Developer (Senior)
India India
Working in Patni Computer Systems, Noida(INDIA). I like to work in C/C++ even from my school time & mostly worked using C++, VC++, COM.

I want to give something bigger than biggest to IT field. Try is going on.

I like to make friends.
That’s all about me.

Ok! See you.
Have a nice Life.

You may also be interested in...

Comments and Discussions

GeneralReading Multiple images of Tiff to IplImage format Pin
Sai Veeranki18-Feb-11 8:01
memberSai Veeranki18-Feb-11 8:01 
QuestionReading All the images from the tiff Pin
Sai Veeranki10-Feb-11 10:46
memberSai Veeranki10-Feb-11 10:46 
QuestionHow to convert Tiff file to BMP Pin
Nilesh Hirpara2-Aug-10 11:44
memberNilesh Hirpara2-Aug-10 11:44 
QuestionCan yoiu show me how to go the other way? TIFF-&gt;BMP Pin
Mick Leong29-Dec-09 20:39
memberMick Leong29-Dec-09 20:39 
Generalhi Pin
Aric Green21-Dec-09 18:03
memberAric Green21-Dec-09 18:03 
GeneralLoadImage() error ERROR_NOT_ENOUGH_MEMORY Pin
renoreballos28-Nov-08 22:41
memberrenoreballos28-Nov-08 22:41 
GeneralRe: LoadImage() error ERROR_NOT_ENOUGH_MEMORY Pin
Sumit Kapoor5-Dec-08 5:54
memberSumit Kapoor5-Dec-08 5:54 
General16 bits tiff Pin
smart_dummies3-Nov-08 12:32
membersmart_dummies3-Nov-08 12:32 
Generalbmp to ccitt g31d convertion Pin
Member 39495025-Oct-08 20:16
memberMember 39495025-Oct-08 20:16 
GeneralTo change Jpeg compresed tif file to ccitt Compresed tiff file Pin
kiran_chikhale_IVI24-Oct-07 2:47
memberkiran_chikhale_IVI24-Oct-07 2:47 
QuestionDo you have any sample for tiff to bmp? Pin
jasonshen2067-Sep-07 17:53
memberjasonshen2067-Sep-07 17:53 
QuestionCan i convert any bmp image to tiff ccitt group 4 image Pin
Anil Pandya23-Jul-07 5:26
memberAnil Pandya23-Jul-07 5:26 
GeneralLibrary Linking Error(Solution) Pin
Sumit Kapoor19-Apr-07 20:31
memberSumit Kapoor19-Apr-07 20:31 
GeneralConversion to Color TIFF Pin
Sabareeswaran18-Dec-06 2:40
memberSabareeswaran18-Dec-06 2:40 
GeneralJuanma Pin
x3yulo9-Feb-06 0:36
memberx3yulo9-Feb-06 0:36 
AnswerRe: Juanma (How to run Debug version of project) Pin
Sumit Kapoor9-Feb-06 0:59
memberSumit Kapoor9-Feb-06 0:59 
GeneralRe: Juanma (How to run Debug version of project) Pin
x3yulo9-Feb-06 21:30
memberx3yulo9-Feb-06 21:30 
AnswerRe: Juanma (How to run Debug version of project) Pin
Sumit Kapoor10-Feb-06 13:55
memberSumit Kapoor10-Feb-06 13:55 
GeneralBitmap 1280x960 Pin
jeff_dodgeit31-Oct-05 16:51
memberjeff_dodgeit31-Oct-05 16:51 
GeneralRe: Bitmap 1280x960 Pin
Sumit Kapoor31-Oct-05 21:58
memberSumit Kapoor31-Oct-05 21:58 
GeneralRe: Bitmap 1280x960 Pin
dpatd11-Nov-10 5:13
memberdpatd11-Nov-10 5:13 
GeneralRe: Bitmap 1280x960 Pin
dpatd11-Nov-10 5:18
memberdpatd11-Nov-10 5:18 
GeneralRe: Bitmap 1280x960 Pin
Sumit Kapoor11-Nov-10 5:52
memberSumit Kapoor11-Nov-10 5:52 
GeneralRe: Bitmap 1280x960 Pin
dpatd11-Nov-10 11:04
memberdpatd11-Nov-10 11:04 
GeneralRe: Bitmap 1280x960 Pin
Sumit Kapoor11-Nov-10 16:22
memberSumit Kapoor11-Nov-10 16:22 
GeneralTrying to compile, but... Pin
lets_pandora119-Oct-05 3:50
memberlets_pandora119-Oct-05 3:50 
GeneralRe: Trying to compile, but... Pin
Sumit Kapoor19-Oct-05 3:58
memberSumit Kapoor19-Oct-05 3:58 
GeneralError msg Pin
lets_pandora120-Oct-05 4:21
memberlets_pandora120-Oct-05 4:21 
GeneralRe: Error msg Pin
Sumit Kapoor20-Oct-05 13:53
memberSumit Kapoor20-Oct-05 13:53 
GeneralRe: Thanks... Pin
lets_pandora125-Oct-05 5:19
memberlets_pandora125-Oct-05 5:19 
GeneralCan&#180;t find a solution... Pin
lets_pandora11-Nov-05 3:58
memberlets_pandora11-Nov-05 3:58 
GeneralRe: Can&amp;#180;t find a solution... Pin
Sumit Kapoor1-Nov-05 12:57
memberSumit Kapoor1-Nov-05 12:57 
GeneralRe: Can&amp;#180;t find a solution... Pin
NixDude31-May-07 12:57
memberNixDude31-May-07 12:57 
GeneralExternal Exception Pin
lets_pandora19-Nov-05 3:13
memberlets_pandora19-Nov-05 3:13 
Generalccitt group 4(2d) Fxa Pin
MLSHWJZ1-Jul-05 10:39
memberMLSHWJZ1-Jul-05 10:39 
GeneralRe: ccitt group 4(2d) Fxa Pin
Sumit Kapoor3-Jul-05 3:11
memberSumit Kapoor3-Jul-05 3:11 
Questionbmp to tiff in colour? Pin
leojose27-May-05 3:33
memberleojose27-May-05 3:33 
AnswerRe: bmp to tiff in colour? Pin
Sumit Kapoor30-May-05 3:36
memberSumit Kapoor30-May-05 3:36 
GeneralRe: bmp to jpeg conversion Pin
Arif Mohammed20-Jul-05 15:47
memberArif Mohammed20-Jul-05 15:47 
GeneralRe: bmp to jpeg conversion Pin
Sumit Kapoor26-Jul-05 23:12
memberSumit Kapoor26-Jul-05 23:12 
Generala simple interface Pin
Manish K. Agarwal13-Mar-05 18:04
memberManish K. Agarwal13-Mar-05 18:04 
GeneralRe: a simple interface Pin
Sumit Kapoor13-Mar-05 19:54
memberSumit Kapoor13-Mar-05 19:54 
QuestionHow to make multi-page tiff file Pin
zuken212-Feb-05 22:23
memberzuken212-Feb-05 22:23 
AnswerRe: How to make multi-page tiff file Pin
Sumit Kapoor8-Feb-05 23:31
memberSumit Kapoor8-Feb-05 23:31 
GeneralRe: How to make multi-page tiff file Pin
zuken2113-Feb-05 19:59
memberzuken2113-Feb-05 19:59 
GeneralRe: How to make multi-page tiff file Pin
zuken2128-Feb-05 21:43
memberzuken2128-Feb-05 21:43 
GeneralRe: How to make multi-page tiff file Pin
lets_pandora125-Oct-05 7:16
memberlets_pandora125-Oct-05 7:16 
GeneralRe: How to make multi-page tiff file Pin
mohammadftm.er6430-Nov-09 23:25
membermohammadftm.er6430-Nov-09 23:25 
Questionwhat's TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options)? Pin
Ayato Kamina4-Dec-04 22:36
memberAyato Kamina4-Dec-04 22:36 
AnswerRe: what's TIFFSetField(image, TIFFTAG_GROUP3OPTIONS, group3options)? Pin
Ayato Kamina4-Dec-04 22:42
memberAyato Kamina4-Dec-04 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Oct 2004
Article Copyright 2003 by Sumit Kapoor
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid