Flexible conversion of multi-format images in VC

zhaozj2021-02-17  51

Sichuan Xindu County State Taxation Bureau Zhou Mingyang colorful and beautiful high quality images, one image of Windows icons, high-speed sports, live-flexible three-dimensional animations, these vivid graphics do not show the art talents of program designers. In programming, image processing has become a compulsory course for each programmer, so for each programmer, familiar with "BMP", "GIF", "JPEG" image format and specific application, palette, Image file head format, image compression algorithm, etc. seem to have become an indispensable basis knowledge in the work. In the face of so many image formats, if you want to master the specific details, it seems that this is a bit unfair. Programming in the VC Display a bitmap, the following steps are indispensable: load bitmaps, get the size of the bitmap, enable the device environment, bit transfer, and the required program code looks thousands of lengthy. If you want to load a bitmap, you can save image files in other formats ...? Two words: headache! And all of this is caused by the limitations of GDI itself.

With the introduction of Windows 2000, the above situation has a great change: you don't have to know the specific meaning of each image format, you can write multi-format image browsing or conversion program, all dependence on Windows 2000 and The GDI technology used in the subsequent version. First take a look at the specific technical details of GDI and the characteristics of GDI programming.

Windows 2000 includes several major improvements in user interface, probably you have noticed a shadow mouse, a progressive toolbar, a transparent window, a smooth window change, etc. Windows 2000 has such a big improvement on the interface, which is complete because Windows2000 uses a GDI (Graphics Device Interface: graphics device interface). This kind of GDI has previously called GDI2K, and now there is a better name: GDI . GDI is a new type of graphical device interface, which is the main feature that it can create a new user desktop system that easily completes two-dimensional or 3D graphics processing, bringing a digitized picture for the desktop. GDI also provides enhanced graphics processing techniques, such as common: alpha blending, textures, map, enhanced text and picture display technology. In fact, GDI main features are to emphasize good visual feelings through hardware acceleration!

Unlike traditional GDI, GDI introduces support for COM (Component Object Model) technology, through COM technology, GDI simplifies access (open, saving) programs for image files: By calling COM components, GDI play Just the header, not the operator. For image files, GDI is not the file header information of the image file, whether or not the file format is in the file format, GDI first wants to see if the encoding (or decoded) information in the image format is registered ( HKEY_CLASS_ROOT / MIME / DATABASE / Content Type) If it is already registered, it is simple to call COM components through this encoded information. This technology has been used in Microsoft's other software, such as IE. "Experience" Friends who have passed Nimda viruses may not be unfamiliar with "Audio / WAV", NIMDA is to disguise their own: Let IE think that the attachment is a WAV file to automatically open the executable. This is actually a prominent manifestation of IE using COM technology.

With GDI launch, Microsoft also released the corresponding SDK at the same time. If you have installed the latest Microsoft Platform SDK or have started using VS .NET, GDI SDK is already in your system. If not, you can download the GDI header file and library files to http://nreamer.top263.net/progtool. After using GDI , there is no need to consider any handle, the device environment is conceptual. You only need to simply create a graphics object, and then call the object's method (Methods) to draw. The graphic object is the core of GDI , just as DC is like GDI. The graphics objects and DCs have many similarities, follow the same rules, but both in essence is essentially very different. One is based on the handle-based GDI, one is GDI based on the component object model. With GDI SDK programming, you must perform according to the following specification: Namespace Gdiplus, you must perform GDI initialization when using the GDI function, and you can destroy GDI , this specification is below There is a detailed description of the procedures for columns. As mentioned earlier, GDI is to access the image file by viewing coding information in the registration. In the SDK of GDI , the encoded information is stored in the ImageCodecInfo class. In this class, there is a coded CLSID (GUID identification code for COM components) ), Encoding mode, etc. In GDI, accessing encoding information in the registry usually implements the following two functions:

1. View image coding information available in the system (quantity and size)

Status getImageEncodersSize

UINT * NUMENCODERS, // Address of the number of encoders

UINT * SIZE // Store the memory size required

);

2, get all the encoding information

Status getImageEncoders

UINT NUMENCODERS, // Number of available encoders

UINT size, // Save the memory of the encoder information (the size of the array consisting of ImageCodecInfo classes)

ImageCodecInfo * encoders // Encoder information pointer

);

In the GetImageEncoders function, parameters Numencoders and SIZE are returned by GetImageEncodersSize. The following code can find the encoding method of the specific format image in the registry:

Int getImageClsid (Const Wchar * Format, CLSID * PCLSID)

{// Get the encoding value of the image file of Format, access to the COM component of the format image

// GUID value is saved in PCLSID

UINT NUM = 0;

Uint size = 0;

ImageCodecInfo * pimagecodecinfo = null;

GetImageEncodersSize (& Num, & size);

IF (size == 0)

Return false; // Code information is not available

//Allocate memory

PimageCodecInfo = (ImageCodecInfo *) (Malloc (size));

IF (pimagecodecinfo == null)

Return false; // Assignment failed

/ / Get all information about the encoding method available in your system

GetImageEncoders (NUM, SIZE, PIMAGECODECINFO); / / Find whether the Format format is supported in the available coded information

For (uint i = 0; i

{// mimeType: specific description of encoding mode

IF (WCSCMP (PimageCodecinfo [i] .Mimetype, Format) == 0)

{

* pclsid = pimagecodecinfo [i] .clsid;

FREE (PIMAGECODECINFO);

Return True;

}

}

FREE (PIMAGECODECINFO);

Return False;

}

With this understanding, it is not difficult to achieve the browsing and conversion of multi-format images. In order to tell the convenience, first establish an SDI project imageShow in the VC, first prepare the code to use GDI declaration and initialization and destruction, the specific code is as follows:

#include "gdiplus.h"

Using namespace gdiplus;

CIMAGESHOWVIEW :: CIMAGESHOWVIEW ()

{

// Initialize GDI

GDIPLUSSTARTUPINPUT GDIPLUSSTARTUPINPUT;

Ulong_ptr gdiplustoken;

GDIPLUSSTARTUP (& gdiplustoken, & gdiplusstartupinput, null);

}

CIMAGESHOWVIEW :: ~ CIMAGESHOWVIEW ()

{

// destroy GDI

Ulong_ptr gdiplustoken;

GDIPLUSSHUTDOWN (GDIPLUSTOKEN);

}

Then by class wizard, "Open" in the "File" menu, "Open" and "Save As", for programming, this program only stores the currently open image file directly as BMP file (actually Files saved into other formats are also very simple, just analyzing the file name). In addition, in order to pass the file name of the file on and saved files, you should first add "CSTRING STROPENFILENAME" in the CIMAGESHOWVIEW class. The response code "Open" and "Save As" is as follows, everyone can understand the programming ideas through the annotation of the code, should there be a problem:

Wchar * Towchar (Char * STR)

{

// In GDI , all parameter types of characters are all Wchar types.

/ / This function converts the traditional string

Static Wchar Buffer [1024];

Wcsset (buffer, 0);

MultibyToWideChar (CP_ACP, 0, STR, STRLEN (STR), BUFFER, 1024);

Return buffer;

}

Void CIMAGESHOWVIEW :: onfileopen ()

{

// This program can open image files in various types of common formats

Static char szfilter [] = "Frequent format graphics file (*. *) | *. * |

CfileDialog DlgchoseImage (1, NULL, NULL, NULL, SZFILTER);

IF (DLGCHOSEIMAGE.DOMODAL () == iDok)

{

StropenFileName = DLGCHOSEIMAGE.GETPATHNAME ();

/ / Immediately after opening the file (redraw the customer window)

this-> invalidate ();

}

}

Void CIMAGESHOWVIEW :: onfilesaveas ()

{

IF (StropenFileName.Isempty ()) {

AFXMessageBox ("" ​​There is currently no image file, can not be saved! ");

Return;

}

// Establish a graphic object

Graphics graphics (getDC () -> m_hdc);

// Load the currently opened graphics file

Image Image (TowChar (StropenFileName.getBuffer ())))))));

Cstring strfilesave;

/ / When the image in other formats is saved as BMP file

Static char szfilter [] = "bitmap (* .bmp) | * .bmp |

Cfiledialog DlgchoseImage (0, "BMP", NULL, NULL, SZFILTER;

IF (DLGCHOSEIMAGE.DOMODAL () == iDok)

{

Strfilesave = DLGCHOSEIMAGE.GETPATHNAME ();

CLSID CLSID;

IF (L "Image / BMP", & clsid)

{

Image.save (Towchar (Strfilesave.getBuffer ()), & clsid, null;

// Display the saved image

StropenFileName = strfilesave;

this-> invalidate ();

}

}

}

Finally, in order to display the effect before and after the browsing image conversion, the image before and after the window should be drawn separately, which is easy, just adding a drawing code in the onDRAW function, as described below:

Void CIMAGESHOWVIEW :: Ondraw (CDC * PDC)

{

CIMAGESHOWDOC * PDOC = getDocument ();

Ask_VALID (PDOC);

// If you don't have a display graphic file, you don't have to redraw.

IF (StropenFileName.Isempty ())

Return;

/ / Show the full name of the currently open image file

This-> getParent () -> setWindowText (StropenFileName);

// Establish a graphic object

Graphics graphics (PDC-> M_HDC);

// Load graphic file

Image Image (TowChar (StropenFileName.getBuffer ())))))));

Point Destpoints [3] =

{

Point (0, 0),

Point (image.getwidth (), 0),

Point (0, image.getHeight ())

}

Point * pdestpoints = destpoints;

/ / Display images in the specified area PDestPoints

Graphics.drawImage (& image, pdestpoints, 3);

}

Before compiling the above programs, you should connect to the project, otherwise "Link 2001" compile error will appear. The program is touched by Visual Studio 6.0, Windows2000 / XP, which can be displayed or converted with BMP, GIF, JPEG, EXIF, PNG, TIFF, ICON, WMF, EMF, and more. It should be noted that this article only describes the basic principles of GDI programming, in fact, GDI application is far more than this. After the back of GDI , there is a surprise you unexpected! Hey, is this program running a picture browsing program like ACDSEE? If you have improved this program, you also make a more powerful image handler. The procedures mentioned herein can be downloaded from the "Http://nationaltax.home.chinaren.com" "Http://nationaltax.home.chinaren.com". For GDI programming help, everyone can go to Microsoft's MSDN website to check. If you have Visual Studio .NET, this is best because all the information required for GDI programming in the attached MSDN for Visual Studio.net 7.0.

转载请注明原文地址:https://www.9cbs.com/read-28577.html

New Post(0)