Awesomium how save a loaded image without redownload it

廉价感情. 提交于 2019-12-23 04:44:05

问题


How can copy or save a loaded image inside Awesomium browser to local disk without redownload it?

There is a method for download an image :
http://docs.awesomium.net/html/M_Awesomium_Windows_Controls_WebControl_SaveImageAt.htm

But it has two problems :

  1. It redownload image.
  2. It shows a pop-up to save image and i don't want it.

Also every way in javascript redownloads goal image!
What is the solution?
How can we grab the downloaded image from cache folder?


回答1:


You can try something like this:

    /// <summary>
    /// Returns Base64 string of image content. Without redownloading image. If it is not on the same domain, WebSecurity must be set to false.
    /// </summary>
    /// <param name="getElementQuery">
    /// <para>Any Javascript query to get an element.</para>
    /// <para>Examples:</para>
    /// <para>"document.getElementById('id')"</para>
    /// <para>"document.getElementsByClassName('name')[0]"</para>
    /// <para>"document.evaluate(\"//a[contains(@href, 'something')]\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue" (XPath)</para>
    /// </param>
    /// <param name="leaveOnlyBase64Data">if true, removes "data:image/type;base64," from the beginning of the string</param>
    /// <returns>Base64 string or empty string in case of error</returns>
    public static string JsGetImgBase64String(string getElementQuery, bool leaveOnlyBase64Data = true)
    {
        string data = webView.ExecuteJavascriptWithResult(@"
                                        function getImgBase64String(img)
                                        {
                                            var cnv = document.createElement('CANVAS');
                                            var ctx = cnv.getContext('2d');
                                            ctx.drawImage(img, 0, 0);
                                            return cnv.toDataURL();
                                        }
                            " + String.Format("getImgBase64String({0});", getElementQuery));

        if (data == "undefined")
            return string.Empty;

        if (leaveOnlyBase64Data && data.Contains(","))
        {
            data = data.Substring(data.IndexOf(",") + 1);
        }

        return data;
    }

Usage:

 string imgBase64 = JsGetImgBase64String("document.getElementById('imageId')");
 byte[] imgBytes = Convert.FromBase64String(imgBase64 );

If the image is not on the same domain you may need to set WebSecurity to false in WebPreferences.



来源:https://stackoverflow.com/questions/31654964/awesomium-how-save-a-loaded-image-without-redownload-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!