Recent

Author Topic: Inserting, Saving and Loading Images  (Read 27067 times)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Inserting, Saving and Loading Images
« Reply #15 on: September 15, 2016, 05:31:40 am »
Let's give r5163 a try.

In order to resize an object two additional functions were introduced in Win32RichMemo module:
* GetRichEditOLE - the function returns IRichEditOle interface. It is used in the next function
* SetOleObjectSize - the function sets the size of an object at the given cursor position. (If there's no object under the position, i.e. text only, the function returns false). The Size is specified in Points (1/72 of an inch).
For your convenience, there's also
* GetOleObjectSize - the function returns the current size of an object in points.

Here's an example of how to shrink the currently selected object twice:
Code: [Select]
var
  ole : IRichEditOle;
  sz : TSize;
begin
  ole:=GetRichEditOLE(RichMemo1);
  GetOleObjectSize(ole, RichMemo1.SelStart, sz);
  sz.cx:=(sz.cx div 2);
  sz.cy:=(sz.cy div 2);
  SetOleObjectSize(ole, RichMemo1.SelStart, sz);     

it just occurred to me... I am doing additional inserts. It may mean that the insert code needs a OLE callback.
Now flags for an inline object inserted can be controlled from your code.
Win32RichMemo is exposing InlineInsertFlag variable. The flag matches values of dwFlags field in REOBJECT structure. MSDN documentation is here..

Values are declared at Win32RichMemoProc unit.

If you want to make any image inserted via InsertInline resizable you want to add the following line into your code.
Code: [Select]
uses ... Win32RichMemo, Win32RichMemoProc;


  InsertInlineFlags:=REO_RESIZABLE;
« Last Edit: September 15, 2016, 05:34:00 am by skalogryz »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Inserting, Saving and Loading Images
« Reply #16 on: September 15, 2016, 05:42:21 am »
WARNING: InsertInlineFlags variable might (and likely will be) removed in future revisions of RichMemo. More likely when "inlines" interfaces will be stable, official and truly cross-platform.

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #17 on: September 15, 2016, 02:13:37 pm »
Just tried r5163.

When opening a file that was previous to this version, the image can be resized by dragging its grips.

Images inserted by this version have the grips, but dragging a grip will resize its box and not resize the image.

The only code that I have implemented at this point is: InsertInlineFlags:=REO_RESIZABLE;

Code: Pascal  [Select][+][-]
  1. procedure TCmdForm.MnuDiskImageClick(Sender: TObject);
  2.   var
  3.     pic : TPicture;
  4.     ip  : TRichMemoInlinePicture;
  5.     sz  : TSize;
  6.   begin
  7.     OpenDialog1.Title:= 'Select Image-File';
  8.     OpenDialog1.Filter:= 'Jpeg (*.jpg)|*.jpg|Ping (*.png)|*.png|Tiff (*.tif)|*.tif';
  9.     OpenDialog1.FileName:= '';
  10.     if OpenDialog1.Execute then
  11.        begin
  12.        pic:=TPicture.Create;
  13.        try pic.LoadFromFile(OpenDialog1.FileName);
  14.            except on e:Exception do
  15.                   begin
  16.                   pic.Free;
  17.                   ShowMessage('unable to read image file: '+OpenDialog1.FileName+#13#10+e.Message);
  18.                   Exit;
  19.                   end;
  20.            end;
  21.        ip:=TRichMemoInlinePicture.Create(pic);
  22.        sz:=Types.Size(round(pic.Width*72/96), round(pic.Height*72/96));  //  Types.Size due to unit conflict
  23.        InsertInlineFlags:=REO_RESIZABLE;      // ***test placement***
  24.        PageMemo.InDelInline(ip, PageMemo.SelStart, 0, sz);
  25.        end;
  26.   end;
  27.  

Does it need to be elsewhere?
And shouldn't the drag-grip to resize still operate?

Rick
 
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #18 on: September 15, 2016, 02:19:09 pm »
I also saved the file with the new inserts. When I opened the file the images were not there.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #19 on: September 15, 2016, 03:00:35 pm »
I relocated the InsertInlineFlags:=REO_RESIZABLE to the top of my function code (just before the picture.create.

It operated the same way. Drag box resized, but the image did not. The image couldn't be saved with the file.

I then removed the InsertInlineFlags:=REO_RESIZABLE and the image was not selectable, nor could it be saved with the file.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #20 on: September 16, 2016, 01:15:29 pm »
I tried a coded resizing for the image to see if it changed the situation.

To do so I had to change the type of IRichEditOle to WIN32RICHMEMOPROC.IRichEditOle due to a naming conflict.

The resizing worked, but with the same results... the frame resized, but the image did not.

It also crashed on closing the program. It went to an Assembler error. CPM is still alive!

Code: Pascal  [Select][+][-]
  1. procedure TCmdForm.MnuDiskImageClick(Sender: TObject);
  2. var
  3.   pic : TPicture;
  4.   ip  : TRichMemoInlinePicture;
  5.   ole : WIN32RICHMEMOPROC.IRichEditOle; // IRichEditOle;
  6.   sz  : TSize;
  7.   x   : longint;
  8. begin
  9.   OpenDialog1.Title:= 'Select Image-File';
  10.   OpenDialog1.Filter:= 'Jpg (*.jpg)|*.jpg|Png (*.png)|*.png|Tif (*.tif)|*.tif|Bmp (*.bmp)|*.bmp';
  11.   OpenDialog1.FileName:= '';
  12.   if OpenDialog1.Execute then
  13.      begin
  14.      pic:=TPicture.Create;
  15.      try pic.LoadFromFile(OpenDialog1.FileName);  // load image file
  16.          except on e:Exception do
  17.                 begin
  18.                 pic.Free;
  19.                 ShowMessage('Unknown Image Format: '+OpenDialog1.FileName+#13#10+e.Message);
  20.                 Exit;
  21.                 end;
  22.          end;
  23.  
  24.      InsertInlineFlags:= REO_RESIZABLE; // test placement
  25.  
  26.      ip:=TRichMemoInlinePicture.Create(pic);  // Screen.PixelsPerInch typically = 96
  27.      sz:=Types.Size(round(pic.Width*72/Screen.PixelsPerInch), round(pic.Height*72/Screen.PixelsPerInch));  //  "Types.Size" due to unit conflict
  28.      PageMemo.InDelInline(ip, PageMemo.SelStart, 0, sz);
  29.  
  30.      ole:=GetRichEditOLE(PageMemo);              // test resizing
  31.      GetOleObjectSize(ole, PageMemo.SelStart, sz);
  32.      sz.cx:=round(sz.cx * 1.25);  // magnify
  33.      sz.cy:=round(sz.cy * 1.25);
  34.      SetOleObjectSize(ole, PageMemo.SelStart, sz);
  35.  
  36.      end;
  37. end;  
  38.  

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #21 on: September 16, 2016, 01:35:04 pm »
I found the naming conflict. I still had the engkin unit files in my design folder. I deleted them, but it did not change any of the results.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #22 on: September 19, 2016, 05:00:24 pm »
Is it possible that the image needs an align=alClient setting?

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Inserting, Saving and Loading Images
« Reply #23 on: September 19, 2016, 05:18:16 pm »
do you mean you want an image to be always at the length of the page?

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #24 on: September 19, 2016, 05:41:20 pm »
No. Rather that it would fit the container box built by the callback procedure. The box with grips for resizing.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Inserting, Saving and Loading Images
« Reply #25 on: September 19, 2016, 06:55:05 pm »
No. Rather that it would fit the container box built by the callback procedure. The box with grips for resizing.
Well, you should be able to achieve this by specifying the size of the image you're trying to insert. There's nothing a callback could do about it.

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #26 on: September 19, 2016, 07:05:41 pm »
Please allow me to repeat the problem...

Images that are already in an RTF file (placed by another editor) show fine and are resizable. The same file saved can be opened and are visible and resizable.

But images inserted from a file using InDelInline have the resize box, and it resizes by code and also dragging grips, but the image does not resize with the box.

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.  
  4.   { TRichMemoInlinePicture }
  5.  
  6.   TRichMemoInlinePicture = class(TRichMemoInline)
  7.     pic: TPicture;
  8.     constructor Create(Apicture: TPicture);
  9.     procedure Draw(Canvas: TCanvas; const ASize: TSize); override;
  10.   end;  
  11.  
  12. implementation
  13.  
  14. {$R *.lfm}
  15.  
  16. { TRichMemoInlinePicture }
  17.  
  18.   constructor TRichMemoInlinePicture.Create(Apicture: TPicture);
  19.   begin
  20.     inherited Create;
  21.     pic:=APicture;
  22.   end;
  23.  
  24.   procedure TRichMemoInlinePicture.Draw(Canvas: TCanvas; const ASize: TSize);
  25.   begin
  26.     inherited Draw(Canvas, ASize);   //** was omited
  27.     Canvas.Draw(0,0, pic.Graphic);
  28.   end;  
  29.  
  30. procedure TCmdForm.MnuDiskImageClick(Sender: TObject);
  31. var
  32.   pic : TPicture;
  33.   ip  : TRichMemoInlinePicture;
  34.   sz  : TSize;
  35.   x   : longint;
  36.  
  37. begin
  38.   OpenDialog1.Title:= 'Select Image-File';
  39.   OpenDialog1.Filter:= 'JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIF (*.tif)|*.tif|BMP (*.bmp)|*.bmp';
  40.   OpenDialog1.FileName:= '';
  41.   if OpenDialog1.Execute then
  42.      begin
  43.      pic:=TPicture.Create;
  44.      try pic.LoadFromFile(OpenDialog1.FileName);  // load image file
  45.          except on e:Exception do
  46.                 begin
  47.                 pic.Free;
  48.                 ShowMessage('Unknown Image Format: '+OpenDialog1.FileName+#13#10+e.Message);
  49.                 Exit;
  50.                 end;
  51.          end;
  52.  
  53.      InsertInlineFlags:= REO_RESIZABLE;
  54.      ip:=TRichMemoInlinePicture.Create(pic);
  55.      //  Types.Size ...due to unit conflict
  56.      sz:=Types.Size(round(pic.Width*72/Screen.PixelsPerInch),
  57.                     round(pic.Height*72/Screen.PixelsPerInch));
  58.      PageMemo.InDelInline(ip, PageMemo.SelStart, 0, sz);
  59.  
  60.      (*
  61.      ole:=GetRichEditOLE(PageMemo);                // **test resizing**
  62.      GetOleObjectSize(ole, PageMemo.SelStart, sz);
  63.      sz.cx:=round(sz.cx * 1.5);  // magnify
  64.      sz.cy:=round(sz.cy * 1.5);
  65.      SetOleObjectSize(ole, PageMemo.SelStart, sz);
  66.      *)
  67.  
  68.      end;
  69.  

I have included in the above the global type for the unit, and its implementation settings. It is code that goes back to when we were first testing an inline insert. Maybe it is conflicting with things.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Inserting, Saving and Loading Images
« Reply #27 on: September 19, 2016, 07:12:42 pm »
try this code:
Code: [Select]
procedure TRichMemoInlinePicture.Draw(Canvas: TCanvas; const ASize: TSize);
  begin
    inherited Draw(Canvas, ASize);   //** was omited
    Canvas.StretchDraw( bounds(0,0, ASize.cx, ASize.cy), pic.Graphic);
    //Canvas.Draw(0,0, pic.Graphic);
  end;

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #28 on: September 19, 2016, 07:16:45 pm »
I just got here. I am attaching a PNG of the application when doing an inline insert. I will try your code changes.

Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Inserting, Saving and Loading Images
« Reply #29 on: September 19, 2016, 07:21:26 pm »
Yes, the code change made it work... thank you. I never would have tracked it down. I was looking at everything else.

Rick

Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

 

TinyPortal © 2005-2018