象"东方影都"等一样,一幅不规则的图像,如何把不需要的白色底色镂空,变得透明?
解决方案 »
- 请教DELPHI的记录集方面的问题
- 把一个listbox多选的内容导入到另一个listbox中的问题
- 如何才能看到只有.dcu的文件里的枚举、函数(参数)的定义??(有.dpr文件)
- 如何获得系统的时间?
- 如何让报表显示比较长的数据的问题!
- 如何增加数据时不会立刻写入数据库中,而是调用POST方法后才写入呢
- 如何通过delphi 如一按button 按扭将excel 文件显示在form中,并存入sql2000
- 请问怎样用循环语句在DBgird中插入记录
- image1.picture.loadfromfile(xyz.jpg);这么简单的一句都出错!!!!
- Kylix的D版出来了!!!!!!!!!!!!
- 何处有delphi下载?
- 用Delphi怎样执行SQL server脚本文件
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; //加这一句就行余勇 再一次感谢大家