Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

printing a bitmap

Posted on 2004-04-28
6
Medium Priority
?
340 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 500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

963 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