본문 바로가기

카테고리 없음

[일반/컴포넌트] 투명한 Bitmaps 인쇄

반응형
Hello All!!

Any help for a programmer in the little woods of Rochester??

I can display several transparent bitmaps onto the screen canvas, but when
it comes to printing... It is a different story! I use Microsoft's
DrawTransparentBitmap routine to display the graphics onto the screen. And
I tried the same thing for printing and nothing comes out but black boxes.
I have taken care of the stretching and coloring before I get to the
routine.

Any ideas or suggestion would be greatly appreciated!

The Crew.


This is part of the routine to get it ready to print...

  theBMP := TBitmap.Create ;
  theStretch := TBitmap.Create ;
  theBMP.Width := Round((PictItem^.Width)*widthDiff) ;
  theBMP.Height := Round((PictItem^.Height)*heightDiff) ;
  try
    theStretch.LoadFromFile(PictPath) ;

    {*** This puts in the palette to keep high-resolution bitmaps clear ***}
    SelectPalette(theBMP.Canvas.Handle, frmCalMain.imgLogo.Picture.Bitmap.Palette, True) ;
    SetStretchBltMode(theBMP.Canvas.Handle, STRETCH_DELETESCANS) ;

    {*** First stretch before displaying picture (the work-around for Win95) ***}
    StretchBlt(theBMP.Canvas.Handle, 0, 0, Round((PictItem^.Width)*widthDiff),
               Round((PictItem^.Height)*heightDiff), theStretch.Canvas.Handle, 0, 0,
               theStretch.Width, theStretch.Height, SRCCOPY);


    {*** Now draw the transparent picture onto the printer ***}
    DrawTransparentBitmap(Printer.Canvas, theBMP,
                          Round((PosX-borderPosL)*widthDiff), Round((PosY-borderPosT)*heightDiff), clOlive) ;
   finally
     theBMP.Free ;
     theStretch.Free ;
   end ;

If I do the above onto the screen canvas, no problem, but to the printer....
Sorry, no cigar!


Here is the routine to draw transparent bitmaps:

procedure DrawTransparentBitmap(Canvas : TCanvas ; BMP : TBitmap ; xStart,
yStart : Integer ;
                                cTransparentColor : LongInt) ;
var
  bm : WinTypes.TBitmap ;
  cColor : TColorRef ;
  bmAndBack,
  bmAndObject,
  bmAndMem,
  bmSave,
  oldBmp : HBITMAP ;
  bmBackOld,
  bmObjectOld,
  bmMemOld,
  bmSaveOld,
  hBmp : HBITMAP ;
  hdcMem,
  hdcBack,
  hdcObject,
  hdcTemp,
  hdcSave,
  dc : HDC ;
  ptSize : WinTypes.TPoint ;
  temp_bitmap : TBitmap ;
begin
  temp_bitmap := TBitmap.Create;
  temp_bitmap.Assign(bmp);
  try
    dc := Canvas.Handle;
    hBmp := Temp_Bitmap.Handle;
    hdcTemp := CreateCompatibleDC(dc);
    oldBmp := SelectObject(hdcTemp, hBmp);

    GetObject(hBmp, SizeOf(bm), @bm);

    ptSize.x := bm.bmWidth ;
    ptSize.y := bm.bmHeight ;

    hdcBack := CreateCompatibleDC(dc);
    hdcObject := CreateCompatibleDC(dc);
    hdcMem := CreateCompatibleDC(dc);
    hdcSave := CreateCompatibleDC(dc);

    bmAndBack := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
    bmAndObject := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
    bmAndMem := CreateCompatibleBitmap(dc, ptSize.x, ptSize.y);
    bmSave := CreateCompatibleBitmap(dc, ptSize.x, ptSize.y);

    bmBackOld := SelectObject(hdcBack, bmAndBack);
    bmObjectOld := SelectObject(hdcObject, bmAndObject);
    bmMemOld := SelectObject(hdcMem, bmAndMem);
    bmSaveOld := SelectObject(hdcSave, bmSave);

    SetMapMode(hdcTemp, GetMapMode(dc));

    BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);

    cColor := SetBkColor(hdcTemp, cTransparentColor);

    BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);

    SetBkColor(hdcTemp, cColor);

    BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);
    BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, dc, xStart, yStart, SRCCOPY);
    BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
    BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
    BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
    BitBlt(dc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
    BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

    DeleteObject(SelectObject(hdcBack, bmBackOld));
    DeleteObject(SelectObject(hdcObject, bmObjectOld));
    DeleteObject(SelectObject(hdcMem, bmMemOld));
    DeleteObject(SelectObject(hdcSave, bmSaveOld));

    SelectObject(hdcTemp, oldBmp);

    DeleteDC(hdcMem);
    DeleteDC(hdcBack);
    DeleteDC(hdcObject);
    DeleteDC(hdcSave);
    DeleteDC(hdcTemp);
  finally
    Temp_Bitmap.Free;
  end ;
end;



-----------------------------------------------------------------------------
Re: Printing differences between NT and Win95???

Actually we did find the error i TPrinter, and have resolved it into this
wrapper function, it works, but the transparency problem still persists in
this version. Do anyone have any suggestions?

procedure THSPrinter.PrintBitmap(Bitmap: Graphics.TBitmap; X, Y, pWidth, pHeight: Integer);
var
  Info     : PBitmapInfo;
  InfoSize : Integer;
  Image    : Pointer;
  {$IFDEF WIN32}
  ImageSize: DWord;
  {$ELSE}
  ImageSize: LongInt;
  {$ENDIF}
  R: Real;
  PrinterHeight, PrinterWidth: Integer;

  begin
    if (pWidth<1) or (pHeight<1) then
    begin
      pWidth:= Bitmap.Width;
      pHeight:= Bitmap.Height;
    end;
    with Bitmap do
    begin
      GetDIBSizes (Handle,InfoSize,ImageSize);
      Info:= MemAlloc(InfoSize);
      try
        Image:= GlobalAllocPtr(HeapAllocFlags,ImageSize);
        try
          GetDIB(Handle,Palette,Info^,Image^);
          R:= Info^.bmiHeader.biWidth;
          R:= R/Info^.bmiHeader.biHeight;
          PrinterWidth:= Trunc (pHeight*R);
          if PrinterWidth>pWidth then
          begin
            PrinterWidth:= pWidth;
            PrinterHeight:= Trunc (PrinterWidth/R);
          end
          else
            PrinterHeight:= pHeight;
          with Info^.bmiHeader do
            StretchDIBits (Printer.Canvas.Handle,(pWidth-PrinterWidth) DIV 2,
                          (pHeight-PrinterHeight) DIV 2,PrinterWidth,PrinterHeight,
                           X,Y,X+biWidth,Y+biHeight,Image,Info^,DIB_RGB_COLORS,SRCCOPY);
        finally
          GlobalFreePtr(Image);
        end;
      finally
        FreeMem (Info,InfoSize);
      end;
    end;
  end;


Davie Reed wrote:

> I have been told that this is an old issue. It seems that the TPrinter
> module in Delphi has PROBLEMS in it. And that you can NOT relaibly use the
> DRAW or STRETCHDRAW :(
>
> So, instead you can use the StretchDiBits funtion to draw/stretch to the
> printer canvas's DC.
>
> Davie
>
> Stig Bircherod Calundan wrote:
>
> > We have a problem when we print bitmaps on Win95 + 98, but not on NT4,
> > the problem consists of the white part of a bitmap, which NT interprets
> > as transparent (thas ok), but on Win95 the white part is painted as a
> > gray area. The intensity of the graying is different on different
> > printers, though color printers does interpret the white area correctly.
> >
> > The bitmaps is loaded into a TBitmap using abitmap.handle:= loadbitmap
> > (...), and is later drawn to the printers canvas, with the canvas.draw
> > command.
> >
> > The printer used on both Win95 and NT is a HP LaserJet 5Si/5Si MX PS
> > with the latest printer drivers...
> >
> > Please help....
> >
> > Stig
반응형