程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi給圖片添加平鋪水印效果

Delphi給圖片添加平鋪水印效果

編輯:Delphi

function TForm1.WaterMaskBitmap(SrcBmp, MaskBmp: Tbitmap;AlphaColor:Tcolor;AlphaValue:integer): Tbitmap; type TPixels = Array of array of TRGBTriple; function GetPPbitmap(const Width,height:integer;SrcBmp:Tbitmap):Tbitmap; var xi,yi:integer; begin Result:=Tbitmap.Create; Result.Width:=Width; Result.Height:=Height; xi:=0; while (xi<=Result.Width) do begin yi:=0; while (yi<=Result.Height) do begin Result.Canvas.Draw(xi,yi,SrcBmp); yi:=yi+SrcBmp.Height; end; xi:=xi+SrcBmp.Width; end; end; Procedure GraphicFading(PicA, PicB: TPixels; const PicR: tBitmap; Percent: Byte);//Make a Fading Picture From var //PicA to PicB MidR,MidG,MidB : Byte; i,j : integer; m:Integer; pixPtrR : PByteArray; Position,pixpos : Single; rPos,gPos:Integer; PicRWidth:Integer; begin Position := Percent / 100; PicRWidth:=PicR.Width-1; for i := 0 to picR.Height -1 do begin PixPtrR := picR.ScanLine[i]; for j := 0 to picRWidth do Begin m:=j*3; rPos:=m+2; gPos:=m+1; if (PicB[j,i].RGBTRed=GetRValue(AlphaColor)) and (PicB[j,i].RGBTgREEN=GetGValue(AlphaColor)) and (PicB[j,i].RGBTBlue=GetBValue(AlphaColor)) then pixpos:=0 else pixpos:=Position; midR := PicA[j,i].RGBTRed+Round((PicB[j,i].RGBTRed-PicA[j,i].RGBTRed)*pixpos); midG := PicA[j,i].RGBTgREEN+Round((PicB[j,i].RGBTgREEN-PicA[j,i].RGBTgREEN)*pixpos); midB := PicA[j,i].RGBTBlue+Round((PicB[j,i].RGBTBlue-PicA[j,i].RGBTBlue)*pixpos); pixPtrR[m] := midB; pixPtrR[gPos] := midG; pixPtrR[rPos] := MidR; end; end; end; procedure ReadPixel(Pic: Tbitmap; var tPix: TPixels); Var PixPtr:PbyteArray; i,j,m:Integer; begin SetLength(tPix,Pic.Width,Pic.Height); Pic.PixelFormat := pf24bit; Pic.HandleType:=bmDIB; For i :=0 to pic.Height-1 do begin PixPtr:=Pic.ScanLine[i]; for j:= 0 to pic.Width-1 do begin m := j*3; tPix[j,i].rgbtBlue:=PixPtr[m]; tPix[j,i].rgbtGreen := PixPtr[m+1]; tPix[j,i].rgbtRed := PixPtr[m+2]; end; end; end; var PixA,PixB:TPixels; begin Result:=Tbitmap.Create; Result.PixelFormat := pf24Bit; Result.HandleType:=bmDIB; Result.Width:=SrcBmp.Width; Result.Height:=SrcBmp.Height; ReadPixel(SrcBmp,PixA); ReadPixel(GetPPbitmap(Result.Width,Result.Height,MaskBmp),PixB); GraphicFading(PixA,PixB,result,AlphaValue); end; procedure TForm1.BitBtn1Click(Sender: TObject); var bmpm,bmps:tbitmap; begin Bmpm:=TBitmap.Create; bmpm.LoadFromFile(D:pic2.bmp); bmps:=tbitmap.Create; bmps.LoadFromFile(D:pic1.bmp); self.Image1.Picture.Bitmap.Assign(self.WaterMaskBitmap(bmps,bmpm,clwhite,50)); end; function TForm1.WaterMaskBitmap(SrcBmp, MaskBmp: Tbitmap;AlphaColor:Tcolor;AlphaValue:integer): Tbitmap; type TPixels = Array of array of TRGBTriple; function GetPPbitmap(const Width,height:integer;SrcBmp:Tbitmap):Tbitmap; var xi,yi:integer; begin Result:=Tbitmap.Create; Result.Width:=Width; Result.Height:=Height; xi:=0; while (xi<=Result.Width) do begin yi:=0; while (yi<=Result.Height) do begin Result.Canvas.Draw(xi,yi,SrcBmp); yi:=yi+SrcBmp.Height; end; xi:=xi+SrcBmp.Width; end; end; Procedure GraphicFading(PicA, PicB: TPixels; const PicR: tBitmap; Percent: Byte);//Make a Fading Picture From var //PicA to PicB MidR,MidG,MidB : Byte; i,j : integer; m:Integer; pixPtrR : PByteArray; Position,pixpos : Single; rPos,gPos:Integer; PicRWidth:Integer; begin Position := Percent / 100; PicRWidth:=PicR.Width-1; for i := 0 to picR.Height -1 do begin PixPtrR := picR.ScanLine[i]; for j := 0 to picRWidth do Begin m:=j*3; rPos:=m+2; gPos:=m+1; if (PicB[j,i].RGBTRed=GetRValue(AlphaColor)) and (PicB[j,i].RGBTgREEN=GetGValue(AlphaColor)) and (PicB[j,i].RGBTBlue=GetBValue(AlphaColor)) then pixpos:=0 else pixpos:=Position; midR := PicA[j,i].RGBTRed+Round((PicB[j,i].RGBTRed-PicA[j,i].RGBTRed)*pixpos); midG := PicA[j,i].RGBTgREEN+Round((PicB[j,i].RGBTgREEN-PicA[j,i].RGBTgREEN)*pixpos); midB := PicA[j,i].RGBTBlue+Round((PicB[j,i].RGBTBlue-PicA[j,i].RGBTBlue)*pixpos); pixPtrR[m] := midB; pixPtrR[gPos] := midG; pixPtrR[rPos] := MidR; end; end; end; procedure ReadPixel(Pic: Tbitmap; var tPix: TPixels); Var PixPtr:PbyteArray; i,j,m:Integer; begin SetLength(tPix,Pic.Width,Pic.Height); Pic.PixelFormat := pf24bit; Pic.HandleType:=bmDIB; For i :=0 to pic.Height-1 do begin PixPtr:=Pic.ScanLine[i]; for j:= 0 to pic.Width-1 do begin m := j*3; tPix[j,i].rgbtBlue:=PixPtr[m]; tPix[j,i].rgbtGreen := PixPtr[m+1]; tPix[j,i].rgbtRed := PixPtr[m+2]; end; end; end; var PixA,PixB:TPixels; begin Result:=Tbitmap.Create; Result.PixelFormat := pf24Bit; Result.HandleType:=bmDIB; Result.Width:=SrcBmp.Width; Result.Height:=SrcBmp.Height; ReadPixel(SrcBmp,PixA); ReadPixel(GetPPbitmap(Result.Width,Result.Height,MaskBmp),PixB); GraphicFading(PixA,PixB,result,AlphaValue); end; procedure TForm1.BitBtn1Click(Sender: TObject); var bmpm,bmps:tbitmap; begin Bmpm:=TBitmap.Create; bmpm.LoadFromFile(D:pic2.bmp); bmps:=tbitmap.Create; bmps.LoadFromFile(D:pic1.bmp); self.Image1.Picture.Bitmap.Assign(self.WaterMaskBitmap(bmps,bmpm,clwhite,50)); end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved