Solved

printing a bitmap

Posted on 2004-04-28
6
328 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
Comment Utility
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
Comment Utility
change the printer resolution to the bitmap resolution
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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