VC Study Notes: Image Display

Source: Internet
Author: User

VC Study Notes: Image Display

 SkySeraph OCT.30th 2010 HQU

Email-zgzhaobo@gmail.com QQ-452728574

Latest Modified Date: OCT.30th 2010 HQU

Principles and Methods

  • Show images in resources

(1) Load bitmap from resources

● Define the CBitmap m_Bitmap Member of the bitmap object;

● Call the CBitmap member function LoadBitmap (), for example, m_Bitmap.LoadBitmap (IDB_BITMAP1 );

● The input LoadBitmap parameter is the identifier assigned when a bitmap is generated in the graphic editor or imported from a bitmap file.

(2) generate the memory device context object associated with the bitmap

CDC MemDC;

MemDC. CreateCompatibleDC (NULL );

MemDC. SelectObject (& m_Bitmap );

(3) display bitmap

CClientDC ClientDC (this );

Bitmap bm;

M_Bitmap.GetObject (sizeof (BM), & BM );

ClientDC. BitBlt (X, Y, // the horizontal and vertical coordinates of the target device Logic

BM. bmWidth, BM. bmHeight, // display the pixel width and height of the bitmap.

& MemDC, // device context object for displaying bitmap data

0, 0, // horizontal and vertical coordinates in the source data

SRCCOPY); // bit Operation Method

Features: This method is fast, but not flexible, and can increase the size of executable files.

 

  • Show images on disk

Steps:

HBITMAP * hBitmap; // defines the bitmap object handle.

Bitmap bm;

CDC MemDC;

CClientDC ClientDC (this );

MemDC. CreateCompatibleDC (& ClientDC );

HBitmap = (HBITMAP *): LoadImage (AfxGetInstanceHandle (), // get the application handle

Includemo1.bmp ", // bitmap file name

IMAGE_BITMAP, // Windows bitmap type

0, 0, LR_LOADFROMFILE); // retrieves bitmap data from the file

MemDC. SelectObject (hBitmap );

: GetObject (hBitmap, sizeof (BM), & BM );

ClientDC. BitBlt (......) // The format and method are used together.

Features: the bitmap display speed is a little slower than the previous one, but it is more flexible. You can change bitmap files without re-compiling the source program, it also reduces the size of executable files.

 

 

Program Template

  • Show images in resources/draw images in device context [1]

Method 1

Void coutputbmp view: OnDraw (CDC * pDC)

{

Coutputbmp Doc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CDC memDC; // defines a device context

MemDC. CreateCompatibleDC (pDC); // obtain compatible device context

CBitmap bmp;

Bmp. LoadBitmap (IDB_BKBITMAP );

MemDC. SelectObject (& bmp); // select a bitmap object

PDC-> BitBlt (180,180, & memDC, SRCCOPY); // draw a bitmap

CRect rc (30, 20, 210,200 );

CBrush brush (RGB (0, 0 ));

PDC-> FrameRect (rc, & brush); // draw a rectangular border

Rc. OffsetRect (); // move the area

BITMAP BitInfo;

Bmp. GetBitmap (& BitInfo );

Int x = BitInfo. bmWidth;

Int y = BitInfo. bmHeight;

PDC-> StretchBlt (rc. left, rc. top, rc. width (), rc. height (), & memDC, x, y, SRCCOPY); // draw a bitmap

PDC-> FrameRect (rc, & brush );

Brush. DeleteObject ();

MemDC. DeleteDC (); // release the device context

Bmp. DeleteObject (); // release the graph object

}

 

  • Attach images to a disk in a window [1]

Cloadbmp view: cloadbmp view () // In the constructor

{

M_hBmp = LoadImage (NULL, "Demo.bmp", IMAGE_BITMAP, LR_LOADFROMFILE); // load the bitmap

}

Void cloadbmp view: OnDraw (CDC * pDC)

{

Cloadbmp Doc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CBitmap bmp;

Bmp. Attach (m_hBmp); // associate a bitmap with a map handle.

CDC memDC;

MemDC. CreateCompatibleDC (pDC); // create a compatible device context

MemDC. SelectObject (& bmp); // select a bitmap object

BITMAP BitInfo; // defines the BITMAP structure.

Bmp. GetBitmap (& BitInfo); // retrieves bitmap Information

Int x = BitInfo. bmWidth; // obtain the bitmap width.

Int y = BitInfo. bmHeight; // obtain the bitmap height.

PDC-> BitBlt (, x, y, & memDC, SRCCOPY); // draw a bitmap to the window

Bmp. Detach (); // detaches the bitmap handle.

MemDC. DeleteDC (); // release the device context

}

  • Direct plotting Based on bitmap data [1]

Char * m_pBmpData; // ① define a buffer in the View class

COutputStreamView: COutputStreamView () // ② the constructor reads the file to the data stream.

{

CFile file; // define a file object

File. Open ("bk.bmp", CFile: modeReadWrite );

Int len = file. GetLength ();

File. Seek (14, CFile: begin); // * Bitmap file Header

M_pBmpData = new char [len-14]; // allocate space for the buffer

File. Read (m_pBmpData, len-14); // Read file data to the buffer

File. Close ();

}

COutputStreamView ::~ COutputStreamView () // releases the buffer in the destructor

{

Delete [] m_pBmpData;

}

Void COutputStreamView: OutputStream (char * pStream) // ④ added member functions, used to output Images Based on Data Streams

{

Char * pHeader = pStream; // defines the temporary buffer

BITMAPINFO BitInfo;

Memset (& BitInfo, 0, sizeof (BITMAPINFO); // initialize the bitmap object.

Memcpy (& BitInfo, pHeader, sizeof (BITMAPINFO); // assign values to bitmap objects

Int x = BitInfo. bmiHeader. biWidth;

Int y = BitInfo. bmiHeader. biHeight;

PHeader + = 40; // point to bitmap data

StretchDIBits (GetDC ()-> m_hDC, 0, 0, x, y, 0, x, y, pHeader, & BitInfo, DIB_RGB_COLORS, SRCCOPY );

}

Void COutputStreamView: OnDraw (CDC * pDC) // 6 draw an image

{

COutputStreamDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

OutputStream (m_pBmpData); // output bitmap

}

 

  • Display JPEG images [1]

① Add member variables to the View class

IStream * m_pStream; // defines the Stream Object

IPicture * m_pPicture; // defines the interface object

OLE_XSIZE_HIMETRIC m_JPGWidth; // Image Width

OLE_YSIZE_HIMETRIC m_JPGHeight; // Image Height

HGLOBAL hMem; // heap handle

CShowJPEGView: CShowJPEGView () // ② constructor: load the IPEG bitmap to the stream from the disk

{

CFile file;

File. Open ("angell.jpg", CFile: modeReadWrite );

DWORD len = file. GetLength ();

HMem = GlobalAlloc (GMEM_MOVEABLE, len); // allocate memory in the heap

LPVOID pData = NULL; // defines a pointer object

PData = GlobalLock (hMem); // lock the memory area

File. ReadHuge (pData, len); // read image data to the heap

File. Close ();//

GlobalUnlock (hMem); // unlock the heap lock

CreateStreamOnHGlobal (hMem, TRUE, & m_pStream); // create a stream in the heap

OleLoadPicture (m_pStream, len, TRUE, IID_IPicture, (LPVOID *) & m_pPicture); // load the image

M_pPicture-> get_Height (& m_JPGHeight); // obtain the Image Height

M_pPicture-> get_Width (& m_JPGWidth );

}

Void cshow1_view: OnDraw (CDC * pDC) // ③ draw JPEG images

{

CShowJPEGDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

M_pPicture-> Render (pDC-> m_hDC, 0,0, (int) (m_JPGWidth/26.45), (int) (m_JPGHeight/26.45)

, 0, m_JPGHeight, m_JPGWidth,-m_JPGHeight, NULL); // draw a JPEG Image

}

 

Description

The biggest disadvantage of Bitmap files

Compression is relatively small and occupies a large amount of space. Therefore, webpages are displayed in IPEG or GIF format.

How to display JPEG images in VC

It is implemented through stream operations. Train of Thought: load the JPEG file to the heap, create a data stream in the heap, and then call the OleLoadPicture function to load the data in the stream to the IPictrue interface, finally, call the Render method in the IPicture interface to output image information.

 

Author: SKySeraph

Email/GTalk: zgzhaobo@gmail.com QQ: 452728574

From: http://www.cnblogs.com/skyseraph/

The copyright of this article is shared by the author and the blog. You are welcome to repost this article. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page. Please respect the author's Labor achievements.

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.