两张图片透明叠加代码优化。 ( 积分: 200 )

  • 主题发起人 主题发起人 shaoye9604
  • 开始时间 开始时间
S

shaoye9604

Unregistered / Unconfirmed
GUEST, unregistred user!
两张图片透明叠加代码优化。
下面是我写的函数,速度慢了点,想用ScanLine来改写,但一直没写出来,请高手出手
相助!

Imag,BmpFileName:放置两张图片
XPos,YPos:叠加的位置
V:透明程序

procedure TransImage(Image: TImage; BmpFileName: string; XPos, YPos: Integer; V: Integer);
var
TempBmp : TBitmap;
hBmp, wBmp : Integer;
h, w : Integer;
r1, g1, b1, r2, g2, b2 : Byte;
begin
TempBmp := Tbitmap.Create;
TempBmp.PixelFormat := pf24bit;
TempBmp.LoadFromFile(BmpFileName);
hBmp := TempBmp.Height;
wBmp := TempBmp.Width;
for w := 0 to wBmp - 1 do
for h := 0 to hBmp - 1 do
begin
r1 := GetRValue(TempBmp.Canvas.Pixels[w, h]);
g1 := GetGValue(TempBmp.Canvas.Pixels[w, h]);
b1 := GetBValue(TempBmp.Canvas.Pixels[w, h]);
r2 := GetRValue(Image.Canvas.Pixels[XPos + w, YPos + h]);
g2 := GetGValue(Image.Canvas.Pixels[XPos + w, YPos + h]);
b2 := GetBValue(Image.Canvas.Pixels[XPos + w, YPos + h]);

Image.Canvas.Pixels[XPos + w, YPos + h] := RGB((R1 * V div 255) + (R2 * (255 - V) div 255), (G1 * V div 255) + (G2 * (255 - V) div 255), (B1 * V div 255) + (B2 * (255 - V) div 255));
end;
Tempbmp.Free;
end;
 
没人会吗,分太少吗?
我顶顶顶!!!
 
很强!
在循环前可以判断下w,h,将小的放到外层;div 255 也许可以用移位
 
哦,256还可以用shr,255么,呵呵~~失败~~.
V定义为byte类型好点吧
 
我当年开始学Delphi时也是这么干的
但后来发现有比这快的多(至少7-10倍)的方法:用“流(TStream)”。把图象存到流中,然后处理,这需要一点BMP文件格式的基础知识(网上肯定能找到)
 
用getbitmapbits 取数据到数组
用setbitmapbits 把数组画到图上

如下面的语句,是把图变暗
var
i,j,k:integer;
begin
getbitmapbits(image1.Picture.Bitmap.Handle ,szsize,bmppx);
for i:=0 to image1.Picture.Bitmap.height-1 do
for j:=0 to image1.Picture.Bitmap.width-1 do
for k:=0 to 2 do
bmppx[longint(k+j*3+i*image1.Picture.Bitmap.Width*3)]:=trunc(bmppx[longint(k+j*3+i*image1.Picture.Bitmap.Width*3)]*0.8);
setbitmapbits(image1.Picture.Bitmap.Handle ,szsize,bmppx);
self.Canvas.Draw(image1.Left ,image1.Top ,image1.Picture.Graphic);
 
下面是两个图片合成

var
bgpx:array of Byte;
bmppx: array of Byte;
bmppx1: array of Byte;
bmpgirl:Tbitmap;
szsize,szsize1:longint;
i,j,k:integer;
begin
bmpgirl:=Tbitmap.Create ;
bmpgirl.PixelFormat:=pf24bit;//设定每一点占的字节数为3;
bmpgirl.Width :=236;
bmpgirl.Height :=236;
szsize:=longint(image1.Picture.Bitmap.Width * image1.Picture.Bitmap.Height * 3);
szsize1:=longint(image2.Picture.Bitmap.Width * image2.Picture.Bitmap.Height * 3);
setlength(bmppx,szsize);
setlength(bgpx,szsize1);
setlength(bmppx1,bmpgirl.Width*bmpgirl.Height*3);
getbitmapbits(image1.Picture.Bitmap.Handle ,szsize,bmppx);
getbitmapbits(image2.Picture.Bitmap.Handle ,szsize1,bgpx);
for i:=0 to image1.Picture.Bitmap.height-1 do
for j:=0 to image1.Picture.Bitmap.width-1 do
for k:=0 to 2 do
bgpx[longint(k+(j+100)*3+i*image2.Picture.Bitmap.Width*3)]:=trunc(bgpx[longint(k+(j+100)*3+i*image2.Picture.Bitmap.Width*3)]*0.5)+trunc(bmppx[longint(k+j*3+i*image1.Picture.Bitmap.Width*3)]*0.5);
setbitmapbits(image2.Picture.Bitmap.Handle ,szsize1,bgpx);
self.Canvas.Draw(image1.Left ,image1.Top ,image2.Picture.Graphic);
end;
 
下面代码可旋转图片

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure xz(hd :single);
public
{ Public declarations }
end;

var
Form1: TForm1;
bmpgirl:Tbitmap;
bgpx:array of Byte;
bmppx: array of Byte;
bmppx1: array of Byte;
zjs:integer;
szsize:longint;
jd:integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
jd:=strtoint(edit1.text);
xz(jd*3.1415926/180);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
bmpgirl:=Tbitmap.Create ;
bmpgirl.PixelFormat:=pf24bit;//设定每一点占的字节数为3;
bmpgirl.Width :=236;
bmpgirl.Height :=236;
szsize:=longint(image1.Picture.Bitmap.Width * image1.Picture.Bitmap.Height * 3);
setlength(bmppx,szsize);
setlength(bmppx1,bmpgirl.Width*bmpgirl.Height*3);
getbitmapbits(image1.Picture.Bitmap.Handle ,szsize,bmppx);
end;


procedure TForm1.xz(hd :single);
var
i,j,k,l,a,b:integer;
kd:integer;
c,s,a1,b1:integer;
begin
kd:=bmpgirl.Width*3;
c:=round(cos(hd)*256);//小数放大256倍,使得可以在循环中使用整数运算,来加快速度
s:=round(sin(hd)*256);
k:=round((image1.Picture.Bitmap.height * s) / 256+(bmpgirl.width-(image1.Picture.Bitmap.width*c) / 256-(image1.Picture.Bitmap.height * s) / 256) / 2);
l:=round(bmpgirl.height / 2 - (image1.Picture.Bitmap.Width * s) / 512 -(image1.Picture.Bitmap.height * c) / 512 );
b:=0;
for i:=0 to (image1.Picture.Bitmap.height-1) do
begin
a1:=k shl 8-i*s;
b1:=l shl 8+i*c;
for j:=0 to (image1.Picture.Bitmap.width-1) do
begin
a:=(j*c+a1) shr 8 *3 +(j*s+b1) shr 8 * kd;//用整数移位运算
bmppx1[a]:=bmppx;
bmppx1[1+a]:=bmppx[1+b];
bmppx1[2+a]:=bmppx[2+b];
bmppx1[3+a]:=bmppx;
bmppx1[4+a]:=bmppx[1+b];
bmppx1[5+a]:=bmppx[2+b];
b:=b+3;
end;
end;

setbitmapbits(bmpgirl.Handle ,bmpgirl.Width*bmpgirl.Height*3,bmppx1);
TransparentBlt(self.Canvas.Handle,image1.Left ,image1.Top ,bmpgirl.width ,bmpgirl.height ,bmpgirl.Canvas.Handle,0,0,bmpgirl.width ,bmpgirl.height ,clblack);
end;

end.
 
后退
顶部