After taking a MCTS certification practice test and getting several questions wrong, I started delving into them to figure out what I needed to study. It was then that I found a question asking how to save an image as a JPEG. The test claims that there are two correct answers:
Bitmap bm = new Bitmap(100,100); bm.Save("picture.jpg", ImageFormat.Jpeg);
- and -
Bitmap bm = new Bitmap(100,100); bm.Save("picture.jpg");
The only difference between the two is the parameters used in the call to Bitmap.Save(). I didn’t check the last option, because I wasn’t sure what in which format the image would be saved. The test explanation says that “If you don’t specify an image format, Bitmap.Save defaults to the JPEG format.” Hmm, really?
I had found some dissenting opinions on the net, and the MSDN documentation clearly states that “If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used.”
Of course, there’s nothing like actually trying the thing out and seeing what it REALLY does. So built a console app that simply does the following:
Bitmap bm = new Bitmap(100, 100); bm.Save("test.jpg"); bm.Save("test.png");
Both files loaded fine in Windows Picture & Fax Viewer, but trying to load the “jpeg” file in Photoshop proves the png theory with the message “Could not complete your request because a JPEG marker segment length is too short (the file may be truncated or incomplete).” Or it may be because IT’S NOT A JPEG, guys. I thought I would post a clarification on the web, since I didn’t see anything when I did a Google search. Hopefully it will help out other poor confused souls, as well.
This error is from the official Microsoft Press study guide. Let’s hope the actual test doesn’t contain these sorts of inaccuracies… I’ve got a $125 test fee riding on it.