Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

printing a bitmap

Posted on 2004-04-28
6
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have 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…
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

664 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