Selenium copy image to Base64 string? - image

How can I get an image on a webpage with Selenium and encode it to a Base64 string which gets added to a variable? I'm using Selenium C# but any language will probably work.

I am not quite sure what you are asking. What do you mean by "get an image on a webpage"? Do you mean:
grab a screenshot of your page and compare it with some given value? or
take a screenshot of specific element on webpage?
download an image contained in (ie) <img> tag and do something with it?
For taking screenshots, it is widely disucessed here. Although mostly java solutions, they probably could be ported to C# with ease. If what you need is nr 3, then get the URL (ie using xpath //img[#id=\"yourId\"]#src ) and download it using something like WebClient and convert that to base64:
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var baseString = System.Convert.ToBase64String(plainTextBytes);

This code will helps you, I am using it for my own report, instead of storing report in seperate location, better to convert into base64 form and add it to report.
String Base64StringofScreenshot="";
File src = ((TakesScreenshot) driverThread).getScreenshotAs(OutputType.FILE);
byte[] fileContent = FileUtils.readFileToByteArray(src);
Base64StringofScreenshot = "data:image/png;base64,"+Base64.getEncoder().encodeToString(fileContent);

Related

convert pdf to html using abcpdf

i am looking for a method to convert a pdf document into corresponding html document using abcpdf. kindly let me know if it is feasible. FYI, My pdf document has rich text along with images.
You can. Try this. Hopefully it'll work.
var doc = new WebSupergoo.ABCpdf10.Doc();
doc.Read('your Pdf byte array');
doc.Save('your HTML file path with .html extension');
doc.Clear();
doc.Dispose();
For documentation please have a look at the note section
http://www.websupergoo.com/helppdfnet/source/5-abcpdf/doc/1-methods/save.htm
To export as XPS, PostScript, DOCX or HTML you need to specify a file path with an appropriate extension - ".xps", ".ps", ".docx", ".htm", ".html" or ".swf". If the file extension is unrecognized then the default PDF format will be used.
You can definitely convert HTML to PDF, but I am not sure the inverse is possible to do with abcpdf.
Perhaps you can give a try to iText (iTextsharp)

Preload the Image in the webbrowser of windows phone

I'm designing a news-reading app and using the NavigateToString() of a webbrowser to show some string. Now I want to implement the off-line reading function, the html string had already been downloaded, except the image.
Windows phone has implemented the image-cache function, once the Image is requested, it had been cached. But now problem is that, all the html strings are stored in a array, some of those html strings hadn't been shown throught the navigateToString(), namely the imgs in those string couldn't show up if the internet is disconnected.
So I'm wondering how to cache the imgs in the webbrowser?
thanks,
ellic
It ain't gonna be easy or pretty.
Here's one thing you can do...
Parse the HTML using something like the HtmlAgilityPack. Linq should be a good solution for finding all the images. If you want to use xpath, you can do that too.
Find all Images in the HTML and use a WebClient (or better yet, a WebRequest) to fetch them.
Save them as part of your database that you use to store your HTML strings (say, Base64 encoded if your medium needs to be strings, key'ed by the URI to the image);
When you want to display the offline HTML, again, parse the HTML and replace all references to images with the data protocol equivalent of your cached images.

Grails - Loading image from the controller

I am trying to retrieve zip file from FTP, unzip it, and get xml file and image file from them and parse the xml, display the contents of xml and image.
byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser,
customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory,
fileName)
FileOutputStream fos1 = new FileOutputStream("zip.img")
try {
fos1.write(image);
} finally {
fos1.close();
}
return [
command: this,
fileName: fileName,
applicationName: applicationName,
contentProvider: contentProvider,
operatingSystem: operatingSystem,
handSets: handSets,
zipImg:"zip.img" ]
I could finish the xml part successfully and image also I am able to retrieve from the zip in a byte format( i could convert it to a file using file outputstream),
Now I am stuck in sending the image to gsp and to display that. Any inputs are much appreciated.
Thanks
If you want to use the image only once, meaning it should always be extract from the zip file, then embedding the img in base64 format into the webpage is a good option here because you don't need to worry about the image file after sending that base64 encoding value to gsp.
If you still need that image file to be used by other http requests then you should extract the images to a folder and send the list of img paths to gsp.
You can either
point img src="${g.createLink(action: 'a', params: [p: p])}" to some other action (with createLink) that will cache the image on your server side,
or embed it right into HTML, like in this question.
Browsers can render byte arrays if you specify the format.
Having a variable image in the model sent to the gsp of type byte[], this is the way to render it HTML:
<img src="data:image/png;base64,${image.encodeBase64()}"/>
You also need to specify if it's image/png or other format.

FCKeditor: displays the formating characters along with text

have a form that uses FCKeditor. I can input with formatting, but when I bring back what I put in FCKeditor it also displays the raw html format syntax. I.E. <p><p>&
Question: is there a setting I'm missing that uses the formatting to format the text instead of displaying the formatting syntax along with the text?
thanks
Randy
i dont know whether you are using ASP.net C# or not but if yes then first import
using System.Text.RegularExpressions;
using FredCK.FCKeditorV2;
these two things and then where you are retrieving your value from fckeditor then use
string fckContent = Regex.Replace(FCKEditorID.value, #"<(.|\n)*?>", string.Empty);

Adding external images to PDF using iText

I'm having trouble figuring out how to add an external image (referenced by a URL) to a PDF using iText. Is this kind of thing possible?
The PDF spec in 7.1.5 says you should be able to reference a PDF via a URL by using a URL specification. This is what I've got so far:
PdfFileSpecification pdfSpec =
PdfFileSpecification.url(writer, "http://www.someurl.com/test.jpg");
StringBufferInputStream sbis = new StringBufferInputStream("");
PdfStream dict = new PdfStream(sbis, writer);
dict.put(PdfName.FILTER, PdfName.DCTDECODE)
dict.put(PdfName.TYPE, PdfName.XOBJECT);
dict.put(PdfName.SUBTYPE, PdfName.IMAGE);
dict.put(PdfName.WIDTH, new PdfNumber(100));
dict.put(PdfName.HEIGHT, new PdfNumber(100));
dict.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
dict.put(PdfName.LENGTH, new PdfNumber(0));
dict.put(PdfName.F, pdfSpec);
PdfIndirectObject img = writer.addToBody(dict);
I know I still need to make sure the color space is added and stuff, but my main concern right now is actually getting this image into the body of the document. I can't figure out how to do this... it seems I can't get a reference to a PdfPage or the resources dictionary or anything. Is this possible using iText?
As a side note, this exercise is useless if I'm going to be presented with a security warning when the view tries to go load the image. Does anyone know if that is the case?
External content is described in the PDF spec, but almost no PDF processor does actually support them. By now Acrobat 9 has support for it, but I would be very cautious with that feature: Your clients or users may not be able to see the referenced content.

Resources