Solved

printing a bitmap

Posted on 2004-04-28
6
330 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now