Click here to Skip to main content
15,894,896 members
Articles / Programming Languages / XML

Sedge: An Automated Error Reporting Tool

Rate me:
Please Sign up or sign in to vote.
4.94/5 (11 votes)
14 Jan 2010Ms-PL5 min read 38.4K   923   53  
This article describes Sedge - a highly customizable tool designed to help your customers create better error reports.
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Sedge.Core.Globalization;
using Sedge.UI.ViewModel;

namespace Sedge.UI.View
{
	public partial class ScreenshotView : UserControl
	{
		#region Private fields
		
		private readonly ScreenshotViewModel _viewModel;
		
		#endregion //Private fields

		#region Constructors

		public ScreenshotView()
		{
			InitializeComponent();
			SetupWindow();
		}

		public ScreenshotView(ScreenshotViewModel viewModel) 
			: this()
		{
			_viewModel = viewModel;
		}
		
		#endregion //Constructors

		#region Private Methods
		
		private void SetupWindow()
		{
			buttonTake.Text = Locale.Strings.ButtonTake;
			buttonEdit.Text = Locale.Strings.ButtonEdit;
			buttonRemove.Text = Locale.Strings.ButtonRemove;
			
			EnableButtonRemove();
		}

		private void ShowForm()
		{
			if (ParentForm != null)
			{
				ParentForm.Show();
				ParentForm.BringToFront();
				ParentForm.Opacity = 100;
			}
		}

		private void HideForm()
		{
			if (ParentForm != null)
			{
				ParentForm.Hide();
				ParentForm.Opacity = 0;

				while (ParentForm.Visible)
				{
					Application.DoEvents();
					Thread.Sleep(100);
				}
			}
		}
      
		private void EnableButtonRemove()
		{
			buttonRemove.Enabled = pictureBox.Image != null;
		}

		private Bitmap GetScaledBmp(Image bmp)
		{
			double factor = (double)pictureBox.Width / bmp.Width;
			if (factor * bmp.Height > pictureBox.Height)
			{
				factor = (double)pictureBox.Height / bmp.Height;
			}

			int w = Convert.ToInt32(bmp.Width * factor);
			int h = Convert.ToInt32(bmp.Height * factor);

			Bitmap imageBmp = new Bitmap(pictureBox.Width, pictureBox.Height);
			using (Graphics g = Graphics.FromImage(imageBmp))
			{
				g.DrawImage(bmp, (pictureBox.Width - w) / 2, (pictureBox.Height - h) / 2, w, h);
			}
			return imageBmp;
		}

		#endregion //Private Methods

		#region Event Handlers

		private void buttonTake_Click(object sender, System.EventArgs e)
		{
			Screen screen = Screen.FromControl(this);
			HideForm();
			Bitmap bmp =_viewModel.TakeScreenshot(screen);
			ShowForm();

			if (bmp.Width == 0 || bmp.Height == 0)
			{
				return;
			}

			pictureBox.Image = GetScaledBmp(bmp);
			EnableButtonRemove();
			bmp.Dispose();
		}

		private void buttonRemove_Click(object sender, System.EventArgs e)
		{
			_viewModel.RemoveScreenshot();
			pictureBox.Image = null;

			EnableButtonRemove();
		}

		#endregion //Event Handlers

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Canada Canada
I am a software developer from Toronto with 15 years of experience in software design and development. My major professional area is the development of highly customized software solutions, including desktop and web applications. Industrial process automation and hardware-related software development are among my favorite projects and I enjoyed developing several applications for semiconductor manufacturing companies.

My blog: http://LunarFrog.com/blog

Comments and Discussions