I need to use libjpeg and libpng to convert an image from the raw RGB format to JPEG and PNG format and then output the result to a memory buffer, and the following two links saved me lots of coding effort:
http://www.codeguru.com/forum/archive/index.php/t-378333.html
http://stackoverflow.com/questions/1821806/how-to-encode-png-to-buffer-using-libpng
Additionally, there is also a good online tutorial showing how to do in-memory data compression using zlib at
http://www.gamedev.net/reference/articles/article2279.asp
PS: just found out that the latest libjpeg (version 8 ) coming out two weeks ago added the in-memory compression method. Cheers!