问题
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 :
- It redownload image.
- 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