Load Gif with animation in lazarus - lazarus

I used this code to load an image (gif)
image:TGIFImage;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
image:=TGIFImage.Create;
image.LoadFromFile('F:\ltdk\pic\bom.gif');
image3.Picture.Assign(image);
end;
but it doesn't work , so I need know how to load gif with animation in lazarus ?
thanks

Creating a TGIFImage and loading the GIF in it will only show a frame of the file, not animate it. In order to animate a GIF file you need some code.
You can check out these resources:
(The last message on this page:)
http://forum.lazarus.freepascal.org/index.php/topic,14884.msg80387.html#msg80387
http://forum.lazarus.freepascal.org/index.php?topic=7818.0

Related

Assign a picture to a TImage and define the background color of transparent pixels

Problem:
I have a TImage on a Delphi VCL form and want to assign a TGraphic.
The picture has transparent pixels.
When displaying the picture inside the TImage I want to display all transparent pixels in the color clBlue.
Unfortunately TImage does not have a property for a background color.
What I have already tried:
Fill the TImage with blue and afterwards to assign the picture:
MyImage.Canvas.Brush.Style := bsSolid;
MyImage.Canvas.Brush.Color := clBlue;
MyImage.Canvas.FillRect(Rect(0, 0, MyImage.Width, MyImage.Height));
MyImage.Picture.Assign(MyGraphic);
Set the TransparentColor of the bitmap:
MyImage.Picture.Bitmap.TransparentColor := clBlue;
MyImage.Picture.Assign(MyGraphic);
Nothing worked :-(
Create a new TBitmap of the desired size, and fill it completely with a solid clBlue color.
Then, draw the source image transparently onto this TBitmap, so that the blue color shows through the transparent areas.
Then, assign this TBitmap to the TImage.
Alternatively, place the TImage onto a TPanel, set the panel's color to clBlue, and then assign the transparent image as-is to the TImage.
Alternatively, use a TPaintBox instead of a TImage. In the TPaintBox.OnPaint event, draw a clBlue background onto the TPaintBox.Canvas, then draw the source image transparently onto the Canvas.
procedure DrawPictureWithBackgroundColor(Image: TImage; Graphic: TGraphic; BackgroundColor: TColor);
begin
Image.Canvas.Brush.Style := bsSolid;
Image.Canvas.Brush.Color := BackgroundColor;
Image.Canvas.FillRect(Rect(0, 0, Image.Width, Image.Height));
Image.Canvas.StretchDraw(Rect(0, 0, Image.Width, Image.Height), Graphic);
end;
Usage:
DrawPictureWithBackgroundColor(MyImage, MyGraphic, clBlue);
MyGraphic contains the picture data itself.
MyImage is the control/container which makes the picture visible on the form.

Can't get transparent color for TBitmap

I have a bitmap that I want to draw on TPaintBox. The problem is that I have to rotate it to a specific angle before. I decided to use TBitmap32.
I do it this way: I create a TBitmap first, then transfer it to TBitmap32, do a conversion and move TBitmap32 to TBitmap again. I am drawing this last TBitmap on TPaintBox. The problem is I can't get transparency.
The bitmap has a red background that I want to make transparent. Will you help? What am I doing wrong?
carImage32 := TBitmap32.Create;
carImage32.Width := carImageTMP.Width;
carImage32.Height := carImageTMP.Height;
carImage32.Canvas.Draw(0, 0, carImageTMP); //assign TBitmap
carImage := TBitmap.Create;
carImage.Width := carImageTMP.Width;
carImage.Height := carImageTMP.Height;
RotateBitmap(carImage32,angle,false,clNone,True);
carImage.Transparent:=True;
carImage.TransparentColor:=clRed;
carImage.Assign(carImage32);
paintBox.Canvas.Draw(0,0,carImage);
I solved my problem.
I should use also for TBitmap32 this part of code:
auto.carImage32.DrawMode:=dmTransparent;
auto.carImage32.OuterColor:=clRed32;
and worked :)

move TRectangle with mouse (FMX, Win32)

I have an FMX form with a TLayout on it aligned to client. On the TLayout I have a TRectangle. I can move the TRectangle easily with the following code in a button click event:
Rectangle1->Position->X = Rectangle1->Position->X + 10;
Is there a clean way for me to do this (move the rectangle) with the mouse? Like click on the Rectangle and move it around to a new location? I'm just playing around trying to make a little drawing program to learn....
Using C++Builder 10.2 Version 25.0.29899.2631 and building in Win32.
UPDATE: I took Hans approach and ended up making it work nicely. I've added the full code as an answer below. Yay!
A way to drag components is to store the offset between the mouse position and the control position on mouse down, then use this offset to calculate the position of the control in the mouse move event.
In semi-pseudo code it would look like this:
Add the following to your TForm class:
fMouseIsDown: boolean;
fMouseDownOffset: TPointF;
procedure OnRectangleMouseDown(X,Y)
begin
fMouseIsDown := true;
fMouseDownOffset := PointF(Rectangle.Position.X-X, Rectangle.Position.Y-Y)
end;
procedure OnRectangleMouseMove(X,Y)
begin
if fMouseIsDown then
begin
Rectangle.Position.X := X+fMouseDownOffset.X;
Rectangle.Position.Y := Y+fMouseDownOffset.Y;
end;
end;
procedure OnRectangleMouseUp(X,Y);
begin
fMouseIsDown := false;
end;
Here is the complete code needed to left-click on and move a TRectangle on an FMX form in Win32 (haven't tried it on mobile yet). Just create a new FireMonkey multi-device application and put a TRectangle and a TButton on it.
Code to add to the forms's class declaration (in the .h file just under class TForm1 : public TForm {):
bool fMouseIsDown; // gets set to TRUE when left mouse click on the rectangle
TPointF fMousePosGlobal; // this is the mouses position relative to the screen
TPointF fMousePosForm; // this is the mouse pos relative to the form
TPointF captionOffset; // this is a small offset necessary since the form's TOP and LEFT are outside of form's client area due to caption bar and left edge of form
TPointF fMouseInRectAtClick; // this is the mouse pos relative to the rectangle (top left of rectangle is 0,0)
Code to add to the rectangle's Rectangle1MouseDown event:
if (Button == 0) { // 0 for left mb, 1 for right mb
fMouseIsDown = true;
fMouseInRectAtClick.X = X; //mouse pos with respect to rectangle at time of click
fMouseInRectAtClick.Y = Y;
}
Code to add to the rectangle's Rectangle1MouseMove event (add to the form's FormMouseMove too or sometimes you lose the rectangle on a fast drag):
fMousePosGlobal = Screen->MousePos(); //mouse global pos
fMousePosForm.X = fMousePosGlobal.X - Form1->Left; // mouse pos relative to the form
fMousePosForm.Y = fMousePosGlobal.Y - Form1->Top;
if (fMouseIsDown) {
Form1->Rectangle1->Position->X = fMousePosForm.X - captionOffset.X - fMouseInRectAtClick.X;
Form1->Rectangle1->Position->Y = fMousePosForm.Y - captionOffset.Y - fMouseInRectAtClick.Y;
}
Code to add to the Rectangle1MouseUp event:
fMouseIsDown = false; // add this to the form's MouseUp too in case you "lose" the rectangle on a drag. That only happened when i forget to set the offsets.
Code to add to the Button's Button1Click event:
captionOffset.X = 8.0; // this accounts for the width of the form left edge
captionOffset.Y = 30.0; // this accounts for the height of the form caption
// if you don't add this your "drag point" on the rectangle will jump as soon as you start the drag.
Thanks to Hans for the direction to start!
Also, i noticed the drag wasn't smooth when moving across other controls. If this bothers you then you need to set those other controls "HitTest" false so they ignore it. Add TEdit boxes if you want to see all the TPointF coordinates as you move mouse and rectangle - helps a bunch when trying to figure out coordinates.

Draw a vertically centered multi-line string using WinAPI's "DrawText" function

I am trying to draw a word-wrapped string within centered both vertically and horizontally within a bitmap using WinAPI's DrawText function.
The problem is that if text is longer than the available space and "END ELLIPSIS" (...) is added to a cropped string, the reported drawing coordinates returned when using the "DT_CALCRECT" report the uncropped number of lines which messes with the vertical centering calculations.
I read many posts on this, and thought that "Delphi - Draw text multiline in the centre of a rect" may hold the answer, but it didn't (screenshot of the code output using the sample in the linked question http://zoomplayer.com/pix/font_vcenter.jpg). The author of the accepted answer suggested I create a new question so here it is.
For quick-reference, here is a slightly simplified (removing unrelated code) text rendering code from the linked accepted answer:
procedure DrawTextCentered(Canvas: TCanvas; const R: TRect; S: String);
var
DrawRect: TRect;
DrawFlags: Cardinal;
begin
DrawRect := R;
DrawFlags := DT_END_ELLIPSIS or DT_NOPREFIX or DT_WORDBREAK or
DT_EDITCONTROL or DT_CENTER;
DrawText(Canvas.Handle, PChar(S), -1, DrawRect, DrawFlags or DT_CALCRECT);
DrawRect.Right := R.Right;
if DrawRect.Bottom < R.Bottom then
OffsetRect(DrawRect, 0, (R.Bottom - DrawRect.Bottom) div 2)
else
DrawRect.Bottom := R.Bottom;
DrawTextEx(Canvas.Handle, PChar(S), -1, DrawRect, DrawFlags, nil);
end;
As you can see from the screenshot, the problem is after the initial call to DrawText with the "DT_CALCRECT" flag to measure the output height for later vertical centering, rendering the string "Trending in: Worldwide" returns a DrawRect.Bottom value representing 3 lines of text even though only 2 lines are drawn, breaking the vertical centering code.

How to make transparent form in lazarus?

I want to make my form transparent. Then when i make AlphaBlend is true, all of component is transparent. How to make the form is transparent except for the component?
Form in lazarus
Windows solution:
uses Windows;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
Color:=clRed;
SetWindowLongPtr(Self.Handle, GWL_EXSTYLE,GetWindowLongPtr(Self.Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
SetLayeredWindowAttributes(Self.Handle, clRed, 0, LWA_COLORKEY);
end;
Filling the form in red color and creation him transparent.

Resources