Image Preview from on click event c# asp .net - c#

I'm currently trying to look at a directory, and then preview a .jpeg from a list box. I have the list box populating with the contents of the directory and only showing Jpegs, but I can't think of what to do to get the jpeg preview in a picture box. I'm using an asp .net application on Visual Studio 2010.
This is the code I have
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo infoDir = new DirectoryInfo(#"G:\Test_Directory");
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name);
}
}
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I'm under the understanding Postback needs to be used. If anyone is able to help, that would be great.
The file which is in the G: Drive, is a jpeg, which can be seen in the list box is : jpegimage.jpeg
Thanks.

How about something like this?
I think you could do this mostly in Javascript, with two additional ASP.NET page.
First, create a new web page. We'll call this A.aspx. This page will be passed the image name in the query string. It will be very simple: it will just fetch the contents of the file from "G:\TestDirectory" and write it to the Response stream. There are quite a few questions and answers on Stack Overflow on how to do this, if you haven't done it before.
Then, create another web page. We'll call this B.aspx. This will have an image control with height and width set appropriately. It will also take the image name from its query string. The code-behind will build a URL to use as the ImageSource property on the image control. The URL will be that of A.aspx, with the (URL-encoded) image name appended as a parameter.
On your ASP.NET page, hook up an event handler to your listbox. When the selected index on the list box changes, on the client side, build a URL, based on the URL to B.aspx with the image name from the list box appended as a parameter. Then open a window, using the URL you just built, pointing to B and passing the desired file name.
So: when the list box selected index changes (or when you double click, or whatever event you pick), the javascript will open a window with page B.aspx. Page B will have an image control, set to the URL to A.aspx. A.aspx will stream the image contents to the image control, which will appear in your new window.

Related

How to display image with user input URL?

Having a bit of trouble getting started on making a simple HTTP image browser. I am doing this in C# and using a Windows Form Application. The requirements included are that I prompt the user for a URL of an image which is inputted in a textbox (I believe I have to use an HTTPwebRequest, but not sure), and then click a button and display the image in a picturebox. I'm used to writing console applications, and am brand new to the Windows Forms commands, which is why I'm looking for some help.
**In addition, but not necessary, is the inclusion of a status code of the image, as well as associated headers that are returned by google's web server, which are displayed in textboxes. Sorry if this is too vague, I am just completely lost, and anything would help.
If you drop a TextBox, a Button and a PictureBox onto your form, you can use the code below to download an image from the internet, convert it to an image and show it in the PictureBox.
m_urlTextBox is the TextBox the user can use to enter the image URL.
m_downloadButton is the button that the user clicks to initiate the download.
m_pictureBox is the PictureBox used to draw the downloaded image.
The code for the Click event handler for the download button is shown below.
private void m_downloadButton_Click(object sender, EventArgs e)
{
using (var client = new WebClient())
{
var imageData = client.DownloadData(m_urlTextBox.Text);
var converter = new ImageConverter();
var image = (Image)converter.ConvertFrom(imageData);
m_pictureBox.Image = image;
}
}
I've made no attempt to handle errors or conversion failures, but this should give you the gist of what is required. If you need to inspect the headers associated with the image, you should look into WebRequest.Create / GetResponse / GetResponseStream etc. instead of using WebClient.

System.IO.File.WriteAllText will not write to a file

I've created a method so that when a button (butCommit) is clicked, the value of a textbox is taken and stored into a string. That string is then written to a .txt file using the WriteAllText method. However, when this method is executed, the file is not modified at all.
The button method is working fine, as I have a response.redirect method in there which works every time.
The path for the .txt file is also correct as I have another method which will, on the page load, display the current contents of the .txt file (using the ReadAllText method) and the exact same path that I am using for the WriteAllText method.
Here is the code giving me problems:
void butCommit_Click(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = txtHomepageContent.Text;
System.IO.File.WriteAllText(path, content);
Response.Redirect("Default.aspx");
}
I'll repeat again: The above method is initialized and works fine with the Response.Redirect method, just not the WriteAllText method.
EDIT For more clarity:
The purpose of this is to display a message on the home page of a site. That message is required to be stored in a .txt file which is on the server (named content.txt). In the ControlPanel.aspx page, the user needs to be able to change the context of the text file using a text box. Preferably they are also able to view what is currently in content.txt using the same text box.
The code I have for the viewing bit is this:
protected void Page_Load(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}
This specifies "path" as the path to content.txt, then specifies "content" as a string containing content.txt's data. After that it places "content" into the textbox txtHomepageContent. This is also the text box used to input the new data for content.txt.
It seems if I remove the above section of code, I am able to write to the text file now with no problems, however I of course am unable to view what is in there first. If I leave the above code chunk in, then whatever I write in the textbox is ignored, and the original text is submitted back into the content.txt, resulting in no changes.
I do understand there are easier ways of doing this such as storing in a database, but the requirements are for it to be in a .txt file.
You can either use the Page_Init event handler instead of Page_Load to initialize your text box (since submitted postback values will load between the Init and Load events, effectively overwriting the text box value with what was submitted), or if you want to stay with Page_Load, check whether there is a postback first:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.IsPostBack)
return;
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}

C# : upload file to a file host

i have been trying to upload a file to Fileshawk.com from my application.
Here is how i did it :
1- Created a web browser control
2- Loaded the page
3- in the web page it has an input tag of file.
4- i tried to add the file to HTML by using that method which failed.
private void Set_Text_TAG_INPUT(string attribute, string attname, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection htmle = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement current in htmle)
{
if (current.GetAttribute(attribute).Equals(attname))
{
current.SetAttribute("value", value);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Set_Text_TAG_INPUT("id","upfile_1342028154587", "FILE.txt");
}
So it didn`t change the value of the Input tag.
Is there any way to add my file to the HTML or page code and about pressing upload i already have a method that invokes the web browser button and click it.
For example , when you click Select file from the input file tag , a window pop up and you select the file , now when you click okay , where is that file stored in the HTML ?
Finally excuse me if i am a newbie in HTML and web knowledge.
This is a security feature in browsers. You cannot programmatically set the value of the value of an input tag of type 'file' since it would introduce a security hole (e.g. a malicious developer could set the value right before handling a submit event and get whatever file they wanted).
Use WebScraper, a C# .NET library to upload file/download file easily, but you need to analyze Fileshawk.com HTML to come up with your own WebScraper syntax, there are totally 22 file host syntax available for sample and already useable.
http://sorainnosia.com/Home/Article/WEBSCRAPER

How to render a string containing HTML into an image in C#?

I am developing a web application that has an interactive feedback tool for users. In this application users can click a send feedback button. The button puts up an overlay over their current web page and allows them to drag highlight area DIVs to emphasize certain areas. Once they submit their feedback the HTML of the entire page is passed via AJAX back to the server.
Once on the server I now have a string containing the HTML of the page. From here I would like to run this string through some sort of engine that renders the HTML and builds an image. A sort of round about way of taking a screenshot if you will.
How might one accomplish something like this? Are there engines available that are written in C# and can build up the HTML and render an image?
Check out this framework - http://awesomium.com/
This is exactly what you need.
Set the base URL, this will be needed to resolve any relative URLs
WebCore.SetBaseDirectory("C:\\MyApplication\\MyBaseDirectory");
Then load the HTML -
myWebView.LoadHTML("<p>Hello World!</p>");
Then use the .Render() method, and you'll be able to save the rendered content to an image.
You can consider usin LLMozLib if you want to go by Gecko.
See more details here
EDIT
There's an ActiveX control to embed Gecko on Windows.
Sample here
EDIT
I got it working on a Windows Forms application.
Using these resources.
It is a csharp wrapper to Gecko ...
That's my sample code ...
public partial class Form1 : Form
{
public Form1()
{
Xpcom.Initialize(#"C:\Users\esouza\Downloads\xulrunner"); //Tell where are XUL bin
InitializeComponent();
//geckoWebBrowser1 is an instance of GeckoWebBrowser control that I've dragged on the Form1
geckoWebBrowser1.DocumentCompleted += new EventHandler(geckoWebBrowser1_DocumentCompleted);
}
private void button1_Click(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate("http://www.google.com");
}
void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
{
Bitmap b = new Bitmap(geckoWebBrowser1.Width, geckoWebBrowser1.Height);
geckoWebBrowser1.DrawToBitmap(b, new Rectangle { X = 0, Y = 0, Width = 800, Height = 600 });
b.Save("file.bmp");
}
}
Very interesting question and I've not got a silver bullet for you.
You're surely going to need a browser engine of some description and then capture the rendered output as a bitmap.
I see from this question that there was a COM wrapper developed for WebKit. Maybe that's a good starting point.

Why are image acting as downloads?

I am using C# ASP.NET. I generate user friendly image names and use rewrite to find the correct image name. Normally in firefox when i right click an image and hit view image i get the image in my browser. However these images are acting like downloads, why?
global.asax:
void Application_BeginRequest(Object sender, EventArgs e)
{
lazy(Context, HttpContext.Current.Request);
}
file.cs:
void lazy(...)
{
...
context.RewritePath(sz);
//sz = "/user/username/type/image.png"
}
Likely because the correct MIME type is not being sent along with the image.
The mime-type of the image is probably being reset by the RewritePath call. If so, FireFox then thinks that the image is just a binary stream and doesn't know what to do with it so it just attempts to download it.

Categories