Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Webcam Still Capture

by Jim Oct 30, 2006 10:28 PM

I got a webcam, and of course I had to access it with some .NET code. I probably could have found a program to upload images to the website, but that wouldn't have been any fun.

I found a great little library on Nauman Leghari's Blog that got me most of the way there. It gave me a video window, but needed one more function to capture a still image from the webcam:

/// <summary>
/// Capture a still image and return it
/// </summary>
/// <returns>A Bitmap image</returns>
public System.Drawing.Image CaptureStillImage()
{
    System.Drawing.Image img = null;
 
    // Tell the device to grab a frame to the clipboard
    int rslt = SendMessage(deviceHandle, WM_CAP_EDIT_COPY, 0, 0);

    // Look for the data on the clipboard            
    IDataObject iData = Clipboard.GetDataObject();
    if (iData.GetDataPresent(DataFormats.Bitmap))
    {
        img = (System.Drawing.Bitmap)iData.GetData(DataFormats.Bitmap);
    }
    return img;
}

Tags:

Code

Tray Icon

by Jim Oct 28, 2006 8:14 AM

The tray icon that I created for my application looked like a dog next to all the other beautiful icons in my system tray. Look at the rough edges on the smiley face.

At first I thought that windows was choosing the wrong size icon for the system tray. My icon file has 32x32 and 16x16 icons, and windows was using the correct one in every other situation. Then I realized that the problem was loading the icon from my exe's resources. I was doing this:

mIcons(0) = New Icon( _ 
  GetType(MainForm).Assembly.GetManifestResourceStream("EventWatcher.EventWatcher.ico"))

When I should have been doing this:

mIcons(0) = New Icon( _ 
  GetType(MainForm).Assembly.GetManifestResourceStream("EventWatcher.EventWatcher.ico"), _ 
  New Size(16, 16))

The .NET icon object doesn't load the whole icon file, just one icon. So Windows never got a chance to choose the right one. By default, it was loading the 32x32 icon, probably just because it was first in the list.

Tags: ,

Code

404 Exceptions

by Jim Jul 18, 2006 10:07 PM

One of the rules of exception handling is to not use exceptions as flow control devices. If an exception can be anticipated, avoid it by testing for the problem condition in the first place.

That's fine, until the .NET framework doesn't give me the option of testing for the condition. I want to use WebClient or WebRequest to get a file from a web server, if it exists. But, there's no way to test for the existence of the file without throwing an exception! The server returns headers and a response stream for a 404, just like any other request - shouldn't I be able to suppress the exceptions and check for the 404 myself?

It's only going to happen a few times in my application, so maybe it's not so bad. But it's still ugly.

Tags:

Parsing bookmarks and images

by Jim Dec 13, 2005 9:54 AM

I uploaded my Firefox bookmarks.html file to the website, so that I could access it from anywhere; I have about a million of them, and it's useful to bring them up at work. The file isn't very user-friendly in its raw form.

So I decided to parse the file, extract the icons from it (they're in there) and add some script to expand and collapse the folders. I could get a little practice with regular expressions along the way.

// Rip the icon data out and replace with images
// I match to the end of the anchor tag so I can use the length
// to put the image at the end of the tag.
Regex regex = new Regex("ICON=\"data:image/x-icon;base64,(.*?)\".*?\\>");
i = 0;
icon = 1;
Match match = regex.Match(file, i);
while (match != null && match.Length != 0)
{
  Debug.WriteLine(match.Value);
  iconname = ExtractIcon(match.Groups[1].Value, dest, icon);
  file = file.Insert(match.Index + match.Length, string.Format(
    "<img src=\"{0}\" border=\"0\" width=16 height=16>&nbsp;", iconname));
  icon++;
  i = match.Index + match.Length;
  match = null;
  if (i < file.Length)
  {
    match = regex.Match(file, i);
  }
}

The image is in the .ico format, so it can be streamed right into a file with that extension:

private static string ExtractIcon(string data, string folder, int index)
{
  int i;
  string filename;
  // Open a binary file for writing
  filename = string.Format("bookmark_icon_{0}.ico", index);
  BinaryWriter bw = new BinaryWriter(
    File.Open(Path.Combine(folder, filename), FileMode.Create));
  bw.Write(Convert.FromBase64String(data));
  bw.Flush();
  bw.Close();
  return filename;
}

Piece of cake. The javascript for showing and hiding the DIVs is pretty straightforward; a little more regex does the trick. Full code for console application here.

In Firefox, image tags can contain data in base64 format. This isn't supported in IE6, but I've tested it in IE7 beta, and that does support it. Unfortunately, I won't have that at work until it's officially released.

Tags: ,