Bug Tracker Blog by Corey Trager

Screen capture utility in C#, .NET

by Corey Trager 3. February 2010 03:55


I just released a new screen capture utility for BugTracker.NET.  With it, with just 2 clicks (2 clicks!) you can capture a screenshot and post it to your BugTracker.NET.   Just click "Capture" and then click "Send".

The screen capture utility looks like this:

 

 

You can annotate the screen capture before you send it with a red "Sharpie" and provide a description.  You can have it create a new bug or post it to an existing bug.

This new app is a rewrite of one I did 5 years ago. That earlier app was...ok.  It did the job.  But then, a couple years ago, Alexej Hirsch developed a new screen capture app, "Bug Shooting", that could talk to BugTracker.NET and several other trackers.  Bug Shooting was much nicer than my old app, and it used a newer, better API - an API that Alexej himself pushed me to develop. There wasn't much point in trying to compete with him, so, I abandoned my own app and started recommending that people use his. That was the story for a couple years.

This month, however, Alexej released a new version of Bug Shooting as 30-day-trial shareware.  I really, truly wish him the best of luck, because his app is really nice and he has earned whatever money he can make from it.  But, I wanted BugTracker.NET to come with a free app, so I decided to try to revamp the screen capture part. 

That earlier app was written using C++/MFC, created using Visual Studio 2005.  I don't even have that installed on my home computer, and I really didn't look forward to returning to MFC development.  I've worked with MFC for years.  I used to be really good at it.  Then, about 3 years ago I started a new assignment at my real job using C#/.NET/WinForms.  Despite being so experienced at MFC and so new to WinForms, I felt like after, oh, 2 days, I was more productive with WinForms than with MFC.   This screen capture app here, starting from scratch, I got a barebones but fully usable version done in about 6 hours, including region selection, annotation, sending to the tracker.  Wow, I even impressed myself!  What you see in the screen shot, that only took about 12 hours.

The MFC years weren't wasted, nor the Petzoldian-Win32 years before those; that Win32 knowledge still comes in handy, even with WinForms.  This screen capture app too uses native Win32 calls here and there.

If you use the new app, please let me know how it goes, good or bad. 

If you are looking to write your own screen capture app and want a very simple starting point, the source code comes with the BugTracker.NET zip file

The rest of this post consists of code snippets demonstrating some of the key APIs you would use for this type of app.


* Capture a portion of the screen as a bitmap:

using System.Drawing;

// Size size is how big an area to capture
// pointOrigin is the upper left corner of the area to capture

Bitmap bitmap = new Bitmap(size.Width, size.Height);
{
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        graphics.CopyFromScreen(pointOrigin, new Point(0, 0), size);
    }
}

 

* Get the extents of a multiple monitor desktop

int maxX = 0;
int maxY = 0;

foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
{
    int x = screen.Bounds.X + screen.Bounds.Width;
    if (x > maxX)
        maxX = x;
    int y = screen.Bounds.Y + screen.Bounds.Height;
    if (y > maxY)
        maxY = y;

}

 

* To save the bitmap as a jpeg

System.IO.Stream fileStream;
if ((fileStream = saveFileDialog1.OpenFile()) != null)
{
    bitmap.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    fileStream.Close();
}


* To convert the bitmap into jpg, in memory

System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);


* To save the bitmap to the clipboard on Ctrl-C

private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.C)
    {
        if (e.Modifiers == Keys.Control)
        {
            if (bitmap != null)
            {
                Clipboard.SetImage(this.getBitmap());
            }
        }
    }
}

* To get the rectangle of the foreground window

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

~
~
~

IntPtr hWnd = GetForegroundWindow();
RECT rect = new RECT();
GetWindowRect(hWnd, ref rect);

 

Tags:

Comments

2/9/2010 5:40:05 PM #

Nice new look and features, however this version does not work for me (v.1.4 works fine):

- bug appears in BugTracker (v.2.4.8) but no attachment
- in v.1.4 it is possible to specify default project, very convenient

Thanks,
Oleg

Oleg |

2/9/2010 9:17:15 PM #

Yes, sorry, the new screen capture app uses a different API, one that isn't in 2.4.8.   You would have to upgrade....    I'll probably add the ability to specify project soon.

Corey Trager |

3/8/2010 5:55:43 AM #

BugTracker.NET app for Android

BugTracker.NET app for Android

Bug Tracker Blog |

8/30/2010 2:23:11 AM #

Pingback from answers.hiwav.com

filled rubber band in Winforms Application | hiwav

answers.hiwav.com |

5/10/2011 5:04:53 PM #

Pingback from hectorea.wordpress.com

Imprimir Pantalla en C# (Print Screen) « hector espinosa

hectorea.wordpress.com |

4/30/2012 2:32:30 PM #

Pingback from golneatr.shikshik.org

Capture utility | Golneatr

golneatr.shikshik.org |

Comments are closed

Powered by BlogEngine.NET 1.5.0.7

RecentComments

Comment RSS