Solved

Printing in C

Posted on 2002-04-12
13
630 Views
Last Modified: 2013-12-03
I want to print image with C on printer.
So, how to do that(how to print pixel)?
0
Comment
Question by:darko_poljak
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:fremsley
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
command.

Hope it helps
 
0
 

Author Comment

by:darko_poljak
ID: 6942645
Ok. Thanks. And what about Windows?
I mean MSDOS?
0
 
LVL 2

Expert Comment

by:fremsley
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
0
 
LVL 2

Expert Comment

by:fremsley
ID: 6945258
Sorry, should be:

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

0
 
LVL 2

Expert Comment

by:obg
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).
0
 
LVL 2

Expert Comment

by:fremsley
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

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

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

Author Comment

by:darko_poljak
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.
Thanks
0
 

Accepted Solution

by:
rahat earned 100 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
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q96795

Want to get some more idea?

I has to acknowlege this code to randyd@alpha2.csd.uwm.edu (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;

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


procedure initbgi;
  var
    errcode,grdriver,grmode: integer;

  begin
    grdriver := Detect;
    initgraph(grdriver,grmode,bgipath);
    errcode:= graphresult;
    if errcode <> grok then begin
      writeln('Graphics error: ',grapherrormsg (errcode));
      halt(1);
    end;
  end;



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 }

  var
    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;

  begin
    assign(lst,'lpt1');
    rewrite(lst);
    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;
                 end;
      rotate90:  begin                     { if rotate then reverse x and y }
                   graphwidth:= getmaxy+1;
                   graphheight:= getmaxx+1;
                   printerwidth:= graphwidth;       { scale 1 x 1 }
                   printerheight:= graphheight;
                 end;
    end;
    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 }
            end;
            if pixcolr > 0 then bits:= bits or bitloc;
          end;
          bitloc:= bitloc shr 1;
        end;
        case rotate of
          widepaper: bytes[x]:= bits;
          rotate90:  bytes[printerwidth-x+1]:= bits;  { reverse image }
        end;
        if bits > 0 then blank:= false; { have something to print this pass }
      end;
      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]));
      end;
      writeln(lst);   { output 8 printer pixels high per pass }
      y:= y+8;
    end;
    write(lst,chr(12));       { top of form }
    write(lst,chr(27),'@');   { re-initalize printer }
    close(lst);
  end;


begin
  initbgi;

  { your graphics code here }
  Line(100,100,200,100);
  Line(200,100,200,100);
  Line(100,200,200,100);
  Line(100,100,200,200);
  SetColor(Blue);
  Circle(300,200,50);

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


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

0
 

Expert Comment

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

Author Comment

by:darko_poljak
ID: 6966648
rahat
it is ok.
I know pascal to.
0
 

Author Comment

by:darko_poljak
ID: 6966761
Ok.
People, where can I get list of Canon BJC250 escape sequences?
Thanks
0
 
LVL 1

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
http://www.experts-exchange.com/questions/Q.20007044.html
http://www.experts-exchange.com/questions/Q.20015616.html
http://www.experts-exchange.com/questions/Q.20015619.html
http://www.experts-exchange.com/questions/Q.20029847.html
http://www.experts-exchange.com/questions/Q.20063980.html
http://www.experts-exchange.com/questions/Q.20102759.html
http://www.experts-exchange.com/questions/Q.20096248.html
http://www.experts-exchange.com/questions/Q.20121061.html
http://www.experts-exchange.com/questions/Q.20125221.html
http://www.experts-exchange.com/questions/Q.20184986.html
http://www.experts-exchange.com/questions/Q.20187691.html
http://www.experts-exchange.com/questions/Q.20287863.html
http://www.experts-exchange.com/questions/Q.20293270.html
http://www.experts-exchange.com/questions/Q.20296417.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20010216.html
http://www.experts-exchange.com/questions/Q.20029843.html
http://www.experts-exchange.com/questions/Q.20121059.html

*****  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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
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.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
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.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now