Sizing Image When Pasted

General TRichView support forum. Please post your questions here
Post Reply
standay
Posts: 274
Joined: Fri Jun 18, 2021 3:07 pm

Sizing Image When Pasted

Post by standay »

Hi Sergey,

I'd like to size an image when I paste it into my rve. My SetImageAspect works fine when I insert an image from an open dialog, or when I drop it in (I put my code into after ole drop for that).

I'm using this right now:

Code: Select all

procedure TForm1.rvePaste(Sender: TCustomRichViewEdit; var DoDefault: Boolean);
begin
  if rve.CurItemStyle = rvsPicture then
  begin
    rve.BeginUpdate;
    SetImageAspect(rve);
    rve.EndUpdate;
  end;
end;
I know that rvePaste runs as I put a breakpoint into it on the "if rve.CurItemStyle = rvsPicture then" line. But it never seems to know that the photo I pasted is the current item. Is there a way to do that? Or some other way to get the item number of what was pasted in?

For now I can run it from a menu item which works fine, but be more convenient if I can get it to work when pasted.

Thanks Sergey

Stan
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Sizing Image When Pasted

Post by Sergey Tkachenko »

OnPaste occurs before pasting, not after it. So this code is executed when the image is not inserted yet.

The code may be like this

Code: Select all

procedure TForm1.rvePaste(Sender: TCustomRichViewEdit; var DoDefault: Boolean);
var
  bmp: TBitmap;
begin
  if Clipboard.HasFormat(CF_BITMAP) then
  begin
    bmp := TBitmap.Create;
    bmp.Assign(Clipboard);
    if rve.InsertPicture('', bmp, rve.DefaultPictureVAlign) then
      SetImageAspect(rve);
    DoDefault := False;
  end;
end;
standay
Posts: 274
Joined: Fri Jun 18, 2021 3:07 pm

Re: Sizing Image When Pasted

Post by standay »

Sergey,

Thanks, that worked. I don't know how you do it, I would not have come up with that in a million years!

In case it will help someone else, here's my image aspect code:

Code: Select all

procedure TForm1.SetImageAspect(rv:TRichViewEdit);
var
  h, w : integer;
  arf: Float32;
  gr: TGraphic;
  Tag: TRVTag;
  Align: TRVVAlign;
  s: TRVUnicodeString;
  rvW, rvH: integer;
begin

  if rv = nil then exit;

  if rv.CurItemStyle = rvsPicture then
  begin

    //rve WxH size limits
    rvW := rv.ClientWidth - 40;
    rvH := rv.ClientHeight - 40;

    //get image sizes
    rv.GetCurrentPictureInfo( s, gr, Align, Tag );
    w := gr.Width;
    h := gr.Height;

    //rve is "portrait" mode:
    if (rvH > rvW) then
    begin
      if w > rvW then //image is landscape
      begin
        arf := w / h;
        w := rvW;
        h := Round(Abs(rvW) / arf);
      end
      else //image is portrait
      begin
        arf := h / w;
        h := rvH;
        w := Round(Abs(rvH) / arf);
      end;
    end;

    //rve is "landscape" mode:
    if (rvW >= rvH) then
    begin
      if h > rvH then //image is portrait
      begin
        arf := h / w;
        h := rvH;
        w := Round(Abs(rvH) / arf);
      end
      else //image is landscape
      begin
        arf := w / h;
        w := rvW;
        h := Round(Abs(rvW) / arf);
      end;
    end;

    rv.SetCurrentItemExtraIntProperty(rvepImageWidth, w, True);
    rv.SetCurrentItemExtraIntProperty(rvepImageHeight, h, True);

  end;

end;
Stan
Post Reply