본문 바로가기
delphi

델파이에서 비트맵 인쇄하기

by doran365 2024. 8. 2.

델파이 프로그래밍에서 비트맵 이미지를 인쇄하는 방법은 다양합니다. 본 글에서는 가장 많이 사용되는 세 가지 방법을 상세히 설명합니다. 이 글을 통해 독자들은 비트맵 인쇄의 기본 개념부터 고급 기술까지 이해할 수 있습니다.

1. 기본적인 비트맵 인쇄 방법

델파이에서 비트맵을 인쇄하는 가장 기본적인 방법은 Printer 객체를 사용하는 것입니다. 이 방법은 간단한 인쇄 작업에 적합합니다.

절차

  1. Printer 객체를 초기화합니다.
  2. 인쇄 작업을 시작합니다.
  3. 비트맵 이미지를 스케일링하여 출력합니다.
  4. 인쇄 작업을 종료합니다.

코드 예제

 
uses
  Printers;

procedure TForm1.Button1Click(Sender: TObject);
var
  ScaleX, ScaleY: Integer;
  RR: TRect;
begin
  with Printer do
  begin
    BeginDoc;
    try
      ScaleX := GetDeviceCaps(Handle, logPixelsX) div PixelsPerInch;
      ScaleY := GetDeviceCaps(Handle, logPixelsY) div PixelsPerInch;
      RR := Rect(0, 0, Image1.Picture.Width * ScaleX, Image1.Picture.Height * ScaleY);
      Canvas.StretchDraw(RR, Image1.Picture.Graphic);
    finally
      EndDoc;
    end;
  end;
end;

2. 고급 비트맵 인쇄 방법

더 정교한 인쇄가 필요한 경우, 비트맵 정보를 직접 처리하여 인쇄할 수 있습니다. 이는 GetDIB와 StretchDIBits 함수를 사용하여 구현할 수 있습니다.

절차

  1. 비트맵의 크기 정보를 가져옵니다.
  2. 비트맵 정보를 메모리에 저장합니다.
  3. 인쇄할 비트맵을 지정된 사각형 영역에 맞춰 스케일링하여 출력합니다.

코드 예제

 
procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
var
  BitmapHeader: pBitmapInfo;
  BitmapImage: Pointer;
  HeaderSize: DWORD;
  ImageSize: DWORD;
begin
  GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
  GetMem(BitmapHeader, HeaderSize);
  GetMem(BitmapImage, ImageSize);
  try
    GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
    StretchDIBits(Canvas.Handle,
      DestRect.Left, DestRect.Top,    
      DestRect.Right - DestRect.Left, 
      DestRect.Bottom - DestRect.Top, 
      0, 0,                           
      Bitmap.Width, Bitmap.Height,    
      BitmapImage,
      TBitmapInfo(BitmapHeader^),
      DIB_RGB_COLORS,
      SRCCOPY);
  finally
    FreeMem(BitmapHeader);
    FreeMem(BitmapImage);
  end;
end;

3. 자동 크기 조정 및 인쇄 방법

이미지의 크기를 자동으로 조정하여 페이지에 맞게 인쇄하는 방법도 있습니다. 이는 비율에 따라 이미지를 조정하여 인쇄하는 방식입니다.

절차

  1. 이미지의 비율을 계산합니다.
  2. 인쇄 페이지 크기에 맞춰 이미지를 스케일링합니다.
  3. 조정된 이미지를 인쇄합니다.

코드 예제

 
uses
  printers;

procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap: TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize: DWORD;
  BitsSize: DWORD;
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
  Header := AllocMem(HeaderSize);
  Bits := AllocMem(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
      DestRect.Right, DestRect.Bottom,
      0, 0, ABitmap.Width, ABitmap.Height, Bits, TBitmapInfo(Header^),
      DIB_RGB_COLORS, SRCCOPY);
  finally
    FreeMem(Header, HeaderSize);
    FreeMem(Bits, BitsSize);
  end;
end;

procedure PrintImage(Image: TImage; ZoomPercent: Integer);
var 
  relHeight, relWidth: integer;
begin
  Screen.Cursor := crHourglass;
  Printer.BeginDoc;
  with Image.Picture.Bitmap do 
  begin
    if ((Width / Height) > (Printer.PageWidth / Printer.PageHeight)) then
    begin
      relWidth := Printer.PageWidth;
      relHeight := MulDiv(Height, Printer.PageWidth, Width);
    end 
    else
    begin
      relWidth  := MulDiv(Width, Printer.PageHeight, Height);
      relHeight := Printer.PageHeight;
    end;
    relWidth := Round(relWidth * ZoomPercent / 100);
    relHeight := Round(relHeight * ZoomPercent / 100);
    DrawImage(Printer.Canvas, Rect(0, 0, relWidth, relHeight), Image.Picture.Bitmap);
  end;
  Printer.EndDoc;
  Screen.cursor := crDefault;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintImage(Image1, 40);
end;

결론

델파이에서 비트맵 이미지를 인쇄하는 방법은 다양합니다. 기본적인 방법부터 고급 기술, 자동 크기 조정 방법까지 여러 가지를 살펴보았습니다. 이 글이 독자들에게 유용한 가이드가 되기를 바랍니다. 더 자세한 정보나 추가적인 질문이 있다면 언제든지 문의해 주세요.