Use other compilers to enhance visual C ++ graphics

Source: Internet
Author: User

Use other compilers to enhance visual C ++ graphics

It is undeniable that visual C ++ is a very powerful programming environment. However, Visual C ++ has many limitations. For example, it is far less convenient than Delphi/BCB in terms of interface construction and graphic processing. However, we can use the functions provided by these compilers to make up for the shortcomings of Visual C ++ in this regard.

I often see topics on various programming forums about how to load images from. BMP files and how to store images to. BMP files. In fact, loading images from BMP is very simple: A LoadImage function call can solve the problem. However, how can we store images in BMP files? Although I know the structure of the BMP file, I will not write such a function or class from the beginning, which is equal to "re-inventing the wheel ". Although I also know that there are many such libraries availableProgramYou will have a sense of accomplishment when solving your own problems. Here, I have a way to be lazy: Just write a dozen linesCodeWith an extra step, I can fix it. I don't need to touch bitmapxxxxheader of the faulty tooth, do not care about the clipboard and pixel format, and do not need to consider the travel encoding, you only need a Delphi compiler. Let's see how I did it:

Open Delphi and generate a new DLL project. In the DLL, export the savebitmaptofile function as follows:

Unit imagedll;

Interface

Uses
Sysutils, classes, windows, graphics;

Function savebitmaptofile (HBM: hbitmap; szfilename: pchar): Boolean; stdcall; export;

Implementation
Function savebitmaptofile (HBM: hbitmap; szfilename: pchar): Boolean;
VaR
BM: tbitmap;
Strfilename: string;
Begin
Result: = true;
Try
BM: = tbitmap. Create;
Try
BM. Handle: = HBM;
Strfilename: = strpas (szfilename );
BM. savetofile (strfilename );
Except
Result: = false;
End
Finally
BM. Free;
End;
End;

How? Is it very simple? As long as the DLL is compiled successfully, my work will be completed. However, it is better to test the actual effect in Visual C ++. Below is the test code I added for a dialog Based Visual C ++ project:

Typedef bool (_ stdcall * pfnsavebitmaptofile) (hbitmap, lpcstr );

Bool ctestdlg: oninitdialog ()
{
M_hlib = loadlibrary ("delphiimage. dll ");
}

Void ctestdlg: ondestroy ()
{
If (m_hlib)
Freelibrary (m_hlib );
}

Void ctestdlg: onbutton1 ()
{
Hbitmap HBM = (hbitmap) LoadImage (AfxGetInstanceHandle (), "C: // windows // installer .bmp", image_bitmap, 0, 0, lr_loadfromfile );
If (HBM)
{
Pfnsavebitmaptofile = (pfnsavebitmaptofile) getprocaddress (m_hlib, "savebitmaptofile ");
If (pfnsavebitmaptofile)
(* Pfnsavebitmaptofile) (HBM, "C: // test.bmp ");
Else
Afxmessagebox ("getprocaddress failed! ");
Deleteobject (HBM );
}
}

As long as you have a little understanding of VC and Delphi, the above code should not be a problem for you. If you want to, you can also add support for ICO, WMF, and even JPG files to the DLL! You only need to introduce JPEG units in the DLL project of Delphi.

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.