How to fade in/out an image (Views: 31)
Problem/Question/Abstract: How to fade in/out an image? Answer: // Stores the colors // 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 // Preparing the Bitmap // Bitmap := TBitmap.Create; try Bitmap.PixelFormat := pf32bit; // 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]; // Getting colors from final image // Row := Bitmap.Scanline[y]; // 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); // Output new image // InvalidateRect(Form1.Handle, nil, False); // 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 // Preparing the Bitmap // Bitmap := TBitmap.Create; try Bitmap.PixelFormat := pf32bit; // 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]; // Getting colors from final image // Row := Bitmap.Scanline[y]; // 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); // Output new image // InvalidateRect(Form1.Handle, nil, False); // Redraw window // RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW); end; finally BaseBitmap.Free; end; finally Bitmap.Free; end; end; // You call the function like this procedure TForm1.Button1Click(Sender: TObject); begin FadeIn('C:\TestImage.bmp') end; |