<h2>Question</h2> I want to create a jpeg file in my program. How can I save a bitmap into a jpeg file?
<h2>Answer</h2> In <a href="../articles/jpeglib.html">Using IJG JPEG library</a> article I have described how to read jpeg files on Pocket PC using IJG library. It is recommended to take a look at that article where jpeg lib porting discussed.
In order to write jpeg files link your projects with IJG jpeg library. I have provided a <a href="samples/WriteJpeg.zip">test project</a> (408 Kb) that writes jpegs. You can compile and run this application. When you press "Write Jpeg" button on this application a bitmap from application resources will be saved as a jpeg file in <i>\My Documents</i> directory.
If you want to use saving code in your application insert <i>STScreenBuffer.cpp</i>, <i>STScreenBuffer.h</i>, <i>JpegWriter.h</i>, <i>JpegWriter.cpp</i> files into your project and link it with the jpeg library. In <i>JpegWriter.h</i> you can find definitions of the following 3 functions:
Code:
BOOL WriteRGBBytesIntoJpegFile(const CString& strOutFileName,
const int nWidth, const int nHeight, const int nQuality, BYTE* pBuffer);
This function destined for saving an array of RGB data pointed by <i>pBuffer</i> to a jpeg file named <i>strOutFileName</i>. The array of data is supposed to be a <i>nWidth*nHeight*3</i> bytes length. The <i>nQuality</i> is jpeg quality in range of 1..100.
Code:
BOOL WriteBitmapIntoJpegFile(const CString& strOutFileName,
const int nQuality, HBITMAP hBitmap);
hBitmap is a handle for standard windows bitmap. Both functions return FALSE when error occurred and TRUE otherwice. If you want to get string representation of last error you should call following method:
Code:
CString GetJpegWriterError();
Provided functions are far from to be optimal in order to save simplicity. <i>WriteBitmapIntoJpegFile</i> can be rewritten without using <i>WriteRGBBytesIntoJpegFile</i> if you do so you can avoid extra memory allocation. If you want use this functions in more critical applications or you want save extremely large files please observe <i>example.c</i> in jpeglib destribution. This file contains well-commented jpeg saving and loading code. My code is based on that functions and I have tried to leave same variable names.
Test project contains CSTScreenBuffer class for bitmap data grabbing. You can read about this class <a href="bitmap_bits.html">there</a>.
<h2>Related resources:</h2>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/sections/image.html">Section: Images</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/articles/jpeglib.html">Article: Using IJG JPEG library</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/articles/bitmap_bits.html">QA: How can I get bitmap bits?</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/libpng.html">Library: LibPng</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/voimage.html">Library: VOImage</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/stscreenbuffer.html">Library: STScreenBuffer</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/dibsection.html">Library: DIBSection</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/picturebox.html">Control: PictureBox</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://www.microsoft.com/mobile/developer/technicalarticles/picturebox.asp">Article: Developing with an Enhanced PictureBox</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://www.microsoft.com/mobile/developer/technicalarticles/graphics.asp">Article: Displaying GIF, JPEG, and Other Graphics in Your C++ Applications</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="http://www.codeproject.com/bitmap/DIBSection.asp">Article: A DIBSection wrapper for Win32 and WinCE</a>