VC: add images

Source: Internet
Author: User

Overall: for common BMP \ DIB format images, The LoadImage () function is provided in GDI; the OleLoadPicture () function is provided for files in formats such as PNP, JPG, GIF, and VC ++.

I,

1. Create a single-document application and add functions and variables to the View class as follows:

Void LoadPicture ();

LPPICTURE m_picture;

Float size;

CString filename;

2. LoadPicture (); the function code is as follows:

Void CTestView: LoadPicture ()

// LPPICTRUE LoadPicture (CStirng filename)

{

HANDLE hfile = CreateFile (filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );

_ ASSERTE (INVALID_HANDLE_VALUE! = Hfile );

DWORD dfilesize = GetFileSize (hfile, NULL );

_ ASSERTE (-1! = Dfilesize );

LPVOID p = NULL;

HGLOBAL h = GlobalAlloc (GMEM_MOVEABLE, dfilesize );

 

_ ASSERTE (NULL! = H );

P = GlobalLock (h );

 

_ ASSERTE (NULL! = P );

DWORD dw = 0;

BOOL bread = ReadFile (hfile, p, dfilesize, & dw, NULL );

_ ASSERTE (FALSE! = Bread );

GlobalUnlock (h );

CloseHandle (hfile );

LPSTREAM pstm = NULL;

HRESULT hr = CreateStreamOnHGlobal (h, TRUE, & pstm );

_ ASSERTE (SUCCEEDED (hr) & pstm );

If (m_picture)

M_picture-> Release ();

Hr =: OleLoadPicture (pstm, dfilesize, FALSE, IID_IPicture, (LPVOID *) & m_picture );

_ ASSERTE (SUCCEEDED (hr) & m_picture );

Pstm-> Release ();

// Return m_picture;

}

3. Add two buttons in the toolbar to expand and zoom out. In the View class, add a file to open:

Void CTestView: OnFileOpen ()

{

// TODO: Add your command handler code here

Static char sfile [] = "JPEG (*. JPEG ;*. JPE ;*. JIF ;*. JPG) | *. JPEG ;*. JPE ;*. JIF ;*. JPG | BMP Windows Bitmap (*. BMP ;*. DIB ;*. RLE) | *. BMP ;*. DIB ;*. RLE | PNG (*. PNG) | *. PNG | ";

CFileDialog dlg (TRUE, "jpg", "", OFN_ALLOWMULTISELECT, sfile );

If (IDOK = dlg. DoModal ())

{

Filename = dlg. GetPathName ();

LoadPicture ();

}

Invalidate ();

}

 

Void CTestView: OnBig ()

{

// TODO: Add your command handler code here

Size = size * 0.909;

Invalidate ();

}

 

Void CTestView: OnSmall ()

{

// TODO: Add your command handler code here

Size = size * 1.1;

Invalidate ();

}

4. Compile the OnDraw function code:

Void CTestView: OnDraw (CDC * pDC)

{

CTestDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// TODO: add draw code for native data here

If (m_picture)

{

Long width;

Long height;

M_picture-> get_Width (& width );

M_picture-> get_Height (& height );

Int nwidth = MulDiv (width, GetDeviceCaps (pDC-> GetSafeHdc (), LOGPIXELSX ),

2540 );

Int nheight = MulDiv (height, GetDeviceCaps (pDC-> GetSafeHdc (), LOGPIXELSY ),

2540 );

CRect rc;

GetClientRect (& rc );

M_picture-> Render (pDC-> GetSafeHdc (), (rc. Width ()-nwidth/size)/2, (rc. Height ()-nheight/size)/2,

Nwidth/size, nheight/size, 0, height, width,-height, & rc );

}

 

}

II,

1. Add bitmap Resources

2. functions:

 

Void CTestDlg: OnPaint ()

{

If (IsIconic ())

{

CPaintDC dc (this); // device context for painting

 

SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. GetSafeHdc (), 0 );

 

// Center icon in client rectangle

Int cxIcon = GetSystemMetrics (SM_CXICON );

Int cyIcon = GetSystemMetrics (SM_CYICON );

CRect rect;

GetClientRect (& rect );

Int x = (rect. Width ()-cxIcon + 1)/2;

Int y = (rect. Height ()-cyIcon + 1)/2;

 

// Draw the icon

Dc. DrawIcon (x, y, m_hIcon );

}

Else

{

CPaintDC dc (this );

CRect rect;

GetClientRect (& rect); // obtain the size of the form.

CDC dcMem;

DcMem. CreateCompatibleDC (& dc );

CBitmap BMP background;

BMP background. LoadBitmap (IDB_BITMAP1); // load the background image

BITMAP bitMap;

BMP background. GetBitmap (& bitMap );

CBitmap * pbmpOld = dcMem. SelectObject (& BMP background );

Dc. StretchBlt (, rect. Width (), rect. Height (), & dcMem, bitMap. bmWidth, bitMap. bmHeight, SRCCOPY); // draw a form

// CDialog: OnPaint ();

}

}

III,

1. Bitmap:

CPaintDC dc (this );

CRect rect;

GetClientRect (& rect); // obtain the size of the form.

CDC dcMem;

DcMem. CreateCompatibleDC (& dc );

CBitmap BMP background;

BMP background. LoadBitmap (IDB_BITMAP1); // load the background image

BITMAP bitMap;

BMP background. GetBitmap (& bitMap );

CBitmap * pbmpOld = dcMem. SelectObject (& BMP background );

 

 

Long width;

Width = bitmap. bmWidth;

// M_picture-> get_Width (& width );

Long height;

Height; = bitmap. bmHeight;

// M_picture-> get_Height (& height );

COLORREF color1, color2;

For (long I = 0; I

{

For (long j = 0; j <width; j ++)

{

Color1 = dcMem. GetPixel (j, I );

Color2 = dcMem. GetPixel (width-j, height-I );

DcMem. SetPixel (j, I, color2 );

DcMem. SetPixel (width-j, height-I, color1 );

}

}*/

 

Dc. StretchBlt (, rect. Width (), rect. Height (), & dcMem, bitMap. bmWidth, bitMap. bmHeight, SRCCOPY); // draw a form

 

2. Images:

/* CPaintDC (this );

CDC dcMem;

DcMem. CreateCompatibleDC (& dc );

Long width;

M_picture-> get_Width (& width );

Long height;

M_picture-> get_Height (& height );

COLORREF color1, color2;

For (long I = 0; I

{

For (long j = 0; j <width; j ++)

{

Color1 = dcMem. GetPixel (j, I );

Color2 = dcMem. GetPixel (width-j, height-I );

DcMem. SetPixel (j, I, color2 );

DcMem. SetPixel (width-j, height-I, color1 );

}

}*/

 

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.