Solved

printing a bitmap

Posted on 2004-04-28
6
331 Views
Last Modified: 2013-12-03
hi ,
i am trying to print a bitmap from a form.
it is in the right size on the form but when i try to print it,the printer print it very small,depends on the printer resulotion.
can any one send me an example of a bitmap print that print in the right size?
thanks Ariel
0
Comment
Question by:mars_r
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10936593
you could paint your bitmap driectly on the printercanvas.
with using the stretchdraw-method you could scale it like you need.

meikl ;-)
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10936597
change the printer resolution to the bitmap resolution
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10936654
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Printers, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Bitmap : TBitmap;
    Printer : TPrinter;
    Rect : TRect;
begin
  Bitmap := TBitmap.Create;
  Printer := TPrinter.Create;
  Printer.PrinterIndex := ListBox1.ItemIndex;;
  Printer.BeginDoc;
  Printer.NewPage;
  Bitmap.LoadFromFile('C:\WINDOWS\Blue Lace 16.bmp');
  Rect.Left := 0;
  Rect.Top := 0;
  Rect.Right := Bitmap.Width;
  Rect.Bottom := Bitmap.Height;
  Printer.Canvas.StretchDraw(Rect, Bitmap);
  Printer.EndDoc;
end;

procedure TForm1.Button2Click(Sender: TObject);
var tmp : TPrinter;
begin
  tmp := TPrinter.Create;
  ListBox1.Items := tmp.Printers;
end;

end.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10936753
I think that You should scale it.
Like Ivanov code, but something like this

 Rect.Right := Printer.PageWidth * Bitmap.Width div 800;
 Rect.Bottom := Printer.PageWidth * Bitmap.Height div 800;
 
0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 10937353
Hi,

uses Printers;

procedure PrintBitmap(Bitmap:  TBitmap;
                     TopOfPagePercent, LeftMarginPercent, PrintedImageWidthPercent: Integer);
var
   BitmapHeader      :  pBitmapInfo;
   BitmapImage       :  POINTER;
   HeaderSize        :  DWORD;
   ImageSize         :  DWORD;
   FromLeftMargin    :  INTEGER;
   PrintedImageWidth :  INTEGER;
   FromTopOfPage     :  INTEGER;
   PrintedImageHeight:  INTEGER;

begin
Printer.BeginDoc; // Uses printers
try
 FromTopOfPage      := MulDiv(Printer.PageHeight, TopOfPagePercent  ,100);
 FromLeftMargin     := MulDiv(Printer.PageWidth , LeftMarginPercent ,100);
 PrintedImageWidth  := MulDiv(Printer.PageWidth , PrintedImageWidthPercent ,100);
 // Maintain aspect ratio
 PrintedImageHeight := Bitmap.Height*PrintedImageWidth div Bitmap.Width;
 GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
 GetMem(BitmapHeader, HeaderSize);
 GetMem(BitmapImage,  ImageSize);
 try
   GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
   StretchDIBits(Printer.Canvas.Handle,
                 FromLeftMargin, FromTopOfPage,          // Destination Origin
                 PrintedImageWidth, PrintedImageHeight,  // Destination Width & Height
                 0, 0,                                   // Source Origin
                 Bitmap.Width, Bitmap.Height,            // Source Width & Height
                 BitmapImage,
                 TBitmapInfo(BitmapHeader^),
                 DIB_RGB_COLORS,
                 SRCCOPY)
 finally
   FreeMem(BitmapHeader);
   FreeMem(BitmapImage);
 end;
finally
   Printer.EndDoc;
end;
end;

// usage:

//place Image1, Button1 on the form

procedure TForm1.Button1Click(Sender: TObject);
begin
 Image1.Picture.LoadFromFile('C:\Program Files\Common Files\Borland Shared\Images\Splash\256Color\handshak.bmp');
 PrintBitmap(Image1.Picture.Bitmap, 1, 1, 80);
end;

Regards, Geo
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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