unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GIFImage, RVStyle, RVScroll, RichView, RVFuncs, ExtCtrls, RVEdit, CRVData, CRVFData, RVOfficeCnv,
StdCtrls,RVGifAnimate,RVItem, RVGifAnimate2007;
type
TForm2 = class(TForm)
btn1: TButton;
OpenDialog1: TOpenDialog;
RichViewEdit1: TRichViewEdit;
RVStyle1: TRVStyle;
procedure FormCreate(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
function CopyImageToTheImagesDir(ImageFileName: String; gr: TGraphic): String;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
RichViewEdit1.AnimationMode := rvaniOnFormat;
RichViewEdit1.StartAnimation;
end;
function TForm2.CopyImageToTheImagesDir(ImageFileName: String; gr: TGraphic): String;
var ImagesDir, NewImageFileName, ImageExt: String;
RandomValue: Integer;
begin
ImageFileName := AnsiLowerCase(ImageFileName);
ImagesDir := AnsiLowerCase(ExtractFilePath(Application.ExeName)+'Images\');
if Pos(ImagesDir,ImageFileName)<>1 then begin
NewImageFileName := ImagesDir+ExtractFileName(ImageFileName);
if FileExists(NewImageFileName) then begin
ImageExt := ExtractFileExt(NewImageFileName);
NewImageFileName := Copy(NewImageFileName, 1, Length(NewImageFileName)-Length(ImageExt));
RandomValue := Random(MaxInt);
while FileExists(NewImageFileName+IntToStr(RandomValue)+ImageExt) do
inc(RandomValue);
NewImageFileName := NewImageFileName+IntToStr(RandomValue)+ImageExt;
end;
if gr=nil then
CopyFile(PChar(ImageFileName), PChar(NewImageFileName), False)
else
gr.SaveToFile(NewImageFileName);
Result := NewImageFileName;
end
else
Result := ImageFileName;
end;
procedure TForm2.btn1Click(Sender: TObject);
var pic: TPicture;
gr: TGraphic;
ImageName: String;
begin
if OpenDialog1.Execute then begin
try
pic := TPicture.Create;
try
pic.LoadFromFile(OpenDialog1.FileName);
gr := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType));
gr.Assign(pic.Graphic);
// using relative path
ImageName := ExtractRelativePath(ExtractFilePath(Application.ExeName),
CopyImageToTheImagesDir(OpenDialog1.FileName, nil));
RichViewEdit1.TopLevelEditor.BeginUndoGroup(rvutInsert);
RichViewEdit1.TopLevelEditor.SetUndoGroupMode(True);
try
if RichViewEdit1.InsertPicture('', gr, rvvaBaseline) then
RichViewEdit1.SetCurrentItemExtraStrProperty(rvespImageFileName, ImageName, True);
finally
RichViewEdit1.TopLevelEditor.SetUndoGroupMode(False);
end;
finally
pic.Free;
end;
except
Application.MessageBox('Image loading error', 'Error', 0);
end;
end;
end;
initialization
RegisterClass(TGifImage);
TPicture.RegisterFileFormat('GIF', 'The best format', TGifImage);
end.