W
wjp888
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的过程能在TReportCover画布上建立一副BMP图,那位高人能指点一下怎样才能把BMP图白色透明。(即:白色不显示,或白色不能挡住TReportCover画布上的表格线和文字,就像在TReportCover画布上盖了个公章一样)。
procedure TReportCover.BltTBitmapAsDib(DestDc: HDC; X, Y, Width, Height: Word; Bmp: TBitmap); {the TBitmap to Blt}
var
OriginalWidth: LongInt; // Bmp的宽度
DC: HDC; // 显示的环境DC
IsPaletteDevice: Bool; // 设备是否用到调色板
IsDestPaletteDevice: Bool;// 设备是否用到调色板s
BitmapInfoSize: Integer; // Bitmap的头信息长度
lpBitmapInfo: PBitmapInfo;// Bitmap的头信息
hBm: hBitmap; // Bitmap的句柄
hPal: hPalette; // 调色板的句柄
OldPal: hPalette; // 临时调色板
hBits: THandle; // DIB bits的句柄
pBits: pointer; // DIB bits的内存地址
lPPalEntriesArray: PPalEntriesArray;
NumPalEntries: Integer; // 调色板数目
I: Integer;
begin
Bmp.Transparent:=true; //我加的无任何作用
Bmp.TransparentColor:=clwhite;//我加的无任何作用
OldPal := 0;
OriginalWidth := Bmp.Width;
DC := GetDc(0);
IsPaletteDevice := GetDeviceCaps(DC, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
ReleaseDc(0, DC);
if IsPaletteDevice then
BitmapInfoSize := SizeOf(TBitmapInfo) + (SizeOf(TRGBQUAD) * 255)
else
BitmapInfoSize := SizeOf(TBitmapInfo);
GetMem(lpBitmapInfo, BitmapInfoSize);
FillChar(lpBitmapInfo^, BitmapInfoSize, #0);
lpBitmapInfo^.bmiHeader.biSize := SizeOf(TBitmapInfoHeader);
lpBitmapInfo^.bmiHeader.biWidth := OriginalWidth;
lpBitmapInfo^.bmiHeader.biHeight := Bmp.Height;
lpBitmapInfo^.bmiHeader.biPlanes := 1;
if IsPaletteDevice then
lpBitmapInfo^.bmiHeader.biBitCount := 8
else
lpBitmapInfo^.bmiHeader.biBitCount := 24;
lpBitmapInfo^.bmiHeader.biCompression := BI_RGB;
lpBitmapInfo^.bmiHeader.biSizeImage := 0;
lpBitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
lpBitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
if IsPaletteDevice then
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 256;
lpBitmapInfo^.bmiHeader.biClrImportant := 256;
end
else
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 0;
lpBitmapInfo^.bmiHeader.biClrImportant := 0;
end;
hBm := Bmp.ReleaseHandle;
hPal := Bmp.ReleasePalette;
DC := GetDc(0);
if IsPaletteDevice then
begin
OldPal := SelectPalette(DC, hPal, True);
RealizePalette(DC);
end;
GetDiBits(DC, hBm, 0, lpBitmapInfo^.bmiHeader.biHeight, nil,
TBitmapInfo(lpBitmapInfo^), DIB_RGB_COLORS);
hBits := GlobalAlloc(GMEM_MOVEABLE, lpBitmapInfo^.bmiHeader.biSizeImage);
pBits := GlobalLock(hBits);
GetDiBits(DC, hBm, 0,lpBitmapInfo^.bmiHeader.biHeight, pBits,
TBitmapInfo(lpBitmapInfo^), DIB_RGB_COLORS);
if IsPaletteDevice then
begin
GetMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
//NumPalEntries := GetPaletteEntries(hPal, 0, 256, lPPalEntriesArray^);
NumPalEntries := GetSystemPaletteEntries(DC, 0, 256, lPPalEntriesArray^);
for i := 0 to (NumPalEntries - 1) do
begin
lpBitmapInfo^.bmiColors.rgbRed := lPPalEntriesArray^.peRed;
lpBitmapInfo^.bmiColors.rgbGreen := lPPalEntriesArray^.peGreen;
lpBitmapInfo^.bmiColors.rgbBlue := lPPalEntriesArray^.peBlue;
end;
FreeMem(lPPalEntriesArray, SizeOf(TPaletteEntry) * 256);
end;
if IsPaletteDevice then
begin
SelectPalette(DC, OldPal, TRUE);
RealizePalette(DC);
end;
ReleaseDc(0, DC);
IsDestPaletteDevice := GetDeviceCaps(DestDc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
if IsPaletteDevice then
begin
OldPal := SelectPalette(DestDc, hPal, TRUE);
RealizePalette(DestDc);
end;
{Do the Blt}
StretchDiBits(DestDc, X, Y, Width, Height, 0, 0, OriginalWidth,
lpBitmapInfo^.bmiHeader.biHeight, pBits, lpBitmapInfo^, DIB_RGB_COLORS, SrcCopy);
if IsDestPaletteDevice then
begin
SelectPalette(DestDc, OldPal, TRUE);
RealizePalette(DestDc);
end;
//=====================我加的无任何作用
// Bmp.Modified:=true;
// Bmp.Transparent:=true;
// Bmp.TransparentColor:=clwhite;
// Bmp.Canvas.Draw(0,0,Bmp);
// Bmp.Transparent:= true;
// TransparentBlt(hBm, 0, 0, Bmp.Width, BmP.Height,
// Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, RGB($FF, $FF, $FF));
//======================
GlobalUnLock(hBits);
GlobalFree(hBits);
FreeMem(lpBitmapInfo, BitmapInfoSize);
Bmp.Handle := hBm;
Bmp.Palette := hPal;
end;
procedure TReportCover.BltTBitmapAsDib(DestDc: HDC; X, Y, Width, Height: Word; Bmp: TBitmap); {the TBitmap to Blt}
var
OriginalWidth: LongInt; // Bmp的宽度
DC: HDC; // 显示的环境DC
IsPaletteDevice: Bool; // 设备是否用到调色板
IsDestPaletteDevice: Bool;// 设备是否用到调色板s
BitmapInfoSize: Integer; // Bitmap的头信息长度
lpBitmapInfo: PBitmapInfo;// Bitmap的头信息
hBm: hBitmap; // Bitmap的句柄
hPal: hPalette; // 调色板的句柄
OldPal: hPalette; // 临时调色板
hBits: THandle; // DIB bits的句柄
pBits: pointer; // DIB bits的内存地址
lPPalEntriesArray: PPalEntriesArray;
NumPalEntries: Integer; // 调色板数目
I: Integer;
begin
Bmp.Transparent:=true; //我加的无任何作用
Bmp.TransparentColor:=clwhite;//我加的无任何作用
OldPal := 0;
OriginalWidth := Bmp.Width;
DC := GetDc(0);
IsPaletteDevice := GetDeviceCaps(DC, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
ReleaseDc(0, DC);
if IsPaletteDevice then
BitmapInfoSize := SizeOf(TBitmapInfo) + (SizeOf(TRGBQUAD) * 255)
else
BitmapInfoSize := SizeOf(TBitmapInfo);
GetMem(lpBitmapInfo, BitmapInfoSize);
FillChar(lpBitmapInfo^, BitmapInfoSize, #0);
lpBitmapInfo^.bmiHeader.biSize := SizeOf(TBitmapInfoHeader);
lpBitmapInfo^.bmiHeader.biWidth := OriginalWidth;
lpBitmapInfo^.bmiHeader.biHeight := Bmp.Height;
lpBitmapInfo^.bmiHeader.biPlanes := 1;
if IsPaletteDevice then
lpBitmapInfo^.bmiHeader.biBitCount := 8
else
lpBitmapInfo^.bmiHeader.biBitCount := 24;
lpBitmapInfo^.bmiHeader.biCompression := BI_RGB;
lpBitmapInfo^.bmiHeader.biSizeImage := 0;
lpBitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
lpBitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
if IsPaletteDevice then
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 256;
lpBitmapInfo^.bmiHeader.biClrImportant := 256;
end
else
begin
lpBitmapInfo^.bmiHeader.biClrUsed := 0;
lpBitmapInfo^.bmiHeader.biClrImportant := 0;
end;
hBm := Bmp.ReleaseHandle;
hPal := Bmp.ReleasePalette;
DC := GetDc(0);
if IsPaletteDevice then
begin
OldPal := SelectPalette(DC, hPal, True);
RealizePalette(DC);
end;
GetDiBits(DC, hBm, 0, lpBitmapInfo^.bmiHeader.biHeight, nil,
TBitmapInfo(lpBitmapInfo^), DIB_RGB_COLORS);
hBits := GlobalAlloc(GMEM_MOVEABLE, lpBitmapInfo^.bmiHeader.biSizeImage);
pBits := GlobalLock(hBits);
GetDiBits(DC, hBm, 0,lpBitmapInfo^.bmiHeader.biHeight, pBits,
TBitmapInfo(lpBitmapInfo^), DIB_RGB_COLORS);
if IsPaletteDevice then
begin
GetMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
//NumPalEntries := GetPaletteEntries(hPal, 0, 256, lPPalEntriesArray^);
NumPalEntries := GetSystemPaletteEntries(DC, 0, 256, lPPalEntriesArray^);
for i := 0 to (NumPalEntries - 1) do
begin
lpBitmapInfo^.bmiColors.rgbRed := lPPalEntriesArray^.peRed;
lpBitmapInfo^.bmiColors.rgbGreen := lPPalEntriesArray^.peGreen;
lpBitmapInfo^.bmiColors.rgbBlue := lPPalEntriesArray^.peBlue;
end;
FreeMem(lPPalEntriesArray, SizeOf(TPaletteEntry) * 256);
end;
if IsPaletteDevice then
begin
SelectPalette(DC, OldPal, TRUE);
RealizePalette(DC);
end;
ReleaseDc(0, DC);
IsDestPaletteDevice := GetDeviceCaps(DestDc, RASTERCAPS) and RC_PALETTE = RC_PALETTE;
if IsPaletteDevice then
begin
OldPal := SelectPalette(DestDc, hPal, TRUE);
RealizePalette(DestDc);
end;
{Do the Blt}
StretchDiBits(DestDc, X, Y, Width, Height, 0, 0, OriginalWidth,
lpBitmapInfo^.bmiHeader.biHeight, pBits, lpBitmapInfo^, DIB_RGB_COLORS, SrcCopy);
if IsDestPaletteDevice then
begin
SelectPalette(DestDc, OldPal, TRUE);
RealizePalette(DestDc);
end;
//=====================我加的无任何作用
// Bmp.Modified:=true;
// Bmp.Transparent:=true;
// Bmp.TransparentColor:=clwhite;
// Bmp.Canvas.Draw(0,0,Bmp);
// Bmp.Transparent:= true;
// TransparentBlt(hBm, 0, 0, Bmp.Width, BmP.Height,
// Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, RGB($FF, $FF, $FF));
//======================
GlobalUnLock(hBits);
GlobalFree(hBits);
FreeMem(lpBitmapInfo, BitmapInfoSize);
Bmp.Handle := hBm;
Bmp.Palette := hPal;
end;