简体   繁体   中英

How to convert IHTMLImgElement to image

I am automating Internet Explorer using SHDocVW.dll and MSHTML with C#, and I wish to save an image from the page to the disk (JPEG format).

I can't use the WebClient class to download the image; if I do it, I end up downloading the site's login page. I can't print the screen either, because the browser has to remain invisible during this process, running in the background.

I have tried to do the following:

IHTMLImgElement imgElement = ...;
IHTMLControlRange imgRange = ...;

imgRange.add(imgElement as IHTMLControlElement);
imgRange.execCommand( "copy", false, null );

This does nothing. I am not able to extract anything from the clipboard. Every solution I found didn't work for me.

Your webclient approach is probably missing cookies... see How do I log into a site with WebClient? for an example that handles cookies.

your code looks fine except the user has to change the security setting to enable clipboard access. If the image is cached on disk you can dig the WinInet cache after parsing the page for the image location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM