Convert BMP to JPG

Source: Internet
Author: User

Convert the formats of BMP, png gif, and jpg into different formats. There are many types of libraries that can use ...jpeg Lib, image, cximage, and so on ....

I have never used other products. I have only used cximage... it feels good...

Http://www.codeproject.com/bitmap/cximage.asp

Download the full source files-2.10 MB.

# Include "ximage. H" when using cximage"

Did you disable JPG in your library?
Ximacfg. h
# Define cximage_support_jpg 1
If you want to implement BMP-> JPG during debugging, you must open
# Define cximage_support_bmp 1
# Define cximage_support_gif 1
# Define cximage_support_jpg 1
The above must be opened ............
# Define cximage_support_png 0 //
# Define cximage_support_mng 0
# Define cximage_support_ico 1
# Define cximage_support_tif 0 //
# Define cximage_support_tga 0 //
# Define cximage_support_pcx 0 //
# Define cximage_support_wbmp 0 //
# Define cximage_support_wmf 0 //
# Define cximage_support_j2k 0 // beta, use JP2
# Define cximage_support_jbg 0
Others can be disabled.
The corresponding PNG. lib and Tiff. Lib are not imported.
XFile. h. ximacfg. h. ximadef. h. ximage. CPP, ximage. h. xiofile. h. xmemfile. CPP, xmemfile. h. copy the file to the project folder and add the file to the project. Then, copy the Lib file in the debug folder in the cximage folder to the project, and set it in VC as follows:

Project Settings
|-C/C ++
|-Code generation
|-Use run-time Library: multithreaded DLL (must be the same
| All the linked libraries) // It should be a multi-threaded DLL, and debug can also be used.
|-Struct member alignment: must be the same for all the linked libraries
|-Precompiled headers: Not using precompiled headers
|-Preprocessor
|-Additional include directories :.. /cximage (enter. H and. the folder where the CPP file is copied and imported to the project. When the file is included in the project, the compiler searches for the folder, so the file to be included does not need a path)
|-Link
|-General
|-Object/library modules: ../PNG/debug/PNG. Lib
../JPEG/debug/JPEG. Lib
../Zlib/debug/zlib. Lib
../Tiff/debug/Tiff. Lib
../Jasper/debug/Jasper. Lib
../Cximage/debug/cximage. Lib (the preceding path is set to the directory where the Lib file in the project is copied from cximage)

To display a PNG file in picture box, you only need:
Cximage image ("myfile.png", cximage_format_png );
Hbitmap m_bitmap = image. makebitmap (m_picture.getdc ()-> m_hdc );
M_picture.setbitmap (m_bitmap );
Other formats, and so on.
Examples: how...
... Convert from a format to another
Cximage image;
// BMP-> JPG
Image. Load ("image.bmp", cximage_format_bmp );
If (image. isvalid ()){
If (! Image. isgrayscale () image. increasebpp (24 );
Image. set0000quality (99 );
Image. Save ("image.jpg", cximage_format_jpg );
}
// PNG-> TIF
Image. Load ("image.png", cximage_format_png );
If (image. isvalid ()){
Image. Save ("image. tif", cximage_format_tif );
}
... Load an image resource
// Load the resource idr_png1 from the PNG Resource Type
Cximage * newimage = new cximage ();
Newimage-> loadresource (findresource (null, makeintresource (idr_png1 ),
"PNG"), cximage_format_png );
Or // load the resource idr_jpg1 from DLL
Cximage * newimage = new cximage ();
Hinstance hdll = loadlibrary ("imagelib. dll ");
If (hdll ){
Hrsrc hres = findresource (hdll, makeintresource (idr_jpg1), "jpg ");
Newimage-> loadresource (hres, cximage_format_jpg, hdll );
Freelibrary (hdll );
}
Or // load a bitmap resource;
Hbitmap bitmap =: loadbitmap (AfxGetInstanceHandle (),
Makeintresource (idb_bitmap1 )));
Cximage * newimage = new cximage ();
Newimage-> createfromhbitmap (Bitmap );
... Decode an image from memory
Cximage image (byte *) buffer, size, image_type );
Orcxmemfile memfile (byte *) buffer, size );
Cximage image (& memfile, image_type );
Orcxmemfile memfile (byte *) buffer, size );
Cximage * image = new cximage ();
Image-> decode (& memfile, type );
... Encode an image in memory
Long size = 0;
Byte * buffer = 0;
Image. encode (buffer, size, image_type );
...
Free (buffer );
Orcxmemfile memfile;
Memfile. open ();
Image. encode (& memfile, image_type );
Byte * buffer = memfile. getbuffer ();
Long size = memfile. Size ();
...
Free (buffer );
... Create a multipage tiff
Cximage * pimage [3];
Pimage [0] = & image1;
Pimage [1] = & image2;
Pimage [2] = & image3;
File * hfile;
Hfile = fopen ("multipage. tif", "W + B ");
Cximagetif multiimage;
Multiimage. encode (hfile, pimage, 3 );
Fclose (hfile );
Orfile * hfile;
Hfile = fopen ("C: // multi. tif", "W + B ");
Cximagetif image;
Image. Load ("C: // 1.tif", cximage_format_tif );
Image. encode (hfile, true );
Image. Load ("C: // 2.bmp", cximage_format_bmp );
Image. encode (hfile, true );
Image. Load ("C: // 3.png", cximage_format_png );
Image. encode (hfile );
Fclose (hfile );
... Copy/paste an image
// Copy
Handle hdib = image-> copytohandle ();
If (: openclipboard (afxgetapp ()-> m_pmainwnd-> getsafehwnd ())){
If (: emptyclipboard ()){
If (: setclipboarddata (cf_dib, hdib) = NULL ){
Afxmessagebox ("unable to set Clipboard data ");
}}}
Closeclipboard ();
// Paste
Handle hbitmap = NULL;
Cximage * newima = new cximage ();
If (openclipboard () hbitmap = getclipboarddata (cf_dib );
If (hbitmap) newima-> createfromhandle (hbitmap );
Closeclipboard ();
Note that the entire cximage class library is very large. If you only need to be able to process several formats, you can find some switching options in the main header file ximage. h to close some image libraries. Each library in JPG, PNG, And Tiff will add about KB of content to the final program. The cximage class library is compressed to about 60 kb. Therefore, you need to carefully select the class libraries you actually need. After compiling the sample project provided by the author, you will find the following files: · cximage: cximage. lib-static library · cximagecrtdll: cximagecrt. DLL-DLL not using MFC · cximagemfcdll: cximage. DLL-DLL using MFC · Demo: demo.exe-program linked with cximage. lib and the C libraries · demodll: demodll.exe-program linked with cximagecrt. DLL · j2k, Jasper, JBIG, JPEG, PNG, Tiff, zlib: static C libraries it takes several minutes to build these projects (the intermediate file can reach 60 MB ).

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1414164

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.