这是 BCB 代码,可以把指定任何颜色透掉。你可以翻译到 delphi. 将Image 画到ahdc 上,透明掉 TrCol 颜色。void DrawTransparentBitmap (HDC ahdc ,Graphics::TBitmap *Image,Word xStart,Word yStart,TColor TrCol ) { TColor TransparentColor; TColorRef cColor ; HBITMAP bmAndBack,bmAndObject,bmAndMem,bmSave,bmBackOld,bmObjectOld,bmMemOld,bmSaveOld ; HDC hdcMem,hdcBack,hdcObject,hdcTemp,hdcSave ; TPoint ptSize; TransparentColor = TrCol; TransparentColor = (TColor)(TransparentColor | 0x02000000); hdcTemp = CreateCompatibleDC (ahdc); SelectObject (hdcTemp, Image->Handle); // select the bitmap ptSize.x = Image->Width; ptSize.y = Image->Height; DPtoLP (hdcTemp, &ptSize, 1); // convert from device logical points hdcBack = CreateCompatibleDC(ahdc); hdcObject = CreateCompatibleDC(ahdc); hdcMem = CreateCompatibleDC(ahdc); hdcSave = CreateCompatibleDC(ahdc);// create a bitmap for each DC // monochrome DC bmAndBack = CreateBitmap (ptSize.x, ptSize.y, 1, 1, NULL); bmAndObject = CreateBitmap (ptSize.x, ptSize.y, 1, 1, NULL);bmAndMem = CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y); bmSave = CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y);// each DC must select a bitmap object to store pixel data bmBackOld = SelectObject (hdcBack, bmAndBack); bmObjectOld = SelectObject (hdcObject, bmAndObject); bmMemOld = SelectObject (hdcMem, bmAndMem); bmSaveOld = SelectObject (hdcSave, bmSave);// set proper mapping mode SetMapMode (hdcTemp, GetMapMode (ahdc));// save the bitmap sent here, because it will be overwritten BitBlt (hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);// set the background color of the source DC to the color. // contained in the parts of the bitmap that should be transparent cColor = SetBkColor (hdcTemp, TransparentColor);// create the object mask for the bitmap by performing a BitBlt() // from the source bitmap to a monochrome bitmap BitBlt (hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);// set the background color of the source DC back to the original color SetBkColor (hdcTemp, cColor);// create the inverse of the object mask BitBlt (hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);// copy the background of the main DC to the destination BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, ahdc, xStart, yStart, SRCCOPY);// mask out the places where the bitmap will be placed BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);// mask out the transparent colored pixels on the bitmap BitBlt (hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);// XOR the bitmap with the background on the destination DC BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);// copy the destination to the screen BitBlt (ahdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);// place the original bitmap back into the bitmap sent here 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));DeleteDC (hdcMem); DeleteDC (hdcBack); DeleteDC (hdcObject); DeleteDC (hdcSave); DeleteDC (hdcTemp); }
unit Unit2;interfaceuses Windows,Graphics; function CreateRegion(SourceBmp:TBitmap;MyTransparentColor:Tcolor):HRGN;implementationfunction CreateRegion(SourceBmp:TBitmap;MyTransparentColor:Tcolor):HRGN; var offbyte,lineaddress,x,y:integer; isline:boolean; bmp:Tbitmap; Cx,Cy:integer; bitcolor:byte; Begin bmp:=Tbitmap.Create; bmp.Assign(SourceBmp); with bmp,bmp.Canvas do begin Mask(MyTransparentColor); Monochrome:=True; Pixelformat:=pf1bit; BeginPath(handle); for y:=10 to Height-1 do Begin lineaddress:=longint(scanline[y]); isline:=false; for x:=0 to Width-1 do Begin offbyte:=(x shr 3); bitcolor:=((Pbyte(lineaddress+offbyte)^shl (x-(offbyte shl 3))) and $80); if bitcolor=0 then Begin if not isline then Begin isline:=True; Cx:=x; Cy:=y; End; if x=Width-1 then Begin Moveto(Cx,Cy); Lineto(Width,Cy); Lineto(Width,Cy+1); Lineto(Cx,Cy+1); CloseFigure(handle); End; End Else if isline then Begin isline:=false; moveto(cx,cy); lineto(x,cy); lineto(x,cy+1); lineto(cx,cy+1); closefigure(handle); End; End; End; EndPath(handle); Result:=PathtoRegion(handle); end; bmp.Free; End;end. 不明白什么意思可以联系我:[email protected]
Delphi和BCB到底是一家人,翻译过来也这么容易: procedure DrawTransparentBitmap(AHdc: HDC; AImage: TBitmap; xStart: WORD; yStart: WORD; TrCol: TColor); var TransparentColor: TColor; Color: TColorRef; bmAndBack, bmAndObject, bmAndMem, bmSave, bmBackOld, bmObjectOld, bmMemOld, bmSaveOld: HBITMAP; hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave: HDC; ptSize: TPoint;begin TransparentColor := TrCol; TransparentColor := TColor(TransparentColor or $02000000); hdcTemp := CreateCompatibleDC(AHdc); SelectObject(hdcTemp, AImage.Handle); ptSize.X := AImage.Width; ptSize.Y := AImage.Height; DPtoLP(hdcTemp, ptSize, 1); // Convert from device logical points hdcBack := CreateCompatibleDC(AHdc); hdcObject := CreateCompatibleDC(AHdc); hdcMem := CreateCompatibleDC(AHdc); hdcSave := CreateCompatibleDC(AHdc); // Create a bitmap for each DC // monochrome DC bmAndBack := CreateBitmap(ptSize.X, ptSize.Y, 1, 1, nil); bmAndObject := CreateBitmap(ptSize.X, ptSize.Y, 1, 1, nil); bmAndMem := CreateCompatibleBitmap(AHdc, ptSize.X, ptSize.Y); bmSave := CreateCompatibleBitmap(AHdc, ptSize.X, ptSize.Y); // each DC must select a bitmap object to store pixel data bmBackOld := SelectObject(hdcBack, bmAndBack); bmObjectOld := SelectObject(hdcObject, bmAndObject); bmMemOld := SelectObject(hdcMem, bmAndMem); bmSaveOld := SelectObject(hdcSave, bmSave); // set proper mapping mode SetMapMode(hdcTemp, GetMapMode(AHdc)); // save the bitmap sent here, because it will be overwritten BitBlt(hdcSave, 0, 0, ptSize.X, ptSize.Y, hdcTemp, 0, 0, SRCCOPY); // set the background color of the source DC to the color. // contained in the parts of the bitmap that should be transparent Color := SetBkColor(hdcTemp, TransparentColor); // create the object mask for the bitmap by performing a BitBlt() // from the source bitmap to a monochrome bitmap BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // set the background color of the source DC back to the original color SetBkColor (hdcTemp, Color); // create the inverse of the object mask BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY); // copy the background of the main DC to the destination BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, ahdc, xStart, yStart, SRCCOPY); // mask out the places where the bitmap will be placed BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND); // mask out the transparent colored pixels on the bitmap BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND); // XOR the bitmap with the background on the destination DC BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT); // copy the destination to the screen BitBlt(ahdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY); // place the original bitmap back into the bitmap sent here 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)); DeleteDC(hdcMem); DeleteDC(hdcBack); DeleteDC(hdcObject); DeleteDC(hdcSave); DeleteDC(hdcTemp);end;
大家好,感谢各位给我的帮助,我刚才实验了一种新的方法,实现了我要实现的功能,写出来供大家参考: 1、Form1的TransParentColor属性设为TRUE 2、Image1的TransParent属性设为TRUE 3、procedure TForm1.FormCreate(Sender: TObject); begin Brush.Style:=bsClear; //加这一句就行余勇 再一次感谢大家
OnPaint;
var
bmp:TBitmap;
begin
bmp:=TBitmap.Create;
try
bmp.LoadFromFile('...');
bmp.TransparentColor:= clWhite;
bmp.Transparent:= true;
Canvas.Draw(bmp);
finally
bmp.Free;
end;
end;
Image1.Picture.Bitmap.Transparent:= true;
将Image 画到ahdc 上,透明掉 TrCol 颜色。void DrawTransparentBitmap (HDC ahdc ,Graphics::TBitmap *Image,Word xStart,Word yStart,TColor TrCol )
{
TColor TransparentColor;
TColorRef cColor ;
HBITMAP bmAndBack,bmAndObject,bmAndMem,bmSave,bmBackOld,bmObjectOld,bmMemOld,bmSaveOld ;
HDC hdcMem,hdcBack,hdcObject,hdcTemp,hdcSave ;
TPoint ptSize; TransparentColor = TrCol;
TransparentColor = (TColor)(TransparentColor | 0x02000000); hdcTemp = CreateCompatibleDC (ahdc);
SelectObject (hdcTemp, Image->Handle); // select the bitmap ptSize.x = Image->Width;
ptSize.y = Image->Height;
DPtoLP (hdcTemp, &ptSize, 1); // convert from device logical points hdcBack = CreateCompatibleDC(ahdc);
hdcObject = CreateCompatibleDC(ahdc);
hdcMem = CreateCompatibleDC(ahdc);
hdcSave = CreateCompatibleDC(ahdc);// create a bitmap for each DC
// monochrome DC
bmAndBack = CreateBitmap (ptSize.x, ptSize.y, 1, 1, NULL);
bmAndObject = CreateBitmap (ptSize.x, ptSize.y, 1, 1, NULL);bmAndMem = CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap (ahdc, ptSize.x, ptSize.y);// each DC must select a bitmap object to store pixel data
bmBackOld = SelectObject (hdcBack, bmAndBack);
bmObjectOld = SelectObject (hdcObject, bmAndObject);
bmMemOld = SelectObject (hdcMem, bmAndMem);
bmSaveOld = SelectObject (hdcSave, bmSave);// set proper mapping mode
SetMapMode (hdcTemp, GetMapMode (ahdc));// save the bitmap sent here, because it will be overwritten
BitBlt (hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);// set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor (hdcTemp, TransparentColor);// create the object mask for the bitmap by performing a BitBlt()
// from the source bitmap to a monochrome bitmap
BitBlt (hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);// set the background color of the source DC back to the original color
SetBkColor (hdcTemp, cColor);// create the inverse of the object mask
BitBlt (hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);// copy the background of the main DC to the destination
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, ahdc, xStart, yStart, SRCCOPY);// mask out the places where the bitmap will be placed
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);// mask out the transparent colored pixels on the bitmap
BitBlt (hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);// XOR the bitmap with the background on the destination DC
BitBlt (hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);// copy the destination to the screen
BitBlt (ahdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);// place the original bitmap back into the bitmap sent here
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));DeleteDC (hdcMem);
DeleteDC (hdcBack);
DeleteDC (hdcObject);
DeleteDC (hdcSave);
DeleteDC (hdcTemp);
}
Windows,Graphics; function CreateRegion(SourceBmp:TBitmap;MyTransparentColor:Tcolor):HRGN;implementationfunction CreateRegion(SourceBmp:TBitmap;MyTransparentColor:Tcolor):HRGN;
var
offbyte,lineaddress,x,y:integer;
isline:boolean;
bmp:Tbitmap;
Cx,Cy:integer;
bitcolor:byte;
Begin
bmp:=Tbitmap.Create;
bmp.Assign(SourceBmp);
with bmp,bmp.Canvas do
begin
Mask(MyTransparentColor);
Monochrome:=True;
Pixelformat:=pf1bit;
BeginPath(handle);
for y:=10 to Height-1 do
Begin
lineaddress:=longint(scanline[y]);
isline:=false;
for x:=0 to Width-1 do
Begin
offbyte:=(x shr 3);
bitcolor:=((Pbyte(lineaddress+offbyte)^shl (x-(offbyte shl 3))) and $80);
if bitcolor=0 then
Begin
if not isline then
Begin
isline:=True;
Cx:=x;
Cy:=y;
End;
if x=Width-1 then
Begin
Moveto(Cx,Cy);
Lineto(Width,Cy);
Lineto(Width,Cy+1);
Lineto(Cx,Cy+1);
CloseFigure(handle);
End;
End Else if isline then
Begin
isline:=false;
moveto(cx,cy);
lineto(x,cy);
lineto(x,cy+1);
lineto(cx,cy+1);
closefigure(handle);
End;
End;
End;
EndPath(handle);
Result:=PathtoRegion(handle);
end;
bmp.Free;
End;end.
不明白什么意思可以联系我:[email protected]
procedure DrawTransparentBitmap(AHdc: HDC; AImage: TBitmap;
xStart: WORD; yStart: WORD; TrCol: TColor);
var
TransparentColor: TColor;
Color: TColorRef;
bmAndBack,
bmAndObject,
bmAndMem,
bmSave,
bmBackOld,
bmObjectOld,
bmMemOld,
bmSaveOld: HBITMAP;
hdcMem,
hdcBack,
hdcObject,
hdcTemp,
hdcSave: HDC;
ptSize: TPoint;begin
TransparentColor := TrCol;
TransparentColor := TColor(TransparentColor or $02000000); hdcTemp := CreateCompatibleDC(AHdc);
SelectObject(hdcTemp, AImage.Handle); ptSize.X := AImage.Width;
ptSize.Y := AImage.Height;
DPtoLP(hdcTemp, ptSize, 1); // Convert from device logical points hdcBack := CreateCompatibleDC(AHdc);
hdcObject := CreateCompatibleDC(AHdc);
hdcMem := CreateCompatibleDC(AHdc);
hdcSave := CreateCompatibleDC(AHdc); // Create a bitmap for each DC
// monochrome DC bmAndBack := CreateBitmap(ptSize.X, ptSize.Y, 1, 1, nil);
bmAndObject := CreateBitmap(ptSize.X, ptSize.Y, 1, 1, nil); bmAndMem := CreateCompatibleBitmap(AHdc, ptSize.X, ptSize.Y);
bmSave := CreateCompatibleBitmap(AHdc, ptSize.X, ptSize.Y); // each DC must select a bitmap object to store pixel data bmBackOld := SelectObject(hdcBack, bmAndBack);
bmObjectOld := SelectObject(hdcObject, bmAndObject);
bmMemOld := SelectObject(hdcMem, bmAndMem);
bmSaveOld := SelectObject(hdcSave, bmSave); // set proper mapping mode
SetMapMode(hdcTemp, GetMapMode(AHdc)); // save the bitmap sent here, because it will be overwritten
BitBlt(hdcSave, 0, 0, ptSize.X, ptSize.Y, hdcTemp, 0, 0, SRCCOPY); // set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
Color := SetBkColor(hdcTemp, TransparentColor); // create the object mask for the bitmap by performing a BitBlt()
// from the source bitmap to a monochrome bitmap
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // set the background color of the source DC back to the original color
SetBkColor (hdcTemp, Color); // create the inverse of the object mask
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY); // copy the background of the main DC to the destination
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, ahdc, xStart, yStart, SRCCOPY); // mask out the places where the bitmap will be placed
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND); // mask out the transparent colored pixels on the bitmap
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND); // XOR the bitmap with the background on the destination DC
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT); // copy the destination to the screen
BitBlt(ahdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY); // place the original bitmap back into the bitmap sent here
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)); DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);end;
1、Form1的TransParentColor属性设为TRUE
2、Image1的TransParent属性设为TRUE
3、procedure TForm1.FormCreate(Sender: TObject);
begin
Brush.Style:=bsClear; //加这一句就行余勇 再一次感谢大家