суббота, 23 апреля 2011 г.

Webbrowser Control Screenshot works in ASP.Net Dev Server but not IIS


Quick Overview:  Webbrowser control that takes a screenshot of a website and converts it to a bitmap works in ASP.Net Development Server but not when deployed to IIS 7.  When in IIS, the bitmap will display "Navigation to the page was canceled."
Quick Facts:
  • IIS server (Win Server 2k8 R2) is on corporate domain (firewall cannot be disabled as it is controlled by Group Policy)
  • I inherited the app from another developer so the below code is not my own
  • The screen capture works on a different server (Win Server 2k8) with identical settings (as far as I can tell) in IE, IIS, Firewall, User Account Permissions, and Folder Access
  • No events are thrown (through Event Viewer)
  • App pool - v2.0, Integrated, Network Service (account needed as an SSRS report is generated (using the screenshoot) using the same account)
I feel this has to be a permission's issue on IIS as I am able to grab a legit screenshot from my ASP.NET Dev Server or when clicking a hyperlink (with the same web address) within the app but I do not know what other settings to check.  Any help or suggestions would be greatly appreciated
Code (most) to capture screenshot and convert to bitmap:
public Bitmap GenerateWebSiteThumbnailImage()
            {
                Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
                m_thread.SetApartmentState(ApartmentState.STA);
                m_thread.Start();
                m_thread.Join();
                return m_Bitmap;
            }

            private void _GenerateWebSiteThumbnailImage()
            {
                WebBrowser m_WebBrowser = new WebBrowser();
                m_WebBrowser.ScrollBarsEnabled = false;
                m_WebBrowser.ScriptErrorsSuppressed = true;
                m_WebBrowser.Navigate(m_Url);
                m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
                m_WebBrowser.Dispose();
            }

            private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser m_WebBrowser = (WebBrowser)sender;
                m_WebBrowser.ClientSize = new Size(1024, 768);
                m_WebBrowser.ScrollBarsEnabled = true;
                m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
                m_WebBrowser.BringToFront();
                m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
                m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
            }

Taking Website Screenshots With The WebBrowser Control

Sample code on how to take screenshots of websites, resize them to thumbnails and save them as PNG file onto the harddrive.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Imaging;
  10. using System.Drawing.Drawing2D;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13.  
  14. namespace ScreenshotTaker
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void btnLoad_Click(object sender, EventArgs e)
  24.         {
  25.             webBrowser1.Navigate("http://www.geekpedia.com");
  26.         }
  27.  
  28.         private void btnSnap_Click(object sender, EventArgs e)
  29.         {
  30.             // The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
  31.             Bitmap bitmap = new Bitmap(1024768);
  32.             Rectangle bitmapRect = new Rectangle(001024768);
  33.             // This is a method of the WebBrowser control, and the most important part
  34.             webBrowser1.DrawToBitmap(bitmap, bitmapRect);
  35.  
  36.             // Generate a thumbnail of the screenshot (optional)
  37.             System.Drawing.Image origImage = bitmap;
  38.             System.Drawing.Image origThumbnail = new Bitmap(12090, origImage.PixelFormat);
  39.  
  40.             Graphics oGraphic = Graphics.FromImage(origThumbnail);
  41.             oGraphic.CompositingQuality = CompositingQuality.HighQuality;
  42.             oGraphic.SmoothingMode = SmoothingMode.HighQuality;
  43.             oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
  44.             Rectangle oRectangle = new Rectangle(0012090);
  45.             oGraphic.DrawImage(origImage, oRectangle);
  46.  
  47.             // Save the file in PNG format
  48.             origThumbnail.Save("Screenshot.png", ImageFormat.Png);
  49.             origImage.Dispose();
  50.         }
  51.     }
  52. }

How to check JQuery object is null



This is simple and no need to write post for it. You may feel same right? But, this is the question I received from almost 10 people till now. So, I thought of writing a post on it. So that, it may help people who are looking for the same through out the world.
By default JQuery returns object when you declare $("$id") or $(".class"). If you write code for it as below, then that's wrong.
  1. if($("#id") != null)  
  2. {  
  3. //Write code if the object is not null  
  4. }  
  5.   
  6. if($(".class") != null)  
  7. {  
  8. //Write code if the object is not null  
  9. }  
If you write the above code for checking null condition then if condition always success and will execute code inside of the if condition. So, the correct solution is,
  1. if($("#id").length > 0)  
  2. {  
  3. //Write code if the object is not null  
  4. }  
  5.   
  6. if($(".class").length > 0)  
  7. {  
  8. //Write code if the object is not null  
  9. }  
It tries to find all objects which has the given id or class and if any exists then the length will be equal to the number of times the id or class occurred. So, if the length is zero then it's nothing but the id or class don't exist and the object is null.
Hope, this solved your problem and this is a new tip for today. Is this helpful?

пятница, 22 апреля 2011 г.

Сравнение Timer с DispatcherTimer

Windows.Forms.Timer uses the windows forms message loop to process timer events. It should be used when writing timing events that are being used in Windows Forms applications, and you want the timer to fire on the main UI thread.

DispatcherTimer is the WPF timing mechanism. It should be used when you want to handle timing in a similar manner (although this isn't limited to a single thread - each thread has its own dispatcher) and you're using WPF. It fires the event on the same thread as the Dispatcher.

In general, WPF==DispatcherTimer and Windows Forms==Forms.Timer.

That being said, there is also System.Threading.Timer, which is a timer class that fires on a separate thread. This is good for purely numerical timing, where you're not trying to update the UI, etc.

среда, 6 апреля 2011 г.

GridView.PagerTemplate Property


Specifies the template to use for the pager row in a GridView control.

Syntax



   <pagertemplate>
      ... template definition here
   

Property Value

An ITemplate interface that defines how the pager row of a GridView control is rendered.
The property is read/write with no default value.

Remarks

The PagerTemplate describes the layout of elements to render in the pager section of a GridView control.
When the paging feature is enabled ( AllowPaging is set to true ), a pager row is displayed in the GridView that contains the controls to enable users to navigate to the different pages in the control. The PagerTemplate enables customizing this paging interface.
 NOTE: When the PagerTemplate property is set, it overrides the built-in pager row interface.
To specify a template for the pager section of a GridView, declare a  element between the opening and closing tags of the control. You can then list the contents of the template between the opening and closing  ...  tags.
In addition, the appearance for this template can be set using the PagerStyle property.
Typically, button controls are added to the pager template to perform the paging operations. The GridView control performs a paging operation when a button control with its CommandName property set to Page is clicked. The button's CommandArgument property determines the type of paging operation to perform. The following table lists the command argument values supported by the GridView control.
CommandArgumentDescription
NextNavigates to the next page.
PrevNavigates to the previous page.
FirstNavigates to the first page.
LastNavigates to the last page.
Integer valueNavigates to the specified page number.
 NOTE: The PagerTemplate item cannot be data bound.