pre-visualize an image - c#

I'd like to pre-visualize an image in a image box before save it in a directory.
How can i do this, i use a checkbox to see if the user wants to pre-visualize or not because i dont find another way to do this without a checkbox.
I use file upload to upload the image.
string serverFileName = "";
serverFileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(MapPath("~/fotosPerfil/") + serverFileName);
i use this piece of code to save the image.

I think this post is exactly what you need to implement. Check out Ivan's solution.
Yes, indeed you can read the path selected by the user and display the image in an <img> tag, all client-side prior to uploading.

Related

SendKeys to a windows file dialog

I want to send the string ABC to the input field of a windows file dialog. With this code line I can set the focus to the correct element. I see a blinken cursor.
var filedialogOverlay = drv.SwitchTo().ActiveElement();
But the following code doesn't write the string into the element.
Thread.Sleep(1000);
filedialogOverlay.SendKeys("ABC");
EDIT:
The file upload prompt is shown by a website which I want to test. Because of black box testing I can't see the source code. Is there a tool to analyse the GUI?
When I right click the input element I get the following choices.
You can use the SendKeys.SendWait of Windows Form
//Input the file path into the filename field:
SendKeys.SendWait(longfilepath);
//Input "Enter" key
SendKeys.SendWait(#"{Enter}");
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx
If you need to upload file, try to send path to file directly to appropriate input field:
drv.FindElement(By.XPath("//input[#type='file']")).SendKeys("ABC");
P.S. If there are more than one input fields for file upload located on page, you might need more specific XPath, like "//input[#id='some_specific_file_upload']"...

itextsharp hyperlink a image and have it open in default viewer when picked

I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);

how can I upload a file via file input box using webbrowser in C#

May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.

open picture in new tab, same as if do right click - View Image in firefox (WatiN)

I need to open image in new tab exactly as it's being done by clicking right button on it and then selecting View Image in firefox. Copying pic url and using browser.Goto doesn't give me the result i need due to some tricky javascript. Could anyone give me some suggestions? Thanks
Old question, some answer for reference only.
Get the src attribute for the image you are looking for and then open a new instance of IE with that address (you will probably need to append the base website address plus whatever there is in the src attribute for the image you are looking for. Some code for that:
Image btnLogin = Window.Image(Find.ByName("TheImageNameOrAnyOtherAttribute"));
string imageAddress = btnLogin.GetAttributeValue("src");
var newBrowser = new IE();
newBrowser.GoTo("http://www.somesite.com/"+imageAddress);

how to grab an image out of a DIV with C#

is it possible to grab an image that is embedded in a div via C# and attach it to an email?
I've got the code for attaching and sending images via email, I just don't know how to get it to grab the specific item out of the div and email it.
The image varies with a previous user selection so I can't just apply a static address of an image.
any ideas?
You need to grab image from inbound email?
Read more about MIME format: all images in email message are encriped in base64 format in MIME message. To grab your image you need to parse your emailmessage, read base64 string, wich encript your image, and decoding this string like this:
var byties = Convert.FromBase64String(Body)
I would look at using the microsoft html control, read the document and use the DOM to get the url of the image, download it using the http and then attach it to your email
for reference
How to download the image
Reading the Dom from visual studio
Use HtmlAgilityPack and HttpWebRequest.

Categories