怎么在delphix中实现一个图像的淡入效果? (50分)

  • 主题发起人 主题发起人 wzgss
  • 开始时间 开始时间
W

wzgss

Unregistered / Unconfirmed
GUEST, unregistred user!
在delphix中,有整个窗口fade in等的例子,但我想实现对一单个图像的淡入效果。drawadd
drawsub等方法也有淡入效果,但出来的图像好像失色了,象蒙上了绿色。
 
淡出
procedure TForm1.Button1Click(Sender: TObject);
var
x, y, i: integer;
Bitmap: TBitmap;
pixcolo: PByteArray;
begin
Bitmap := TBitmap.Create;
//创建TBitMap实例
try
Bitmap.LoadFromFile
('sample.bmp');
Bitmap.PixelFormat := pf24bit;
for i := 0 to 255 do
begin
for y := 0 to Bitmap.Height - 1 do
begin
pixcolo := Bitmap.Scanline[y];
//扫描每行像素颜色
for x := 0 to ((Bitmap.Width * 3) - 1) do
if pixcolo[x] > 0 then pixcolo[x] := (pixcolo[x] - 1);
//递减颜色值,不同的递减值可改变不同的速度
end;
Image1.Canvas.Draw(0, 0, Bitmap);
//画出图像
Application.ProcessMessages;
//系统做其他工作
end;
finally
Bitmap.free; //释放位图
end;
end;
 
淡入
procedure TForm1.Button2Click(Sender: TObject);
var
x, y, i, j: integer;
Bitmap1, Bitmap2: TBitmap;
pixcolo1, pixcolo2: PByteArray;
begin
Bitmap1 := TBitmap.Create;
Bitmap2 := TBitmap.Create;
try
Bitmap1.LoadFromFile('sample.bmp');
//将同一幅图像装入两个TBitmap实例
Bitmap2.LoadFromFile('sample.bmp');
Bitmap1.pixelFormat := pf24bit;
Bitmap2.pixelFormat := pf24bit;
for y := 0 to Bitmap2.height - 1 do
begin
pixcolo2 := Bitmap2.Scanline[y];
for x := 0 to ((Bitmap2.Width*3) - 1) do
pixcolo2[x] := 0;
//先将要处理的图像的像素颜色值设为0
end;
for i := 0 to 255 do
begin
for y := 0 to Bitmap2.Height - 1 do
begin
pixcolo2 := Bitmap2.Scanline[y];
pixcolo1 := Bitmap1.Scanline[y];
for x := 0 to ((Bitmap2.Width*3) - 1) do if
pixcolo2[x]<pixcolo1[x] then pixcolo2[x] := (pixcolo2[x] + 1);
end;
//与原始图的像素颜色值比较,并递增其值直到与原始图相等
Image1.Canvas.Draw(0, 0, Bitmap2);
Application.ProcessMessages;
end;
finally
Bitmap1.free
end;
end;
 
下面的代码可以在窗体上面淡入淡出一个图形:是不是delphix就不清楚了,你看一下吧
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[0..32767] of TRGBTriple;

/////////////////////////////////////////////////
// Fade In //
/////////////////////////////////////////////////

procedure FadeIn(ImageFileName: TFileName);
var
Bitmap, BaseBitmap: TBitmap;
Row, BaseRow : PRGBTripleArray;
x, y, step : integer;
begin
// Bitmaps vorbereiten / Preparing the Bitmap //
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf32bit; // oder pf24bit / or pf24bit //
Bitmap.LoadFromFile(ImageFileName);
BaseBitmap := TBitmap.Create;
try
BaseBitmap.PixelFormat := pf32bit;
BaseBitmap.Assign(Bitmap);
// Fading //
for step := 0 to 32 do
begin
for y := 0 to (Bitmap.Height - 1) do
begin
BaseRow := BaseBitmap.Scanline[y];
// Farben vom Endbild holen / Getting colors from final image //
Row := Bitmap.Scanline[y];
// Farben vom aktuellen Bild / Colors from the image as it is now //
for x := 0 to (Bitmap.Width - 1) do
begin
Row[x].rgbtRed := (step * BaseRow[x].rgbtRed) shr 5;
Row[x].rgbtGreen := (step * BaseRow[x].rgbtGreen) shr 5; // Fading //
Row[x].rgbtBlue := (step * BaseRow[x].rgbtBlue) shr 5;
end;
end;
Form1.Canvas.Draw(0, 0, Bitmap); // neues Bild ausgeben / Output new image //
InvalidateRect(Form1.Handle, nil, False);
// Fenster neu zeichnen / Redraw window //
RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW);
end;
finally
BaseBitmap.Free;
end;
finally
Bitmap.Free;
end;
end;

/////////////////////////////////////////////////
// Fade Out //
/////////////////////////////////////////////////


procedure FadeOut(ImageFileName: TFileName);
var
Bitmap, BaseBitmap: TBitmap;
Row, BaseRow: PRGBTripleArray;
x, y, step: integer;
begin
// Bitmaps vorbereiten / Preparing the Bitmap //
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf32bit; // oder pf24bit / or pf24bit //
Bitmap.LoadFromFile(ImageFileName);
BaseBitmap := TBitmap.Create;
try
BaseBitmap.PixelFormat := pf32bit;
BaseBitmap.Assign(Bitmap);
// Fading //
for step := 32 downto 0 do
begin
for y := 0 to (Bitmap.Height - 1) do
begin
BaseRow := BaseBitmap.Scanline[y];
// Farben vom Endbild holen / Getting colors from final image //
Row := Bitmap.Scanline[y];
// Farben vom aktuellen Bild / Colors from the image as it is now //
for x := 0 to (Bitmap.Width - 1) do
begin
Row[x].rgbtRed := (step * BaseRow[x].rgbtRed) shr 5;
Row[x].rgbtGreen := (step * BaseRow[x].rgbtGreen) shr 5; // Fading //
Row[x].rgbtBlue := (step * BaseRow[x].rgbtBlue) shr 5;
end;
end;
Form1.Canvas.Draw(0, 0, Bitmap); // neues Bild ausgeben / Output new image //
InvalidateRect(Form1.Handle, nil, False);
// Fenster neu zeichnen / Redraw window //
RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW);
sleep(20);
end;
finally
BaseBitmap.Free;
end;
finally
Bitmap.Free;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
FadeIn('F:/Documents/xywper0071.BMP')
end;

{*****************************}
{by Yucel Karapinar, ykarapinar@hotmail.com }

{ Only for 24 ve 32 bits bitmaps }

procedure FadeOut(const Bmp: TImage; Pause: Integer);
var
BytesPorScan, counter, w, h: Integer;
p : pByteArray;
begin
if not (Bmp.Picture.Bitmap.PixelFormat in [pf24Bit, pf32Bit]) then
raise Exception.Create('Error, bitmap format is not supporting.');
try
BytesPorScan := Abs(Integer(Bmp.Picture.Bitmap.ScanLine[1]) -
Integer(Bmp.Picture.Bitmap.ScanLine[0]));
except
raise Exception.Create('Error!!');
end;

for counter := 1 to 256 do
begin
for h := 0 to Bmp.Picture.Bitmap.Height - 1 do
begin
P := Bmp.Picture.Bitmap.ScanLine[h];
for w := 0 to BytesPorScan - 1 do
if P^[w] > 0 then P^[w] := P^[w] - 1;
end;
Sleep(Pause);
Bmp.Refresh;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FadeOut(Image1, 1);
end;
 
好象picshow组件中有该效果,看看源码。
 
后退
顶部