johnkainn
asked on
convert html string to an image dynamically (continue2)
I want to use following code (see also related question) to creata dynamically an image from html.
How do I change it so that it works for web application (C#)?
How do I change it so that it works for web application (C#)?
[STAThread()] << -- attribute
protected void Page_Load(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.Size = new System.Drawing.Size(1024, 768);
wb.ScrollBarsEnabled = false;
wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(delegate(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e) {
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(wb.ClientRectangle.Width, wb.ClientRectangle.Height);
wb.DrawToBitmap(bmp, new System.Drawing.Rectangle(0,0, bmp.Width, bmp.Height));
bmp.Save(@"c:\foo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
});
wb.Navigate(@"file://c:\my.html");
//wb.Navigate("http://www.google.com");
}
ASKER
I want to save the image to a webserver.
I get an error using System.Windows.Forms.WebBr owser
( The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) )
How should I write it so it works for web application?
I get an error using System.Windows.Forms.WebBr
( The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) )
How should I write it so it works for web application?
I've been trying to get this to work.
The STAThread attribute didn't work for me so I added AspCompat="true" to the Page....
<%@ Page ... AspCompat="true" %>
And you need to add a reference the System.Windows.Forms
At the moment I can't get the Navigate(url) to trigger anything. I've added event handers to DocumentCompleted, ProgressChanged and StatusTextChanged. non of them fire.
oobayly, your version of the code has nested DocumentCompleted events, is this correct?
The STAThread attribute didn't work for me so I added AspCompat="true" to the Page....
<%@ Page ... AspCompat="true" %>
And you need to add a reference the System.Windows.Forms
At the moment I can't get the Navigate(url) to trigger anything. I've added event handers to DocumentCompleted, ProgressChanged and StatusTextChanged. non of them fire.
oobayly, your version of the code has nested DocumentCompleted events, is this correct?
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.Size = new System.Drawing.Size(1024, 768);
wb.ScrollBarsEnabled = false;
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.DocumentCompleted);
wb.ProgressChanged += new WebBrowserProgressChangedEventHandler(wb_ProgressChanged);
wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
wb.Navigate(url);
// Wait for the download to complete
while (this.bmp == null)
Thread.Sleep(50);
// Write to a JPG (stream needs to be seekable)
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
// Send the image to the client
this.Response.ContentType = "image/jpg";
ms.WriteTo(this.Response.OutputStream);
ms.Dispose();
bmp.Dispose();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Unfortunately I am getting errors when I try to run this code.
johnkainn,
could you explain what the errors are!
could you explain what the errors are!
ASKER
I get following errors:
1) Error 18 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
2) Error 17 The name 'WebBrowserReadyState' does not exist in the current context
3) Error 16 The type or namespace name 'Uri' could not be found (are you missing a using directive or an assembly reference?)
4) Error 15 The type or namespace name 'WebBrowser' could not be found (are you missing a using directive or an assembly reference?)
1) Error 18 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
2) Error 17 The name 'WebBrowserReadyState' does not exist in the current context
3) Error 16 The type or namespace name 'Uri' could not be found (are you missing a using directive or an assembly reference?)
4) Error 15 The type or namespace name 'WebBrowser' could not be found (are you missing a using directive or an assembly reference?)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If it's the latter, you could modify the code to write the bitmap to a member variable. Then poll the result until it's valid & write it to the response.
I'd suggest saving local copies of the image, and check for them first. That way you don't have to recreate the image each time.
Open in new window