How to paste GIF from clipboard?

General TRichView support forum. Please post your questions here
Post Reply
huafeifei
Posts: 6
Joined: Wed Jul 26, 2006 9:05 am

How to paste GIF from clipboard?

Post by huafeifei »

First copy a GIF file from some webpage in IE/Firefox browser, then paste into tRichViewEdit.

tRichViewEdit recognize the GIF file as bitmap, and no animation! RVGifAnimate unit is included, but doesn't work.

How can I display GIF when paste a GIF file from clipboard? And show it animated and transparent?

How to recognize picture format in clipboard is GIF, JPEG or BMP?

Pls give me example.

Thanks...........................
Last edited by huafeifei on Wed Jul 26, 2006 10:40 am, edited 1 time in total.
huafeifei
Posts: 6
Joined: Wed Jul 26, 2006 9:05 am

Post by huafeifei »

Try with code belowing, can paste image, but no animation.

try
pic := TPicture.Create;
try
// pic.LoadFromFile(OpenDialog1.FileName);
pic.Bitmap.Assign(Clipboard);
pic.SaveToFile('c:\a.gif');//debug information
gr := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType));
gr.Assign(pic);
finally
pic.Free;
end;
if TextAsName and Clipboard.HasFormat(CF_TEXT) then
s := Clipboard.AsText
else
s := '';
if gr<>nil then
InsertPicture(s,gr,rvvaBaseLine);
except
Application.MessageBox(PChar('Cannot read picture from clipboard'), 'Error',
MB_OK or MB_ICONSTOP);
end;

//Here is original codes of PasteBitmap
// bmp := TBitmap.Create;
// bmp.Assign(Clipboard);
// if TextAsName and Clipboard.HasFormat(CF_TEXT) then
// s := Clipboard.AsText
// else
// s := '';
// InsertPicture(s,bmp,rvvaBaseline);
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

As far as I know, there are no standard Clipboard formats for Gifs or Jpegs.
Browsers copy them to the Clipboard as bitmaps.
huafeifei
Posts: 6
Joined: Wed Jul 26, 2006 9:05 am

Post by huafeifei »

Thanks for your reply.

Some popular software can paste gif image from clipboard, like acdsee, outlook.

Is there any tips to solve this problem?


I will try with htmlimporter.
huafeifei
Posts: 6
Joined: Wed Jul 26, 2006 9:05 am

Post by huafeifei »

Problem solved! Share tip code here.

Acctually, clipboard copy image(jpeg/gif/bmp) operation from browser or harddisk will save file as bitmap(CF_BITMAP), at the same time, save file to hard disk and filename to CF_HDROP. Use Clipboard.HasFormat(CF_HDROP) and DragQueryFile to get filename in harddisk. Then you can load image file from harddisk according filename.

I modified the PasteBitmap, and it can work well with GIF image/JPEG image/GITMAP.

procedure TCustomRichViewEdit.PasteBitmap(TextAsName: Boolean);
var gr: TGraphic;
pic: TPicture;
GIFImage: TGifImage;
s: String;
i: integer;
drophandle :Thandle;
Filescount :integer;
FileName :array[0..MAX_PATH] of Char;
begin
{$IFNDEF RVDONOTUSEINPLACE}
if (InplaceEditor<>nil) and (InplaceEditor is TCustomRichViewEdit) then begin
TCustomRichViewEdit(InplaceEditor).PasteBitmap(TextAsName);
exit;
end;
{$ENDIF}
if not BeforeChange(False) then exit;
if not Clipboard.HasFormat(CF_BITMAP) then exit;

if Clipboard.HasFormat(CF_HDROP) then begin
DropHandle:=Clipboard.GetAsHandle(CF_HDROP);
FilesCount:=DragQueryFile(DropHandle, $FFFFFFFF, Filename, MAX_PATH); //get file count
for i:=0 to FilesCount-1 do begin
FillChar(FileName, SizeOf(FileName), 0);
if DragQueryFile(DropHandle, i, FileName, MAX_PATH)>0 then begin
try
pic := TPicture.Create;
try
pic.LoadFromFile(StrPas(@FileName));
gr := RV_CreateGraphics(TGraphicClass(pic.Graphic.ClassType));
gr.Assign(pic.Graphic);
finally
pic.Free;
end;
if TextAsName and Clipboard.HasFormat(CF_TEXT) then
s := Clipboard.AsText
else
s := '';
if gr<>nil then InsertPicture(s,gr,rvvaBaseLine);
except
Application.MessageBox(PChar('Cannot read picture from Clipboard'), 'Error', MB_OK or MB_ICONSTOP);
end;
StrPas(@FileName)
end;
end;
end;
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Thank you!
Something like this will be included in the next update of TRichView.
huafeifei
Posts: 6
Joined: Wed Jul 26, 2006 9:05 am

Post by huafeifei »

You do great job in developing the components.

Hope them to be more simply to use.
Darken
Posts: 11
Joined: Tue Jul 22, 2014 1:30 pm

Post by Darken »

Hello Sergey!

I'm using TRichViewEdit with image, bmp, jpeg - good copy and paste from clipboard. But if copy gif or png image, paste doesn't work. (used TGIFImage, TPNGObject and Delphi 7).

rve.CopyDef - for copy
rve.Paste - for paste

Can you have decision? :roll:

Thank you Sergey!
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

When you copy-paste in TRichViewEdit, data are pasted in RVF format.

You need to register graphic classes for insertion from RVF. Without registering, you should have the same problem when loading RVF files containing images.

In Dephi 7, TGIFImage and TPNGObject are not standard formats.

PNG:
Call (one time, before the first RVF loading):
RegisterClass([TPNGObject]);
Also, I recommend calling:

Code: Select all

uses RVGrHandler;

RVGraphicHandler.RegisterHTMLGraphicFormat(TPNGObject);
RVGraphicHandler.RegisterPngGraphic(TPNGObject);
GIF:
Include RVGifAnimate in your project. This unit includes registrations for TGifImage.
Darken
Posts: 11
Joined: Tue Jul 22, 2014 1:30 pm

Post by Darken »

I'm so sorry Sergey, but doesn't work...

I'm prepare test project with GIF and PNG library. Image insert in RVF document good, but if paste from clibpoard RichViewEdit do nothing.

http://softprostudio.com/Test_ClipboardIMAGE.7z

Code: Select all

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtDlgs, StdCtrls, RVScroll, RichView, RVEdit, RVFuncs,
  GIFImage, Jpeg, pngimage,  RVGifAnimate, RVStyle, ExtCtrls
  ;

type
  TForm1 = class(TForm)
    rve: TRichViewEdit;
    OpPictDialinsert: TOpenPictureDialog;
    Panel1: TPanel;
    btnOpenImage: TButton;
    btnCopy: TButton;
    btnCut: TButton;
    btnPaste: TButton;
    RVS: TRVStyle;
    procedure btnOpenImageClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure btnCutClick(Sender: TObject);
    procedure btnPasteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnOpenImageClick(Sender: TObject);
var
  gr: TGraphic;
begin
  if OpPictDialinsert.Execute then begin

    gr := RVGraphicHandler.LoadFromFile(OpPictDialinsert.FileName);

    if gr <> nil then begin
      try
        rve.BeginUpdate;
        RVE.InsertPicture(ExtractFileName(OpPictDialinsert.FileName), gr, rvvaBaseLine);
      finally
        rve.EndUpdate;
      end;
    end else
      Application.MessageBox(PChar('Can''t open file ' + OpPictDialinsert.FileName), 'Error',
        MB_OK or MB_ICONSTOP);

  end;

end;

procedure TForm1.btnCopyClick(Sender: TObject);
begin
  rve.CopyDef;
end;

procedure TForm1.btnCutClick(Sender: TObject);
begin
  rve.CutDef;
end;

procedure TForm1.btnPasteClick(Sender: TObject);
begin
  rve.Paste;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  RVGraphicHandler.RegisterHTMLGraphicFormat(TPNGObject);
  RVGraphicHandler.RegisterPngGraphic(TPNGObject);

end;

end.
Thank You!
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

As for PNG, call RegisterClass(TPNGObject) in FormCreate.

As for GIF, it looks like you use not the newest version of TRichView (in the newest version, GraphicHandler is defined in RVGrHandler unit, but it is not included in "uses" in your test application). Older versions of TRichView did not register TGifImage in RvGifAnimate unit.
So, either upgrade to the newest version of TRichView, or add in FormCreate:

Code: Select all

  RegisterClass(TGifImage);
  RVGraphicHandler.RegisterHTMLGraphicFormat(TGifImage);
Darken
Posts: 11
Joined: Tue Jul 22, 2014 1:30 pm

Post by Darken »

Thank you Sergey, thats is work! :)
Post Reply