Using Delphi to add JPEG graphics processing to Visual C + +

Source: Internet
Author: User
Tags bool versions

Visual C + + support for common graphics formats such as JPEG is not always a flawed flaw, and delphi/c++ Builder is much stronger in this regard than ever before. Because JPEG is a graphic format that is often used in practical applications, if you are a Visual C + + user, you will naturally want to make Visual C + + work with JPEG graphics.

Although there are now many Visual C + + libraries that can handle JPEG graphics, other people's things are always a bit less handy, and often have some limitations, such as the well-known Imageobject library requirements must be statically linked to the MFC DLL, the use of a lot of inconvenience , and if it's an ActiveX control, you'll have to consider how to sign up for problems. In fact, with the help of the Delphi JPEG graphics powerful processing function, write a small piece of code, you can in a few minutes to let Visual C + + also use the jpeg,diy feeling is different!

To use the code described in this article, you should have a set of Delphi (more than 3.0 versions) and a Visual C + + (more than 5.0 versions). Because the code in this article is very simple, the following code does not comment, I believe that the two sets of language of the basic friends are not difficult to understand.

Create a new DLL project Imagelib in Delphi and add a Mainfn.pas unit. The list of documents is as follows:

////////////////////////////////////

IMAGELIB.DPR Project Content

////////////////////////////////////

Library imagelib;
Uses
Sysutils,
Classes,
Mainfn in ' Mainfn.pas ';
Exports
? Createjpegimage,
? Loadjpegimage,
? Freejpegimage,
? Drawjpegimage;
Begin
End.
///////////////////////////////////////////////
Mainfn.pas Unit Contents
///////////////////////////////////////////////
Unit MAINFN;
Interface
Uses
Sysutils, 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;export;
Implementation
function Createjpegimage:tjpegimage;
Var
Image:tjpegimage;
Begin
Try
Image: = Tjpegimage.create;
Result: = Image;
Except
Result: = nil;
End
End
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.
DLLs that are built after the project has been compiled can be used directly in Visual C + +. The following is the content of the test project generated with Visual C + +, using the 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)
FreeLibrary (M_hlib);
}
void Ctdlg::onpaint ()
{
CPAINTDC DC (this);
Pjpeg pjpeg;
Pjpeg = (*pcreatejpegimage) ();
if (pjpeg)
{
if ((*ploadjpegimage) (Pjpeg, "c:est.jpg"))
(*pdrawjpegimage) (Dc.m_hdc,0,0,pjpeg);
(*pfreejpegimage) (Pjpeg);
}
}

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.