Display a JPG file in your MFC application

Source: Internet
Author: User

In VB, I can create an image control to display a JPG or GIF file, but how do I display a JGP file in my MFC application?

Good question! Sometimes programmers who use VB think this is easy. Just drag an image control into your table, and then you can go down ... However, C + + programmers have to feel trouble and headaches. So what are we going to do, write our own JPG decompression function?

Of course not! In fact, a C + + programmer can use an image control that is very similar to what the VB Programmer uses (which can be said to be almost). I'm not joking. The VB image control is based on a system COM class called "IPicture" (as shown in Figure 1). IPicture manages an Image object and its attributes. An Image object provides an abstraction for the bitmap. Windows provides a standard operation that knows how to handle bmp,jpg and GIF bitmaps. All you have to do is instantiate the IPicture and call the render. You can call a special function called "oleloadpicture" to replace the "CoCreateInstance" that is normally invoked.

IStream* pstm = // 需要一个信息流
IPicture* pIPicture;
hr = OleLoadPicture(pstm, 0, FALSE,
IID_IPicture, (void**)&pIPicture);

OleLoadPicture loads images from the flow of information and creates a new IPicture object that you can use to display images.

rc = // 要在其中显示的矩形
// 转换rc为HIMETRIC
spIPicture->Render(pDC, rc);

IPicture has done all the boring things to calculate whether the image is a Windows bitmap, JPEG, or GIF file, and it can even calculate whether the image is an icon and metafile! Naturally, the details are tricky, so I've written a demo that contains all of them called "Imgview" (as shown in Figure 2).

Figure 2 Imgview

Imgview is a typical MFC document/view architecture application that uses a class that I previously wrote called "CPicture" (as shown in Figure 3) to encapsulate IPicture. CPicture maps Some troublesome Com-type parameters to those that are more easily accepted by MFC programmers. For example, CPicture allows you to load images directly from a file name, such as CFile or CArchive, rather than processing the flow of information, and Cpicture::render completes all the annoying ipicture coordinate conversions that are required for himetric. That way, you don't have to do this. CPicture even has a load function that can load images from your resource data, so to display a resource image, all you have to do is write as follows:

CPicture pic(ID_MYPIC); // 加载pic
   CRect rc(0,0,0,0);   // 使用默认 rc
   pic.Render(pDC, rc);  // 显示它

What makes it easier to work? Cpicture::render can get a rectangle in which you want to display the image. IPicture can stretch the image appropriately. If you pass an empty rectangle, CPicture uses the original size of the image and does not stretch it. For the image itself, CPicture is looking for a resource type called "image", so you must write your RC file as follows:

IDR_MYPIC IMAGE MOVEABLE PURE "res\\MyPic.jpg"

Overall, CPicture is rather brainless. It has an ATL ccomqiptr ingenious pointer to the IPicture interface, where different load functions initialize the interface by calling OleLoadPicture. CPicture provides a generic wrapper function to invoke the ipicture inside. CPicture only encapsulates the IPicture member functions I need to write imgview, because I am such a lazy programmer. If you still need to call ipicture::get_handle or some other less-used IPicture member function, I'm sorry that you have to add the package yourself. At the very least, the code is a trivial matter.

By the way, after I finished cpicture, I thought there was one thing to mention, and that was that I found that a little-known MFC class called "CPictureHolder" did most of the same thing. You can find it in the afxctl.h.

As I mentioned earlier, Imgview is a typical application of the MFC document/view architecture, where the Cpicturedoc and CPictureView classes correspond to the document and view structures respectively. This view is shown in Figure 4. Cpicturedoc some trivial; it uses cpicture to save images--

class CPictureDoc : public CDocument {
protected:
 CPicture m_pict; // 图像
};

, and Cpicturedoc::serialize calls Cpicture::load to read the image from an archive established by MFC.

void CPictureDoc::Serialize(CArchive& ar)
{
 if (ar.IsLoading()) {
  m_pict.Load(ar);
 }
}

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.