markymsofth
Programmer
any help making this work with bcb6
taken from here
void __fastcall Tmain_form:rint_image_btnClick(TObject *Sender)
{
TPrinter *p = Printer();
// create a memory dc for the image
HDC h_dc = image->Picture->Bitmap->Canvas->Handle;
int bmp_w = image->Picture->Bitmap->Width,
bmp_h = image->Picture->Bitmap->Height;
HDC h_mem_dc = ::CreateCompatibleDC (h_dc);
HBITMAP h_mem_bmp = ::CreateCompatibleBitmap (h_dc, bmp_w, bmp_h);
HBITMAP h_old_bmp = ::SelectObject (h_mem_dc, h_mem_bmp);
// fix up bad video drivers
bool is_pal_dev = false;
LOGPALETTE *pal;
HPALETTE h_pal, h_old_pal;
if :GetDeviceCaps (image->Canvas->Handle, RASTERCAPS) & RC_PALETTE)
{
pal = static_cast(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
pal->palVersion = 0x300;
pal->palNumEntries = ::GetSystemPaletteEntries(image->Canvas->Handle, 0, 256, pal->palPalEntry);
if (pal->palNumEntries != 0)
{
h_pal = ::CreatePalette (pal);
h_old_pal = ::SelectPalette (h_mem_dc, h_pal, false);
is_pal_dev = true;
}
else
{
free (pal);
}
}
// copy the image on to the memory dc
::BitBlt (h_mem_dc, 0, 0, bmp_w, bmp_h, h_dc, 0, 0, SRCCOPY);
if (is_pal_dev)
{
::SelectPalette (h_mem_dc, h_old_pal, false);
:eleteObject (h_pal);
}
// delete the mem dc
::SelectObject (h_mem_dc, h_old_bmp);
:eleteDC (h_mem_dc);
// get memory for a BITMAPIFO Structure
HANDLE h_bmp_info = ::GlobalAlloc (GHND, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 256));
BITMAPINFO* bmp_info = static_cast:GlobalLock (h_bmp_info));
//Set up the structure
memset (bmp_info, NULL, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 255));
bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp_info->bmiHeader.biPlanes = 1;
bmp_info->bmiHeader.biBitCount = 8;
bmp_info->bmiHeader.biWidth = bmp_w;
bmp_info->bmiHeader.biHeight = bmp_h;
bmp_info->bmiHeader.biCompression = BI_RGB;
// find out how much memory for the bits
::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, NULL, bmp_info, DIB_RGB_COLORS);
// Allocate memory for the bits
HANDLE h_bits = GlobalAlloc (GHND, bmp_info->bmiHeader.biSizeImage);
void *bits = ::GlobalLock (h_bits);
// this time get the bits
::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, bits, bmp_info, DIB_RGB_COLORS);
// fix up for bad video driver
if (is_pal_dev)
{
for (int i = 0; i < pal->palNumEntries; i++)
{
bmp_info->bmiColors.rgbRed = pal->palPalEntry.peRed;
bmp_info->bmiColors.rgbGreen = pal->palPalEntry.peGreen;
bmp_info->bmiColors.rgbBlue = pal->palPalEntry.peBlue;
}
free (pal);
}
// begin the printing
p->BeginDoc ();
// scale print size
int scale_x, scale_y;
if (p->PageWidth < p->PageHeight)
{
scale_x = p->PageWidth;
scale_y = image->Picture->Height * (p->PageWidth / bmp_w);
}
else
{
scale_x = image->Picture->Width * (p->PageHeight / bmp_h);
scale_y = p->PageHeight;
}
// fix up for print with palette
is_pal_dev = false;
if :GetDeviceCaps (h_dc, RASTERCAPS) & RC_PALETTE)
{
pal = static_cast(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
pal->palVersion = 0x300;
pal->palNumEntries = 256;
for (int i = 0; pal->palNumEntries; i++)
{
pal->palPalEntry.peRed = bmp_info->bmiColors.rgbRed;
pal->palPalEntry.peGreen = bmp_info->bmiColors.rgbGreen;
pal->palPalEntry.peBlue = bmp_info->bmiColors.rgbBlue;
}
h_pal = CreatePalette(pal);
free (pal);
h_old_pal = SelectPalette(p->Canvas->Handle, h_pal, false);
is_pal_dev = true;
}
// send the bits to the printer
StretchDIBits(p->Canvas->Handle, 0, 0, scale_x, scale_y,
0, 0, bmp_w, bmp_h, bits,bmp_info, DIB_RGB_COLORS, SRCCOPY);
// end the print
p->EndDoc ();
// clean up
:eleteObject (h_mem_bmp);
if (is_pal_dev)
{
::SelectObject (p->Canvas->Handle, h_old_pal);
:eleteObject (h_pal);
}
::GlobalUnlock (bits);
::GlobalFree (h_bits);
::GlobalUnlock (bmp_info);
::GlobalFree (h_bmp_info);
}
taken from here
void __fastcall Tmain_form:rint_image_btnClick(TObject *Sender)
{
TPrinter *p = Printer();
// create a memory dc for the image
HDC h_dc = image->Picture->Bitmap->Canvas->Handle;
int bmp_w = image->Picture->Bitmap->Width,
bmp_h = image->Picture->Bitmap->Height;
HDC h_mem_dc = ::CreateCompatibleDC (h_dc);
HBITMAP h_mem_bmp = ::CreateCompatibleBitmap (h_dc, bmp_w, bmp_h);
HBITMAP h_old_bmp = ::SelectObject (h_mem_dc, h_mem_bmp);
// fix up bad video drivers
bool is_pal_dev = false;
LOGPALETTE *pal;
HPALETTE h_pal, h_old_pal;
if :GetDeviceCaps (image->Canvas->Handle, RASTERCAPS) & RC_PALETTE)
{
pal = static_cast(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
pal->palVersion = 0x300;
pal->palNumEntries = ::GetSystemPaletteEntries(image->Canvas->Handle, 0, 256, pal->palPalEntry);
if (pal->palNumEntries != 0)
{
h_pal = ::CreatePalette (pal);
h_old_pal = ::SelectPalette (h_mem_dc, h_pal, false);
is_pal_dev = true;
}
else
{
free (pal);
}
}
// copy the image on to the memory dc
::BitBlt (h_mem_dc, 0, 0, bmp_w, bmp_h, h_dc, 0, 0, SRCCOPY);
if (is_pal_dev)
{
::SelectPalette (h_mem_dc, h_old_pal, false);
:eleteObject (h_pal);
}
// delete the mem dc
::SelectObject (h_mem_dc, h_old_bmp);
:eleteDC (h_mem_dc);
// get memory for a BITMAPIFO Structure
HANDLE h_bmp_info = ::GlobalAlloc (GHND, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 256));
BITMAPINFO* bmp_info = static_cast:GlobalLock (h_bmp_info));
//Set up the structure
memset (bmp_info, NULL, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 255));
bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp_info->bmiHeader.biPlanes = 1;
bmp_info->bmiHeader.biBitCount = 8;
bmp_info->bmiHeader.biWidth = bmp_w;
bmp_info->bmiHeader.biHeight = bmp_h;
bmp_info->bmiHeader.biCompression = BI_RGB;
// find out how much memory for the bits
::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, NULL, bmp_info, DIB_RGB_COLORS);
// Allocate memory for the bits
HANDLE h_bits = GlobalAlloc (GHND, bmp_info->bmiHeader.biSizeImage);
void *bits = ::GlobalLock (h_bits);
// this time get the bits
::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, bits, bmp_info, DIB_RGB_COLORS);
// fix up for bad video driver
if (is_pal_dev)
{
for (int i = 0; i < pal->palNumEntries; i++)
{
bmp_info->bmiColors.rgbRed = pal->palPalEntry.peRed;
bmp_info->bmiColors.rgbGreen = pal->palPalEntry.peGreen;
bmp_info->bmiColors.rgbBlue = pal->palPalEntry.peBlue;
}
free (pal);
}
// begin the printing
p->BeginDoc ();
// scale print size
int scale_x, scale_y;
if (p->PageWidth < p->PageHeight)
{
scale_x = p->PageWidth;
scale_y = image->Picture->Height * (p->PageWidth / bmp_w);
}
else
{
scale_x = image->Picture->Width * (p->PageHeight / bmp_h);
scale_y = p->PageHeight;
}
// fix up for print with palette
is_pal_dev = false;
if :GetDeviceCaps (h_dc, RASTERCAPS) & RC_PALETTE)
{
pal = static_cast(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
pal->palVersion = 0x300;
pal->palNumEntries = 256;
for (int i = 0; pal->palNumEntries; i++)
{
pal->palPalEntry.peRed = bmp_info->bmiColors.rgbRed;
pal->palPalEntry.peGreen = bmp_info->bmiColors.rgbGreen;
pal->palPalEntry.peBlue = bmp_info->bmiColors.rgbBlue;
}
h_pal = CreatePalette(pal);
free (pal);
h_old_pal = SelectPalette(p->Canvas->Handle, h_pal, false);
is_pal_dev = true;
}
// send the bits to the printer
StretchDIBits(p->Canvas->Handle, 0, 0, scale_x, scale_y,
0, 0, bmp_w, bmp_h, bits,bmp_info, DIB_RGB_COLORS, SRCCOPY);
// end the print
p->EndDoc ();
// clean up
:eleteObject (h_mem_bmp);
if (is_pal_dev)
{
::SelectObject (p->Canvas->Handle, h_old_pal);
:eleteObject (h_pal);
}
::GlobalUnlock (bits);
::GlobalFree (h_bits);
::GlobalUnlock (bmp_info);
::GlobalFree (h_bmp_info);
}