Adding JPEG graphics processing function (heavy stick) using Delphi for Visual C ++

zhaozj2021-02-08  225

Adding a JPEG graphics processing function using Delphi for Visual C

Visual C 's support for JPEG and other common graphics formats have not been a defect for people, compared to, Delphi / C Builder is much stronger in this area. Since JPEG is a graphic format that is often used in practical applications. If you are Visual C users, you will naturally want to make Visual C to process JPEG graphics. Although there are many Visual C libraries that can handle JPEG graphics, someone else is a bit not solely, and there are often some restrictions, such as the famous ImageObject library requires static link to MFC DLL, give Use a lot of inconveniences; if it is ActiveX control, you have to consider how to register and other troublesome problems. In fact, with the power of Delphi's powerful processing function, write a small piece of code, you can make Visual C in a few minutes to use JPEG, DIY feelings are different!

To use the code introduced in this article, your hand should have a delphi (3.0 or more version) and a set of Visual C (5.0 or more versions). Since the code in this article is very simple, the following code is not commented, I believe that friends who have a slightly basis for the two languages ​​are not difficult to understand.

Create a DLL project imageLib in Delphi and add a mainfn.pas unit. The list of files is as follows: // imagelib.dpr Project Library ImageLib;

UsessysUtils, Classes, mainfn in 'mainfn.pas';

Exports? CREATEJPEGIMAGE,? LOADJPEGIMAGE,? DREEJPEGIMAGE,? DRAWJPEGIMAGE

Begund.

///// mainfn.pas unit content /// UNIT mainfn;

InterfaceUsessysUtils, Classes, Windows, Graphics, JPEG

function CreateJPEGImage: TJPEGImage; stdcall; export; function LoadJPEGImage (image: TJPEGImage; szFileName: PChar): LongBool; stdcall; export; procedure FreeJPEGImage (image: TJPEGImage); stdcall; export; procedure DrawJPEGImage (hdc: HDC; x, y: Integer; image: tjpegimage; stdcall;

ImplementationFunction Createjpegimage: TJPEGIMAGE; begin try image: = tjpegimage.create; result: = image; eXCEPT RESULT: = NIL;

function LoadJPEGImage (image: TJPEGImage; szFileName: PChar): LongBool; var strFileName: string; begin try strFileName: = StrPas (szFileName); image.LoadFromFile (strFileName); result: = True; except result: = False; end; end ;

procedure FreeJPEGImage (image: TJPEGImage); begin image.Free; end; procedure DrawJPEGImage (hdc: HDC; x, y: integer; image: TJPEGImage); var Canvas: TCanvas; begin Canvas: = TCanvas.Create; Canvas.Handle: = HDC; Canvas.Draw (X, Y, Image); canvas.free; end;

End.

The DLL generated after the project compilation is done directly in Visual C . The following are generated using Visual C test program content, use Dialog Based Framework: typedef void * PJPEG; / * function CreateJPEGImage: TJPEGImage; stdcall; export; function LoadJPEGImage (image: TJPEGImage; szFileName: PChar): LongBool; procedure FreeJPEGImage (image : TJPEGImage); stdcall; export; procedure DrawJPEGImage (hdc: HDC; x, y: integer; image: TJPEGImage); stdcall; export; * / typedef PJPEG (__stdcall * CREATEJPEGIMAGE) (); typedef BOOL (__stdcall * LOADJPEGIMAGE) ( PJPEG, LPCSTR); typedef void (__stdcall * freejpegimage) (PJPEG); typedef void (__stdcall * drawjpegimage) (HDC, int, int, pjpeg);

class CTDlg: public CDialog {... public: HINSTNACE m_hLib; CREATEJPEGIMAGE pCreateJPEGImage; LOADJPEGIMAGE pLoadJPEGImage; FREEJPEGIMAGE pFreeJPEGImage; DRAWJPEGIMAGE pDrawJPEGImage;} BOOL CTDlg :: OnInitDialog () {... m_hLib = LoadLibrary ( "ImageLib.dll"); pCreateJPEGImage = (CREATEJPEGIMAGE) GetProcAddress (m_hLib, "CreateJPEGImage"); pLoadJPEGImage = (LOADJPEGIMAGE) GetProcAddress (m_hLib, "LoadJPEGImage"); pFreeJPEGImage = (FREEJPEGIMAGE) GetProcAddress (m_hLib, "FreeJPEGImage"); pDrawJPEGImage = (DRAWJPEGIMAGE) GetProcAddress (m_hLib, "DRAWJPEGIMAGE");

Void ctdlg :: ONDESTROY () {if (m_hlib) freeelibrary (m_hlib);}

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

New Post(0)