Solved

Web Page screen capture

Posted on 2016-08-24
3
46 Views
Last Modified: 2016-08-25
I have a .Net CMS Web App

I would like the most efficient means of doing a screen capture and launch email client from a button or link on the masterpage

In other words...
Customer sees data in the GridView they can't understand
CLick link or button that
1. Captures the screen as an image
2. Renders into a toggled div tag (With email form) as a jpg and they can send the mail.

I have the toggled div and form already
0
Comment
Question by:lrbrister
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
I had thrown together a page capture for another EE_PAQ - https:/Q_28693274.html#a40863648

Here is that code reposted:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using System.Threading;

namespace EE_Q28693274
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			WebSiteToImage site = new WebSiteToImage(new Uri("http://www.experts-exchange.com/").AbsoluteUri, "experts-exchange.jpg");
			if (site.IsOk)
			{
				site.Generate();
				pictureBox1.Image = site.Bitmap;
			}
		}
	}

	[ComVisible(true), ComImport()]
	[GuidAttribute("0000010d-0000-0000-C000-000000000046")]
	[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
	public interface IViewObject
	{
		[return: MarshalAs(UnmanagedType.I4)]
		[PreserveSig]
		int Draw(
			[MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect, 
			int lindex, IntPtr pvAspect, [In] IntPtr ptd, 
			IntPtr hdcTargetDev, IntPtr hdcDraw, 
			[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcBounds, 
			[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcWBounds, 
			IntPtr pfnContinue, 
			[MarshalAs(UnmanagedType.U4)] UInt32 dwContinue
			);

		[PreserveSig]
		int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hicTargetDev, [Out] IntPtr ppColorSet);

		[PreserveSig]
		int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);

		[PreserveSig]
		int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);

		void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);

		void GetAdvise(
			[In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, 
			[In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, 
			[In, Out, MarshalAs(UnmanagedType.LPArray)] IAdviseSink[] pAdvSink
			);
	}

	class WebSiteToImage
	{
		WebBrowser browser;
		bool fIsGenerated = false;
		bool fIsGenerating = false;
		ManualResetEvent generated = new ManualResetEvent(true);
		public string Url { get; private set; }
		public string FileName { get; private set; }
		public int Width { get; private set; }
		public int Height { get; private set; }
		public Bitmap Bitmap { get; private set; }
		public string ErrorMessage { get; private set; }
		public bool IsOk { get; private set; }

		public WebSiteToImage(string url, string fileName, int width = -1, int height = -1)
		{
			Url = url;
			FileName = fileName;
			Width = width;
			Height = height;

			try
			{
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
				request.AllowAutoRedirect = true;
				request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
				request.Referer = "http://www.google.com/";
				request.ContentType = "text/html";
				request.Accept = "*/*";
				request.KeepAlive = false;

				using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
				{
					string x = response.StatusDescription;
				}
			}
			catch (Exception ex)
			{
				ErrorMessage = ex.Message;
				IsOk = false;
				return;
			}
			IsOk = true;
		}

		public void Generate()
		{
			if (!fIsGenerating)
			{
				fIsGenerating = true;
				if (!fIsGenerated)
				{
					Thread t = new Thread(_Generate);
					t.SetApartmentState(ApartmentState.STA);
					t.Start();
					t.Join();
					generated.WaitOne();
				}
				fIsGenerating = false;
			}
		}

		private void _Generate()
		{
			if (!fIsGenerated)
			{
				try
				{
					HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
					request.AllowAutoRedirect = true;
					request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
					request.Referer = "http://www.google.com";
					request.ContentType = "text/html";
					request.AllowWriteStreamBuffering = true;
					request.AutomaticDecompression = DecompressionMethods.GZip;
					request.Method = "GET";
					request.Proxy = null;
					request.ReadWriteTimeout = 20;

					HttpStatusCode status;
					using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
						status = response.StatusCode;

					if (status == HttpStatusCode.OK || status == HttpStatusCode.Moved)
					{
						browser = new WebBrowser();
						browser.DocumentCompleted += OnDocumentCompleted;
						browser.Size = new Size(Width, Height);
						browser.ScrollBarsEnabled = false;
						browser.ScriptErrorsSuppressed = true;
						browser.Navigate(Url);
						while (browser.ReadyState != WebBrowserReadyState.Complete)
							Application.DoEvents();

						generated.Set();
						if (Width == -1 || Height == -1)
						{
							Width = browser.Document.Body.ScrollRectangle.Width;
							Height = browser.Document.Body.ScrollRectangle.Height;
							browser.ClientSize = new Size(Width, Height);
						}
					}
					Bitmap = GetBitmap();
					fIsGenerated = true;
				}
				catch (Exception ex)
				{
					ErrorMessage = ex.Message;
					fIsGenerated = false;
				}
			}
		}

		private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			if (sender is WebBrowser)
				((WebBrowser)sender).Document.Window.Error += SuppressScriptErrorsHandler;
		}

		private void SuppressScriptErrorsHandler(object sender, HtmlElementErrorEventArgs e)
		{
			e.Handled = true;
		}

		private Bitmap GetBitmap(int width = -1, int height = -1)
		{
			Bitmap result = default(Bitmap);
			IViewObject ivo = browser.Document.DomDocument as IViewObject;
			Rectangle rect = new Rectangle(0, 0, Width, Height);
			Bitmap bitmap = new Bitmap(rect.Width, rect.Height);

			using (Graphics g = Graphics.FromImage(bitmap))
			{
				IntPtr ghdc = g.GetHdc();
				ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ghdc, ref rect, ref rect, IntPtr.Zero, 0);
				g.ReleaseHdc(ghdc);
				g.Dispose();

				if ((height == -1 || width == -1) || (height == Height && width == Width))
					result = bitmap;
				else
				{
					Bitmap thumbnail = new Bitmap(width, height);
					using (Graphics gfx = Graphics.FromImage(thumbnail))
					{
						// high quality image sizing
						gfx.SmoothingMode = SmoothingMode.HighQuality;
						gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
						gfx.DrawImage(bitmap, new Rectangle(0, 0, width, height), rect, GraphicsUnit.Pixel);
					}
					bitmap.Dispose();
					result = thumbnail;
				}
			}

			if (FileName.Length > 0)
				result.SaveJPG100(FileName);
			return result;
		}
	}

	static class Extensions
	{
		private readonly static ImageCodecInfo[] fEncoders = ImageCodecInfo.GetImageEncoders();

		private static ImageCodecInfo GetEncoder(ImageFormat format)
		{
			return (from codec in fEncoders where codec.FormatID.Equals(format.Guid) select codec).FirstOrDefault();
		}

		public static void SaveJPG100(this Bitmap bmp, string filename)
		{
			var encoderParameters = new EncoderParameters(1);
			encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
			bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
		}
	}
}

Open in new window


-saige-
0
 

Author Comment

by:lrbrister
Comment Utility
I'll take a look and review when I am back in front of my workstation
0
 

Author Closing Comment

by:lrbrister
Comment Utility
Gave me a great start. Thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now