As a developer of single board computers, we at Eurotech Inc, have some single board computers lying around the office, and a few LCD panels. I thought that it might be fun to put one to use as an electronic picture frame. My goal is to hang it on the wall to show picture of some of the work that we do. This is also an opportunity to work on my C# skills.
I am not an application developer, so of course I am no expert on the .NET Compact Framework or C#. I have been working with C# to maintain an application that one of my engineers developed. The good news for me is that the .NET Compact Framework made it easy for me to write the application, the bad news is that as a C/C++ driver developer I spent a lot of time searching for the wrong things.
The following may be more interesting for how I got to the end, rather than for the code that I developed.
I decided to start simple by figuring out how to display a Windows Bitmap file. I Googled for “C# display bmp” and found what I needed quickly. By adding a System.Windows.Forms.PictureBox to my form, all I needed to to was then assign a Bitmap to the PictureBox.Image and Invalidate the PictureBox. That was just too easy. I added the following, after letting Visual Studio name my PictureBox to pictureBox1:
PictureBox1.Image = new Bitmap( \\Storage Card\\image.bmp);
Now the hard part, update the bitmap with a new one on a regular basis. I have done a little Windows application development, so I know enough to be dangerous. I decided to use a separate thread to search a folder for bitmap files. The first task was to figure out how to find the files. I Googled for “C# FindFirstFile” which of course is a good WIN32 C/C++ function for finding files, and as luck would have it, this turned up DirectoryInfo as a way to find the files. So to find the BMP files on my Storage Card I added:
foreach (FileInfo fi in new DirectoryInfo("\\Storage Card").GetFiles(“*.BMP”))
I will fill in the loop later, but first I need to figure out how to do something with the file once I find it. Now I need that thread I wanted to create, and this turned out to be the most difficult part of the application. It probably would have been easy if I had a good C# book, or if I wasn’t a C/C++ programmer. But as a C/C++ programmer I was searching for the wrong things. Finding information about creating a thread and blocking execution wasn’t so hard. I found information and created a thread which was able to find the BMP files on the Storage Card, that wasn’t a problem at all.
The problem was that a data thread, which is what my thread to find BMPs is, cannot access the Windows Form data directly. I have enough experience with Windows programming to know that well. Now I needed to find information about how to deal with this in C#. If I were writing this in C++, I would simply add a handler to the system and send a message to the Window to update the data. That knowledge probably made this more difficult for me. I Googled for “C# SendMessage” and found many results telling me how to P/Invoke the SendMessage function. That was my first clue that using SendMessage was not the best way to accomplish my goal in C#.
I searched for several hours for all of the C/C++ terms that I could think of to figure out how to update the BMP being displayed. Finally, I started to hit on the right information to get going, but not completely. I started to understand that what I needed was to Invoke() a Delegate function.
A few days later I was talking with Chris Tacke from OpenNETCF who laughed at me and told me that I needed a Delegate, like it is a no brainer for a C# programmer. But I was trying to do it without his help.
A little more searching and I figured out how to use a Delegate in a Windows Form. I also expanded it to search other folders and file types.
So the lessone here is to understand the language and API well enough to be able to search for the correct terms. Otherwise it is easy to get off track with good answers to bad questions.
Copyright © 2008 – Bruce Eitman
All Rights Reserved