Change image and image aspect ratio

General TRichView support forum. Please post your questions here
Post Reply
Lojk
Posts: 3
Joined: Fri Dec 06, 2019 1:30 pm

Change image and image aspect ratio

Post by Lojk »

Hi,

I am a new TRichViewEdit user and I am still struggling to learn how to use it properly.
The first thing I have tried to do, is to replace all images with Tag="Signature" in TRichViewEdit with new ones.

I need this, because we will use templates of documents that will include dummy images of signatures.
Those images will be later replaced with actual customers signatures images from a sign pad.
They can be differentiated form other images by the Tag="Signature".

I hope I am using the right aproach...
This is how I did it.

Code: Select all

procedure TForm3.Button2Click(Sender: TObject);
var i, itemNo : Integer;
    Item : TCustomRVItemInfo;
    sTag : String;
begin
  itemNo := RichViewEdit1.CurItemNo;
  RichViewEdit1.SetItemTag(itemNo,'Signature');

  for i := RichViewEdit1.ItemCount - 1 downto 0 do
  begin
    Item := RichViewEdit1.GetItem(i);
    sTag := TRVGraphicItemInfo(Item).Tag;
    if ((Item is TRVGraphicItemInfo) or (Item is TRVBulletItemInfo)) and
       ( sTag = 'Signature' ) then
    begin
      TRVGraphicItemInfo(Item).Image.LoadFromFile('C:\Users\Razvijalec\Desktop\audax_logo.jpg');
    end;
  end;
  RichViewEdit1.Format;
end;
The next thing I am trying to do is to set a certain image aspect ratio, but I can't find any information on how to do it.

Why I need this...
The templates I have mentioned above can be modified by the user. Text and other images can be added, and dummy signature images can be moved and stretched.
So, I have to guarantee that the dummy signatures images (not all images) will always keep the aspect I define.
That aspect ratio must be the same as of those images I get from the sign pad, otherwise the customers signatures will be deformed.

Thanks for help.

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

Re: Change image and image aspect ratio

Post by Sergey Tkachenko »

There are several problems with this code

1. Its enumerates only items of the main editor, they do not include table cells.
2. TRVBulletItemInfo is a different item type, it does not contain an image, it cannot be type-casted to TRVGraphicItemInfo.
3. You load a JPEG file to an existing graphic object. It works correctly only if this object can contain jpegs (i.e. if it is TJPEGImage)

I'll make an example in the next message.
Sergey Tkachenko
Site Admin
Posts: 17254
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Change image and image aspect ratio

Post by Sergey Tkachenko »

Code: Select all

uses
  CRVData, RVItem, RVGrHandler;

procedure TForm3.ChangeGraphicProc(RVData: TCustomRVData; ItemNo: Integer;
  var UserData1: Integer; const UserData2: TRVUnicodeString;
  var ContinueEnum: Boolean);
var
  Graphic: TGraphic;
  Name: TRVUnicodeString;
  VAlign: TRVVAlign;
  Tag: TRVTag;
begin
  if not (RVData.GetItem(ItemNo) is TRVGraphicItemInfo) or
    (RVData.GetItemTag(ItemNo) <> 'Signature') then
    exit;
  RVData.GetPictureInfo(ItemNo, Name, Graphic, VAlign, Tag);
  Graphic := RVGraphicHandler.LoadFromFile(UserData2);
  if Graphic = nil then
    exit;
  RVData.SetPictureInfo(ItemNo, Name, Graphic, VAlign, Tag);
end;

procedure TForm3.ToolButton12Click(Sender: TObject);
var
  v: Integer;
begin
   v := 0;
   RichViewEdit1.RVData.EnumItems(ChangeGraphicProc, v, 'C:\Users\Razvijalec\Desktop\audax_logo.jpg');
   RichViewEdit1.Format;
end;
Sergey Tkachenko
Site Admin
Posts: 17254
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Change image and image aspect ratio

Post by Sergey Tkachenko »

As for the aspect ratio... Do you want to reset image sizes and use the signature image size?
I modified the example to do it.
It assumes that the signature picture DPI = 300.

Code: Select all

procedure TForm3.ChangeGraphicProc(RVData: TCustomRVData; ItemNo: Integer;
  var UserData1: Integer; const UserData2: TRVUnicodeString;
  var ContinueEnum: Boolean);
var
  Graphic: TGraphic;
  Name: TRVUnicodeString;
  VAlign: TRVVAlign;
  Tag: TRVTag;
begin
  if not (RVData.GetItem(ItemNo) is TRVGraphicItemInfo) or
    (RVData.GetItemTag(ItemNo) <> 'Signature') then
    exit;
  RVData.GetPictureInfo(ItemNo, Name, Graphic, VAlign, Tag);
  Graphic := RVGraphicHandler.LoadFromFile(UserData2);
  if Graphic = nil then
    exit;
  RVData.SetPictureInfo(ItemNo, Name, Graphic, VAlign, Tag);
  RVData.SetItemExtraIntProperty(ItemNo, rvepImageWidth,
    MulDiv(Graphic.Width, RVData.GetRVStyle.UnitsPixelsPerInch, UserData1));
  RVData.SetItemExtraIntProperty(ItemNo, rvepImageHeight,
    MulDiv(Graphic.Height, RVData.GetRVStyle.UnitsPixelsPerInch, UserData1));
end;

procedure TForm3.ToolButton12Click(Sender: TObject);
var
  v: Integer;
begin
   v := 300; // picture DPI
   RichViewEdit1.RVData.EnumItems(ChangeGraphicProc, v, 'd:\Test\Checks.png');
   RichViewEdit1.Format;
end;
Lojk
Posts: 3
Joined: Fri Dec 06, 2019 1:30 pm

Re: Change image and image aspect ratio

Post by Lojk »

Thanks for the answer.

I have solved the problem of the aspect ratio when resizing with the ItemResize event.

Code: Select all

procedure TfrmDokument.rvEditItemResize(Sender: TCustomRichViewEdit;
  RVData: TCustomRVFormattedData; ItemNo, Val1, Val2: Integer);
var Item : TCustomRVItemInfo;
    orgWidth, orgHeight : integer;
    newWidth, newHeight : integer;
begin

  Item := RVData.GetItem(ItemNo);

  if (Item is TRVGraphicItemInfo) then
  begin
    if (TRVGraphicItemInfo(Item).Tag = 'Signature' ) then
    begin

      orgWidth  := TRVGraphicItemInfo(Item).Image.Width ;
      orgHeight := TRVGraphicItemInfo(Item).Image.Height ;

      newWidth  := TRVGraphicItemInfo(Item).ImageWidth ;
      newHeight := TRVGraphicItemInfo(Item).ImageHeight ;

      if (((orgWidth/orgHeight)/(newWidth/newHeight)) < 1 ) then
      begin
        newWidth  := Round(orgWidth * (newHeight / orgHeight ));
        newHeight := Round(orgHeight * (newHeight / orgHeight ));
      end
      else
      begin
        newWidth  := Round(orgWidth * (newWidth / orgWidth ));
        newHeight := Round(orgHeight * (newWidth / orgWidth ));
      end;

      RVData.SetItemExtraIntProperty(ItemNo, rvepImageWidth , newWidth);
      RVData.SetItemExtraIntProperty(ItemNo, rvepImageHeight, newHeight);

      Sender.Format;
    end;
  end;
end;
I would have done it better if I knew the old and new values of Width and Height..., but it works. :)

Bdw.In the documentation I have read:
(Val1, Val2) are:
§ for images and controls: (-1, -1).
§ for tables: (0, row index) after resizing row, or (1, column index) after resizing columns.
It think it would be realy usefull if for the images and controls that Val1, Val2 would contain the old width and height of the item.


Now I have an additional question.

I have added the signature picture to the RichViewEdit and saved it as *.rtf .
Then I loaded that file and noticed that some properties I have set to the picture are missing. Tag, Name ...

Then I've looked at the rtf file content, and noticed that only some data of the picture is saved
\par {\pict\picwgoal4800\pichgoal2400\dibitmap0\wbmwidthbytes320\picw320\pich160 28000000400100....
So, is there a way to save properties like tag, name.. of the image in the rtf file ?

If not ...
At : http://www.biblioscape.com/rtf15_spec.htm#Heading49
In the picture syntax specification I found :
\bliptagN A mostly unique identifier for a picture, where N is a long integer value
Can this property be saved and loaded between RichViewEdit and a *.rtf file?


Thank you once more for your help.

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

Re: Change image and image aspect ratio

Post by Sergey Tkachenko »

I implemented your requests in TRichView 18.2, partially.

1) In OnItemResize event, old item sizes are provided.
2) There is OnWriteObjectProperties event where you can store an image identifier in RTF. But loading is not implemented yet, it will be in the next update. Note: do not use zero or small positive values, otherwise MS Word would display images incorrectly (I do not know why, reserved values of \bliptag are not documented); so I suggest using negative values.
Lojk
Posts: 3
Joined: Fri Dec 06, 2019 1:30 pm

Re: Change image and image aspect ratio

Post by Lojk »

I have another question regarding the image in this topic.

TheTSRichVewEdit containing the image is set for A4 format.
..PageProperty.PageFormat := srvfmA4;

Is there a way to get the position (coordinates Top, Left ...) of the image , if we know its ItemNo.
Sergey Tkachenko
Site Admin
Posts: 17254
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Change image and image aspect ratio

Post by Sergey Tkachenko »

See:
- GetItemBounds and GetItemBounds100
- GetItemCoords100

See also ScaleRichView demos in <TRichView Dir>\ScaleRichView\Demos\Delphi\CustomDraw\Rectangles\
Post Reply