Delphi
StretchDIBits - printing 256 color images
by Steven Dente
.
 
Franz
Glaser
      Subject:  Re: StretchDIBits (256 colors)
         Date:  30 Jun 1998 07:28:12 GMT
         From:  "sdente1" <sdente1@san.rr.com> Steven Dente
 Organization:  Time Warner
   Newsgroups:  borland.public.delphi.graphics

note: question is below

Wiktor,
  I had similar problems with 256 color images when trying to print.  A
canvas is a canvas so maybe my solution will work for you.  There is a
difference between 16 and 32 bit in variable types so i have included
compiler directives for each.  Following is my code to print 256 color to
the printer.canvas. You could modify it to also pass the source bitmap and
destination canvas. 

// pass in left,top,width,height of the image you want to print.
procedure TSomeForm.PrintBitmap(X,Y,pWidth,pHeight:integer);
var
   Info: PBitmapInfo;
   InfoSize: integer;
   Image: Pointer;
{$IFDEF DEL32}
   ImageSize: DWord;
{$ELSE}
   ImageSize: Longint;
{$ENDIF}
begin
  // if passed parameters don't make sense get the whole image
  if (pWidth<1) or (pHeight <1) then 
    begin
      pWidth := YourImageControl.Picture.Bitmap.Width;
      pHeight:= YourImageControl.Picture.Bitmap.Height;
    end;

  with YourImageControl.Picture.Bitmap do 
    begin
      GetDibSizes(Handle,InfoSize,ImageSize);
      Info:= MemAlloc(InfoSize);
      Try
        Image:= GlobalAllocPtr(HeapAllocFlags,ImageSize);
        Try
          GetDib(Handle,Palette,Info^,Image^);
          with Info^.bmiHeader do
            StretchDlBits(Printer.Canvas.Handle,
                          X,Y,pWidth,pHeight,
                          0,0,biWidth,biHeight,
                          Image,Info^,
                          DIB_RGB_COLORS,SRCCOPY);
        finally
          GlobalFreePtr(Image);
        end;
      finally
        FreeMem(Info,InfoSize);
      end;
    end;
end;// PrintBitmap

Again, this was very specific to my app.  I was always using the same image control and always printing to the printer.  The printer had been previously initialized with a call to Printer.BeginDoc so I was sure the canvas was valid.  You may want to change all of this to be more generic.  The app using this function has thousands of users with a wide variety of video cards, resolutions and printers.  The function never fails to print great color pictures.

Hope this helps, let me know if you have questions.
Steven Dente
sdente1@san.rr.com



Wiktor Sadowski <malibu@perytnet.pl> wrote in article

> I have problem with StretchDIBits (256 colors).

> My problem:
>
> var
> BitmapInfo:pbitmapinfo;{with color table defined}
> bits:pointer;{indexes defining the image} 

> begin
> StretchDIBits(SomeCanvas.Handle,
>                        0,0,Width,Height,
>                        0,0,Width,Height,
>                        Bits,BitmapInfo^,
>                        DIB_RGB_COLORS,SRCCOPY);
> end;

> Everything works fine in truecolor mode,but in 256 mode
> colors look awful.
> I was trying to use SelectPalette(Somecanvas.handle,
>                                   PaletteFromColorTable,
>                                   False);
>                     RealizePalette(Somecanvas.handle);

> but without success.
> Please help.

> Kind regards, Wiktor Sadowski