Often, when I work with some form of graphics, I do not have a handy library to save the image buffer I'm working with in a file on disk. In most cases, when I want to do that, I make use of the Targa format. This is very easy to save since the header is very simple (18 bytes) and the data can be saved verbatim. Plus, many tools, such as the Gimp, can read the result at once.
Here is my C code to do so. It will work in C++ also. It should be easy to tweak for other languages such as C#.
{ unsigned char hdr[18]; // the targa file header FILE *f; memset(hdr, 0, sizeof(hdr)); ...