Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing TImage.Canvas using Delphi 6

Status
Not open for further replies.

dhgillis

Programmer
Dec 12, 2002
1
GB
I have had problems trying to print TImage.Canvas using Delphi 6. The way I have got round this is by transferring eachg pixel one at a time to TPrinter.Canvas (as below). This does not seem very elegant. Is there a better way?

{This assumes that the image, when printed, is smaller than the page}

procedure TForm1.PrintImageClick(Sender: TObject);
var
x,y,Counter,ScaleFactorInxDirection,ScaleFactorInyDirection,ScaleFactor: integer;
begin
If (Printer.PageWidth Div Image1.Width) = (Printer.PageWidth / Image1.Width)
Then ScaleFactorInxDirection := Printer.PageWidth Div Image1.Width - 1
Else ScaleFactorInxDirection := Printer.PageWidth Div Image1.Width;
If (Printer.PageHeight Div Image1.Height) = (Printer.PageHeight / Image1.Height)
Then ScaleFactorInxDirection := Printer.PageHeight Div Image1.Height - 1
Else ScaleFactorInxDirection := Printer.PageHeight Div Image1.Height;
If ScaleFactorInxDirection < ScaleFactorInyDirection
Then ScaleFactor := ScaleFactorInxDirection
Else ScaleFactor := ScaleFactorInyDirection;
Printer.BeginDoc;
For x := 1 To Image1.Width
Do
For y := 1 To Image1.Height
Do
For Counter := Printer.PageWidth - Image1.Width To Printer.PageWidth - Image1.Width + m
Do Printer.Canvas.Pixels[Counter + ScaleFactor * x,Counter + ScaleFactor * y] := Graph1.Canvas.Pixels[x,y];
Printer.EndDoc;
end;
 
Can you use something like this?
Printer.Canvas.StretchDraw(AImage.Picture.Bitmap); or
Printer.Canvas.Draw(0, 0, Image1.Picture.Graphic);


By the way, people smarter than me will tell you not to do this, as it will not work on some printers. This was on the borland newsgroups:

//---------------------------------------
procedure PrintBitmap( R: TRect; BM: TBitmap );
var
BitmapHeader: pBitmapInfo;
BitmapImage: Pointer;
HeaderSize, ImageSize: DWord;
begin
GetDIBSizes( BM.Handle, HeaderSize, ImageSize );
// ********** WORK-AROUND FOR WIN95 MIS-REPORTING OF REQUIRED HEADERSIZE *** 4K Should
always be big enough ***
HeaderSize:=4096;
GetMem( BitmapHeader, HeaderSize );
try
GetMem( BitmapImage, ImageSize );
try
GetDIB( BM.Handle, BM.Palette, BitmapHeader^, BitmapImage^ );
StretchDIBits( Printer.Canvas.Handle,
R.Left, R.Top, // Destination Origin
R.Right - R.Left, // Destination Width
R.Bottom - R.Top, // Destination Height
0, 0, // Source Origin
BM.Width, BM.Height, // Source Width & Height
BitmapImage,
TBitmapInfo( BitmapHeader^ ),
DIB_RGB_COLORS,
srcCopy )
finally
FreeMem( BitmapImage, ImageSize );
end;
finally
FreeMem( BitmapHeader, HeaderSize );
end;
end;


procedure PrintImage( R: TRect; I: TImage );
begin
PrintBitmap( R, I.Picture.Bitmap );
end;
//---------------------------------------

And this guy will tell you (and apparently he knows what he's talking about) that even this kind of stuff is unreliable if you are distributing your application.

&quot;The difference between practice and theory is that in theory, there is no difference between practice and theory&quot; - Somebody's tag line I stole
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top