Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Setting the Pixel Format and Image Format of an Image in .Net


This one is because of a question I had someone ask me and I thought it might help out others so I figured I'd talk about the subject. Basically I've done quite a few different posts on various aspects of image manipulation. In fact I may focus a bit too much on the subject from time to time, but it's something that I find interesting. Anyway, while I've shown some of the more interesting aspects of the GDI+ code, I've never really talked about the subject of pixel formats or even image formats and how they're handled. Mainly because it's a fairly boring subject but I might as well cover it.

In GDI+, we generally use either the Bitmap or Image classes. When creating a Bitmap object, we can set the pixel format for the image (one of the constructors excepts it along with the height/width). There are a few of these but they're all fairly similar. For example, there is a format called Format16bppArgb1555. This format, is a 16-bit format that reserves one bit for the alpha channel and 5 bits each for the green, red, and blue channels. The listing of the various formats that are available can be found here. You may want to note that there is a format for 48-bit and 64-bit images. There is an issue though as it converts it down to a 24/32-bit image when you save it again. Other than this exception, once you've set the pixel format for that object, you're stuck with that format. The only way to convert it is to create a new image and copy the data over.

Image formats on the other hand are quite simple to deal with and converting from one to the other is easy. Generally when loading an image, it doesn't matter what the file format is (assuming of course that it's supported). With saving a file though, you may want it to be in a specific file format. For instance, since most of what I do at work is web related, I save a lot of things as JPEGs or PNGs. Now if I load up a Bitmap object with an image and then just try to save it as a JPEG, I'll run into an issue (anything that I load it in will say that it's not a jpeg, it may still load but it will throw up that warning). The reason for the issue is that it didn't save it as a JPEG. Instead the image is simply a bitmap file. The reason for that is that I never told it what format to save the image as and it simply used its default which is a bitmap. In order to fix this the Save function of the Bitmap class can take in a second object of type ImageFormat. This, as the name suggests, determines the format of the image. This is however not an enumeration, but a class. Thankfully there are some predefined definitions for us to use that can be found here. They're static members so you don't have to create anything yourself. Although if the format you want isn't listed, you're going to have to create your own definition (basically your own file loader). It's not a fun process...

Anyway, that's it. With that information you should be able to get the file to the specific format that you want. Sorry that there isn't any code on this one, but hopefully it helps someone out a bit. Anyway, give it a try, leave feedback, and happy coding.