VB omnipotent 2: using GDI + to process image format conversion

Source: Internet
Author: User
VB omnipotent 2: using GDI + to process image format conversion -- Author: bell Today, I want to write another article about file format conversion in VB. For Windows programming, in addition to processing network protocols, it is more important to process several types of file formats. The mastery of these file formats is conducive to making your programs and Windows systems closer. Our programmers are often troubled by the following formats of Files: 1. image file formats: BMP, JPG, GIF, and PNG; 2. audio file formats: for example: wav, Midi, etc.; 3. Video file formats: such as Avi, rmvb, and RM; 4. Private, non-public file formats: such as Doc and XLS. In fact, any language can be processed when you understand the file format standards. It is not limited to a development language. Therefore, I would like to encourage VB supporters here. VB, as a development language for powerful Windows software, is a natural language for processing these file formats. Today, we will only introduce some tips for converting the image format. First, let's get to know the most basic graph file structure "BMP ".BMP is an image file format unrelated to hardware devices and is widely used. It does not use any compression, so the space occupied by BMP files is large. Because the BMP file format is a standard for exchanging graph-related data in windows, the graphic image software running in Windows supports the BMP image format. The BMP file format is also considered as a basic image file format. BMP file structure: When a BMP file stores data, the image scan mode is in the order of left to right and bottom to top. As shown in: we drew a point in the drawing version, which has only three pixels. We dyed it red, purple, and yellow. At this time, we saved it as a BMP file. Then we open this BMP with a UltraEdit-32 and select the hexadecimal Editor: Then, we observe the format in this file: The three colors we put in respectively are: Red: 255, 0, 0 purple: 128, 128, 255 Yellow: 255, 255, 0 at this time, we compare the bytes in the file: Is this exactly the value of these three colors? Therefore, BMP format graphics files are not compressed, but are honestly marked with the position of each pixel, telling the graphics software which pixel should show what color. The preceding bytes describe the image file type and image depth. So good, because it is too large, so someone studied the BMP compression algorithms, including JPEG, GIF, and PNG. ACDSee, a commonly used image-watching software, is a great tool for converting graphic files in various formats. Isn't it necessary to study compression algorithms like ACDSee or even Photoshop? I tell you that we don't need to study the compression algorithm at all. Windows provides a good tool function-GDI +. With this function tool group, you can convert any image file format. The conversion method is as follows:  1. Declare functions and structuresPrivate type guid
Data1 as long
Data2 as integer
Data3 as integer
Data4 (0 to 7) as byte
End type
Private type gdiplusstartupinput
Gdiplusversion as long
Debugeventcallback as long
Suppressbackgroundthread as long
Suppressexternalcodecs as long
End type
Private type encoderparameter
Guid as guid
Numberofvalues as long
Type as long
Value as long
End type
Private type encoderparameters
Count as long
Parameter as encoderparameter
End typeprivate declare function gdiplusstartup lib "gdiplus" (token as long, inputbuf as gdiplusstartupinput, optional byval outputbuf as long = 0) as long
Private declare function gdiplusshutdown lib "gdiplus" (byval token as long) as long
Private declare function gdipcreatebitmapfromhbitmap lib "gdiplus" (byval HBM as long, byval hpal as long, bitmap as long) as long
Private declare function gdipdisposeimage lib "gdiplus" (byval image as long) as long
Private declare function gdipsaveimagetofile lib "gdiplus" (byval image as long, byval filename as long, clsidencoder as guid, encoderparams as any) as long
Private declare function clsidfromstring lib "OLE32" (byval STR as long, ID as guid) as long
Private declare function copymemory lib "Kernel32" alias "rtlmovememory" (DEST as any, Src as any, byval CB as long) as long 2. Convert BMP to JPGDim quality as byte
Dim pai_colordepth as long
Dim pai_compression as long
Screen. mousepointer = vbhourglass
Dim TSI as gdiplusstartupinput
Dim lres as long
Dim lgdip as long
Dim lbitmap as long
Dim aencparams () as byte
Dim PICT as stdpicture

On Error goto errhandle:

Quality = 80
Pai_colordepth = 24
Pai_compression = 6
PICT = picture1.picture

Tsi. gdiplusversion = 1
Lres = gdiplusstartup (lgdip, TSI)
Lres = gdipcreatebitmapfromhbitmap (Pict. Handle, 0, lbitmap)
Dim tjpgencoder as guid
Dim tparams as encoderparameters
Clsidfromstring strptr ("{557cf401-1a04-11d3-9a73-440f81ef32e}"), tjpgencoder
Tparams. Count = 1
With tparams. Parameter 'quality
Clsidfromstring strptr ("{1d5be4b5-fa4a-452d-9cdd-5db35105e7eb}"),. guid
. Numberofvalues = 1
. Type = 4
. Value = varptr (quality)
End
Redim aencparams (1 to Len (tparams ))
Call copymemory (aencparams (1), tparams, Len (tparams) The above program is to convert the image in picture1 to JPG, and finally we get an array of JPG, of course, you can handle it at will, store it as a file, or handle it separately. Do you think the conversion of image formats using this method is related to VB itself? Bell

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.