473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there any alternative to Application.DoEvents()

I have a web application where I am trying to save webpage as image using the code provided at http://stackoverflow.com/questions/2...e-from-asp-net. Somehow I am not able to make it work on my server whereas its working perfectly on my local machine.Not sure about it but I think the line Application.DoEvents() is creating trouble for me , not even sure what it does but I don't get expected result if i remove it.
Is there any other alternative to Application.DoEvents() or any modification in the code which will solve my problem.
Jul 26 '12 #1
1 7373
Aimee Bailey
197 Expert 100+
This question was moved because it is ASP.NET specific.

To add my two cents, while I was writing this reply I had a look at the code you pointed too. The example that Cat Man Do has provided in theory could work, but there are many pitfalls with it, and it's fairly long winded. For example he creates a new thread, yet syncs it with the original, in theory making DoEvents useless in my book. For a bit of fun i've put a little example together myself, although I should mention that i've tested this only on C# WinForms not ASP.NET so it may need a bit of adaptation;

Expand|Select|Wrap|Line Numbers
  1. namespace WebBrowserCapture
  2. {
  3.     using System;
  4.     using System.Drawing;
  5.     using System.Drawing.Imaging;
  6.     using System.Linq;
  7.     using System.Windows.Forms;
  8.     using System.Collections.Generic;
  9.  
  10.     public class WebsiteToImage
  11.     {
  12.         private WebBrowser Browser;
  13.  
  14.         public string URL { get; private set; }
  15.         public List<Bitmap> Output { get; private set; }
  16.         public event EventHandler Complete;
  17.  
  18.         public WebsiteToImage(string url)
  19.         {
  20.             URL = url;
  21.             Output = new List<Bitmap>();
  22.             Browser = new WebBrowser();
  23.             Browser.ClientSizeChanged += delegate
  24.             {
  25.                 Output.Clear();
  26.             };
  27.  
  28.             Browser.DocumentCompleted += delegate
  29.             {
  30.                 var r = Browser.Document.Body.ScrollRectangle;
  31.                 var bmp = new Bitmap(r.Width, r.Height);
  32.  
  33.                 this.Browser.ClientSize = new Size(r.Width, r.Height);
  34.                 this.Browser.ScrollBarsEnabled = false;
  35.                 this.Browser.BringToFront();
  36.                 this.Browser.DrawToBitmap(bmp, Browser.Bounds);
  37.                 Output.Add(bmp);
  38.  
  39.                 if (Complete != null) Complete(this, null);
  40.             };
  41.         }
  42.  
  43.         public void Generate()
  44.         {
  45.             Output.Clear();
  46.             if (Browser.ReadyState == WebBrowserReadyState.Loading) return;
  47.             Browser.Navigate(this.URL);
  48.         }
  49.  
  50.         public void SaveAsJpeg(int outputIndex, string filename, long quality = 100)
  51.         {
  52.             var parameters = new EncoderParameters(1);
  53.             var codecs = ImageCodecInfo.GetImageDecoders();
  54.             var encoder = codecs.First(t => t.FormatID == ImageFormat.Jpeg.Guid);
  55.             parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
  56.             Output[outputIndex].Save(filename, encoder, parameters);
  57.         }
  58.     }
  59. }
  60.  
You may notice I have added a list for the Output, I found that the WebBrowser calls DocumentComplete a few times, so having a list allows you to see what was actually captured. To use this class, the following code can be written:

Expand|Select|Wrap|Line Numbers
  1. WebsiteToImage t = new WebsiteToImage("some-web-address");
  2. t.Complete += delegate
  3. {
  4.     t.SaveAsJpeg(0, "some-filename");
  5. };
  6. t.Generate();
  7.  
Hope this helps in some way.

Aimee
Jul 31 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: bazad | last post by:
Hi, I'd like to understand consequences of Application.DoEvents call. Does it create a new thread? Thank you
6
by: foldface | last post by:
Hi I am doing drag and drop between controls (listview and treeview) using mouse_event. What I am after is a book/resource to explain the behaviour below. If I do the various Win32 api calls...
6
by: Ollie Riches | last post by:
I understand the use of Application.DoEvents() to process all outstanding messages on the message queue in a winforms application if you have long running process on the UI thread. But can anyone...
1
by: RSH | last post by:
I created a new Windows Form project and I created a simple richtextbox to write to and I am looping through a simple example but obviously the screen isn't updated it only shows the first...
13
by: Amjad | last post by:
Hi, Is there an equivalent to the "Application.Doevents" method in modules or Windows services? I want to make a Windows service that calls a DLL. The DLL would have all my functions and it...
2
by: joeforbroke | last post by:
Background: I am helping someone debug a problem with their form that I have traced to a strange interaction between a click event handler and Application.DoEvents(). I'm trying to talk him into...
5
by: james.jdunne | last post by:
System.ArgumentException: Item has already been added. Key in dictionary: "-1" Key being added: "-1" at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) at...
16
by: Alan T | last post by:
I tried to use a thread to process a iterative execution of processes but afraid my thread is not thread-safe. If I am not using a thread, my main form will become 'white' when switch forth and...
4
by: Woo Mun Foong | last post by:
Hi, I have a DLL that is used to carry out some lengthly process. I would like to have something similar to DoEvents that can yield control back to Windows every now and then. Any ideas ? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.