0

I am having a WebBrowser control and loaded a local image into an IMG tag and set the DocumentText of the WebBrowser as below

<!DOCTYPE HTML>
<meta http-equiv='X-UA-Compatible' content='IE=10' />
<html lang='en'>                                    
<body style='margin: 0; overflow: hidden;'>
<img Width = 1269 Height = 1600 src='C:\Users\ADMIN\Desktop\p41.png'></img>
</body>
</html>

It is loading perfectly fine. For some reason, I want to invoke the context menu of the image and click one of the context menu items programmatically (say for example "Copy" - though I don't want this - I can choose any item in the menu). I don't know how to achieve this. When I tried to iterate the context menu of the browser control, I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.' as I have not placed any ContextMenu control in the form, since I wanted to access the WebBrowser's inbuilt context menu. Is this achievable ?

Or is there a way to get the code (which IE does) for each of its menu item, so I can directly call that - atleast for "Save picture as..." and "Copy"

Please Note: I do not want to use the "SRC" attribute to be part of any solution as I do not want that as you see I am the one forming the HTML tag with that. Hence I know that.

Please find the screenshot below

enter image description here

bkar81
  • 25
  • 7
  • 1
    Add the missing ` ` block. Change your `` tag in `` and place it inside the `` block. Right-click on the Image, linked as `src='file://C:/Users/ADMIN/Desktop/p41.png'`. Save the image. – Jimi Jan 19 '21 at 21:19
  • I `don't want to manually` right click on the image. As I mentioned, it is loading correctly without any issues, but I want to programmatically / automatically do it without human intervention – bkar81 Jan 19 '21 at 21:23
  • 1
    [IWebBrowser2 interface](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752127(v=vs.85)) -> [ExecWB](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752087(v=vs.85)) or [IHTMLDocument2.execCommand](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752577(v=vs.85)) -> [IDM_COPY](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/hh801226(v=vs.85)#idm_copy) – Jimi Jan 19 '21 at 22:07
  • I don't remember whether the Activex allows programmatic access to the clipboard. – Jimi Jan 19 '21 at 22:10
  • Sadly, I am unable to understand those concepts :( – bkar81 Jan 20 '21 at 12:31
  • I want to Invoke / Mimic mainly the "Save picture as..." menu item without manually doing it as I am working on an automation stuff - for that I am badly looking for that feature. Any help would be greatly appreciated. Thank you in advance – bkar81 Jan 20 '21 at 12:34
  • Do you *need* to use the context-menu function to save? Another idea would simply be to scrape the source for image files (or the file your looking to download), perhaps with a regex, or simply the DOM, and either use the save file dialog or just save it the normal way you'd save a file. – J. Scott Elblein Jan 20 '21 at 12:48
  • The IMG tag can be used to place any image file and also SVG files. So if I place any of these files inside the IMG tag, then we would be able to save it as PNG/BMP. This is what I am actually wanted to do... By this way, I can convert all my SVG and other images to PNG format. Can this be doable in any other way? One such way I found is using the WebBrowser control and load the image and manually clicking on "Save picture as...". But I want the last step to be automated too.. Any help would be deeply appreciated... Please let me know if you need clarifications – bkar81 Jan 20 '21 at 19:09
  • @Jimi I found a good piece of info about ExecWB in the below thread. But it all works on the main browser (or the control) and there is no option to Invoke the ContextMenu of the browser https://stackoverflow.com/questions/38977399/microsoft-internet-controls/38978463#38978463 – bkar81 Jan 20 '21 at 19:12
  • What has the ContextMenu to do with anything? Do you want to open up a menu or copy the current selection? The menu is for Users, to select a function to execute. – Jimi Jan 20 '21 at 19:32
  • Actually whatever I am loading inside the IMG tag (be it SVG / PNG / anything) ` ` I have taken care of changing the image source automatically every few seconds, but after displaying the content, it has to automatically save that as PNG without manually doing anything (as of now I am manually saving with WebBrowser contextmenu) – bkar81 Jan 20 '21 at 20:24
  • Any help on this please? – bkar81 Jan 27 '21 at 20:46

0 Answers0