Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Printing in C

Posted on 2002-04-12
Medium Priority
Last Modified: 2013-12-03
I want to print image with C on printer.
So, how to do that(how to print pixel)?
Question by:darko_poljak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2

Expert Comment

ID: 6937182
Please be a bit more specific, e.g. which operating system
you are using. Under Unix you would normally convert the
graphics to PostScript and then send it to the printer by
opening a pipe (see manual page for popen)to the lpr

Hope it helps

Author Comment

ID: 6942645
Ok. Thanks. And what about Windows?
I mean MSDOS?

Expert Comment

ID: 6945248
Under MSDOS you can open the pseudo-file for the printer
(e.g. LPT1 -- in some MSDOS configurations you'll have to
use \DEV\LPT1 instead) for writing in binary mode:

  FILE *printer;
  printer fopen("LPT1", "wb");

Then you can send any control code to the printer using
putc() or fprintf(). What sort of control codes are used
to print graphics depends on your printer model -- these
should be listed in the printer's documentation or on the
web page of the manufacturer.

Hope it helps
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)


Expert Comment

ID: 6945258
Sorry, should be:

 FILE *printer;
 printer = fopen("LPT1", "wb");


Expert Comment

ID: 6947522
Are you really sure that works in DOS, opening the port as a file I mean??? I remember doing this in gw-basic some time back in the dark ages, but I didn't think it was possible from the more serious languages... An I certainly never heard of constructions like \DEV\LPT1 in DOS. - Sure?

Otherwise you'd have to go directly to the I/O port (at 0x378), or use the printer interrupt function (sorry, I forgot what number it has).

Expert Comment

ID: 6948179
Names like LPT1, COM1, and the like are handled differently
by the DOS kernel (sic! sounds like DOS would be an
operating system ;-). Even with newer Windows versions
these are not valid names for ordinary files -- try
creating a file called LPT1 or rename an existing one!

If you should not be able to open LPT1 use LPT1: instead.

In MSDOS there was a system call (INT 0x21) which made the
prefix \DEV\ mandatory to access devices -- even when
no directory \DEV existed; AFAIK with this option enabled
it was possible to create file like COM1 in any directory
but \DEV.

If you'd like to find out more about DOS' secrets visit
your favourite search engine and scan for Ralph Brown's
interrupt list -- you'll be surprised to find out that
spaces were perfectly legal characters in file names and
that the good old FCB API allows you to create and
access files with lower case characters in their names.

Happy reading


Expert Comment

ID: 6949863
Thanks! - Interresting comments. However, I've put DOS behind me now, though I knew it pretty well ten years ago...

Author Comment

ID: 6954174
Ok people.
I will try things first and then you'll get points.
And, if you have some more info for me, please, send.

Accepted Solution

rahat earned 300 total points
ID: 6954699
Why not directly try to manipulate ESCAPE SEQUENCE.
It should work mostly in all the DOT and LASER printers.

Here is how to do it in windows;EN-US;q96795

Want to get some more idea?

I has to acknowlege this code to (Randall Elton Ding)

>Printing text is simple... just open the appropriate LPT port and
>redirect text into it.

It runs also in Epson MX-100 (made in early 80's)
You should get some ideas from this program, it may even be capable
of being modified to work with your printer.
I don't know if the escape codes are the same, you'll have to
look them up.  BTW, this printer is a 9 pin and only 8 are used.
Thats convenient because each print head pass generates 8 pixils high
per character sent.  

program develop;  

uses graph;

  rotate90= true;
  widepaper= false;
  bgipath: string = 'e:\bp\bgi';

procedure initbgi;
    errcode,grdriver,grmode: integer;

    grdriver := Detect;
    errcode:= graphresult;
    if errcode <> grok then begin
      writeln('Graphics error: ',grapherrormsg (errcode));

procedure developgraph(rotate: boolean);
                            { if passed parameter is true, the graphics
                              image will be rotated 90 degrees to fit on
                              a narrow sheet of printer paper, if false
                              the image will completely fill the wide
                              paper erect and double height }

  const maxprinter = 816; { maximum width of printer }

    graphwidth,graphheight,printerwidth,printerheight: integer;
    n1,n2,sx,sy,x,y,y2,pixcolr: integer;
    widthratio,heightratio: real;
    blank: boolean;
    bitloc,bits: byte;
    bytes: array [1..maxprinter] of byte;
    lst: text;

    case rotate of
      widepaper: begin                       { develop erect on wide paper }
                   graphwidth:= getmaxx+1;
                   graphheight:= getmaxy+1;
                   printerwidth:= maxprinter;       { scale 1.275 x 2 }
                   printerheight:= graphheight*2;
      rotate90:  begin                     { if rotate then reverse x and y }
                   graphwidth:= getmaxy+1;
                   graphheight:= getmaxx+1;
                   printerwidth:= graphwidth;       { scale 1 x 1 }
                   printerheight:= graphheight;
    n2:= printerwidth div 256;
    n1:= printerwidth mod 256;
    write(lst,chr(27),'A',chr(8));   { set line spacing to 8 }
    widthratio:= printerwidth/graphwidth;
    heightratio:= printerheight/graphheight;
    y:= 0;
    while y < printerheight do begin
      blank:= true;    { remains true if entire printer pass is blank }
      for x:= 1 to printerwidth do begin
        sx:= trunc((x-1)/widthratio);  { screen x coorid }
        bits:= 0;
        bitloc:= $80;
        for y2:= y to y+7 do begin
          sy:= trunc(y2/heightratio);  { screen y coorid }
          if sy < graphheight then begin { last printer pass is incomplete }
            case rotate of
              widepaper: pixcolr:= getpixel(sx,sy);
              rotate90:  pixcolr:= getpixel(sy,sx);   { x and y swaped }
            if pixcolr > 0 then bits:= bits or bitloc;
          bitloc:= bitloc shr 1;
        case rotate of
          widepaper: bytes[x]:= bits;
          rotate90:  bytes[printerwidth-x+1]:= bits;  { reverse image }
        if bits > 0 then blank:= false; { have something to print this pass }
      if not blank then begin    { line feed if nothing to print this pass }
        write (lst,chr(27),'K',chr(n1),chr(n2));  { set printer graph mode }
        for x:= 1 to printerwidth do write (lst,chr(bytes[x]));
      writeln(lst);   { output 8 printer pixels high per pass }
      y:= y+8;
    write(lst,chr(12));       { top of form }
    write(lst,chr(27),'@');   { re-initalize printer }


  { your graphics code here }

  developgraph(rotate90);    { or use (widepaper) }

You may find details by searching ESCAPE SEQUENCE for Graphics mode....


Expert Comment

ID: 6954708
Oh! I am sorry the Sample code in Pascal!!!

Author Comment

ID: 6966648
it is ok.
I know pascal to.

Author Comment

ID: 6966761
People, where can I get list of Canon BJC250 escape sequences?

Expert Comment

ID: 7006119
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link 
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange

Featured Post

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question