BeforePasteImage event question

  • In WPT5, the BeforePasteImage event was not triggered if pasting a linked image. However, in WPT9 it does. This is actually a good thing so I'm not complaining. However, is there an easy way to know if I'm pasting a linked image vs a non-linked image in that event?

    FYI: I allow a non-linked image ONLY if it was from a signature capture (I know this by using a flag), otherwise I do not allow a non-linked image to be inserted. For linked images, I keep track of how many times it is referenced (not just in the currently loaded WPRIchText, but in the database for the combined report comment set). I need to keep track in order to know whether to place a thumbnail of it in the either the used or unused image panel (I did not do this in my old app that used WPT5). So knowing that a user is coping/pasting a linked image is important to me and where pasting a non-linked image is not important (and not allowed).

  • This event is triggered before an image was pasted into the text. It will be also triggered for each image which is part of an RTF stream.

    The property InsertedObj is assigned when the event BeforePasteImage has been triggered for images which are part of pasted RTF text.

    If an image was pasted as object loaded from the clipboard, InsertedObjwill be nil since the event happens before the bitmap is actually inserted into the text.

    This means - you can check the property WPRichText.InsertedObj if <>nil and StreamName<>''

  • OK, except in my quick test:

    1. When inserting an image copied from a web page (Right click, copy image), InsertedObj is not nil, but ObjRef and TxtObj are both nil (using Ctrl+V to paste it).

    2. When inserting a signature image, InsertObj is nil and TxtObj is not (using WPRichText1.PasteFromClipboard to paste it)

    3. When inserting text that includes a linked image copied (Ctrl+C) from another WPRichText, InsertedObj, ObjRef and TxtObj are all not nil. (using Ctrl+V to paste it)

    4. When copying just the image from another WPRichText (Ctrl+C), pasting it (Ctrl+V) into another WPRichText is treated as a non-linked image (same as 1 above)

    So how do I then stop the non-linked image (1) from being inserted such as pasting an image grabbed from a web page? And why is pasting using Ctrl+V different than using WPRichText1.PasteFromClipboard?

    Actually, It would be even better if I could intercept the non-linked image being pasted and convert it into a linked image by pulling it into my database (this way I could scale it, add borders etc as the user configured it).

    (I thought setting TxtObj to nil would do it but it is already nil... so much for my great testing in my old version since that is what I was doing there but just saw that it didn't work there either for all these years, lol).

  • Post by ehimmer ().

    This post was deleted by the author themselves: moved to previous post ().
  • Please note that BeforePasteImage is triggered also for the images which are embedded in a text which has been pasted. This is something completely different to pasting just a bitmap. Only if a bitmap is pasted textobj can be used and freed. In this case InsertedObj is nil because the image has not yet been inserted. I don't think it can ever be linked.

    If text is inserted you can also use the BeforePaste event to scan for images and delete if you prefer that they are not copied.

  • BeforePaste event?

    Note also that I added item 4 to my previous post before seeing your post. It seems even copying just the linked image it is pasted as a non-linked image, i.e. only pasted as a linked image if it was part of text.

    If I cannot convert a non-linked image to a linked image, I would prefer to not allow it to be inserted or to immediately remove it to prevent customer confusion.

  • I replaced your WPAPaste1 action with my own Paste action and have it almost exactly like I need it. It checks to see what is being inserted and skips it if it is just an image. This does prevent an image I copied from a web page from being pasted, but still allows a linked image to be pasted, whether or not it is in conjunction with with text.

    However, when pasting just a linked image without text, both TxtObj and WPRichText1.InsertedObj.ObjRef are nil in the BeforePasteImage event, and WPRichText1.InsertedObj.Source is blank, so I do not know the name of the source to be able to manage it in my thumbnail panels. Yet it still seems to insert it as a linked image because if I look at the RTF or reload the WPRIchText, they all seem to be correct as a linked image. If I pasted that linked image along with surrounding text, TxtObj, WPRichText1.InsertedObj.ObjRef and WPRichText1.InsertedObj.Source are all available and I can get the name that I need. Is there an issue there? I really need that source, stream or file name.

    I also noticed that it hits the RequestHTTPImage event just before the BeforePasteImage event, so it seems to know what is being pasted because the Source is as expected.

    This is what I added to my paste action:

    1. if fActiveEditor = nil then exit;
    2. if not (
    3. ClipBoard.HasFormat(CF_METAFILEPICT) or
    4. ClipBoard.HasFormat(CF_ENHMETAFILE) or
    5. ClipBoard.HasFormat(CF_BITMAP) or
    6. ClipBoard.HasFormat(CF_PICTURE) ) then
    7. begin
    8. fActiveEditor.PasteFromClipboard;
    9. end;
  • Sorry, I do not understand what problem you need to solved. Show Thumbnail?`

    A linked image will trigger the OnRequestHTTPImage event, that should also happen when pasting not just loading.

    1.   TWPRequestHTTPImageEvent = procedure(RTFData: TWPRTFDataCollectionBase;
    2.     Reader: TWPCustomTextReader; const LoadPath, url: string;
    3.     TextObject: TWPTextObj; var Ok: Boolean) of object;

    There are other methods to detect if you are in the paste operation while processing this event, but checking WPRichText.Memo._InClipboardOP=true is probably the easiest.


    Actually, It would be even better if I could intercept the non-linked image being pasted and convert it into a linked image by pulling it into my database (this way I could scale it, add borders etc as the user configured it).

    So you can use that event to avoid linked images if that is the purpose.

  • Sorry, sometimes I tend to say too much that clouds the real issue.

    Consider this... you have two WPRichTexts side by side. The one on the left has some text and a linked image. If you select just the linked image, copy it (using Ctrl+C) and paste it (using Ctrl+V) into the WPRichtext on the right, the TxtObj in the BeforePasteImage is nil (bad for me). However, if you select any of the text along with the linked image on the left then copy and paste it into the WPRichText on the right, TxtObj in BeforePasteImage is NOT nil, which is good because I can extract the image name from it.

    So it seems just copying and pasting a linked image by itself is not performing correctly.

  • Quote

    So it seems just copying and pasting a linked image by itself is not performing correctly.

    A linked image is not pasted as bitmap but as text. The event is triggered after the text was loaded, the code which does it was recently updated on 20.6.. Do you use the latest 9.1?

  • I am using the July 21 version. I see you now have and update from July 24 and will try that.

    But yes, I understand linked image is pasted as text. I am trying to point out that just selecting the image does not work the same as if I selected more than just the image for copy/paste. Both paste as linked images fine, I have no issue with that and is perfect, however in BeforePastImage event the TxtObj parameter is nil when just the image had been selected and not nil when more than the image had been selected. I would hope it would be not nil in both cases.

    I will check with the latest and if TxtObj is still nil, I will send you an example. Maybe it will be more clear.

  • Updated made no difference. I'll send you an example if this doesn't help...

    When pasting a linked image with or without surrounding text, it gets put onto the FProcessImages correctly in the TWPToolsReaderWriter.AddProcessedImage:

    1. (50, wpobjlmage, S1DB94040, [], [], S1DBD3050, -1, '5295_Highway_17_Canon_Ga_021.jpg', '5295_Highway_17_Canon_Ga_021.jpg', ", ", nil, nil, ", 0, 0, 0,4320, 3352, (nil,nil), (nil.nil), (nil, nil), 0, 0, wpwrAutomatic, nil, [])

    However, if you only copied/pasted the linked image without surrounding text, then in TWPCustomRtfEdit.PasteFromClipboard's NowLoadFromStream procedure it looks like this when pulled from FProcessImages using the line anImageObj := Reader.ProcessedImageObj(i) and ultimately ends up this way in WPRichText1.InsertedObj and as such makes TxtObj nil in WPRichText1BeforePasteImage:

    1. (0, wpoifbjCustom, nil, [], [], nil, 0, ", ", ", ", nil, nil, ", 0, 0, 0, 0, 0, (nil,nil), (nil,nil), (nil,nil), 0, 0, wpwrAutomatic, nil, [])

    But when pasted with more than just the linked image it correctly pulls it like this (just as it was put onto it) and all is good:

    1. (50, wpobjlmage, S1DB94040, [], [], S1DBD3050, -1, '5295_Highway_17_Canon_Ga_021.jpg', '5295_Highway_17_Canon_Ga_021.jpg', ", ", nil, nil, ", 0, 0, 0,4320, 3352, (nil,nil), (nil.nil), (nil, nil), 0, 0, wpwrAutomatic, nil, [])

    In trying to help by tracing thru it, I cannot find where it gets lost or corrupted when just copy/pasting a linked image. Seems to start out the same, but ends up completely different.

  • I gave up. I tried to track it down for you, but for now I went with your suggestion to use WPRichText.Memo._InClipboardOP in the RequestHTTPImage event to keep track of the image usage and seems to do what I need. So thanks for that suggestion!

    I think, in your free time (ha), you should investigate why the TextObject is being reset to an initial state. I even tried just saving the WPTextObj in the RequestHTTPImage event (i.e. savedTextObj := TextObject) to see what happens, and sure enough in the BeforePasteImage event savedTextObj was in an initial state. So something is resetting the actual text object itself when pasting only a linked image vs more than a linked image.