Bitmap Topics: Displaying From a File

//例子1(对话框)
void CFoodPreview1Dlg::OnBnClickedSelect()
{
	// TODO: Add your control notification handler code here
	TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
	CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter);

	if( dlg.DoModal() == IDOK )
	{
		strPictureName = dlg.GetFileName();
		Invalidate();
	}

	UpdateData(FALSE);
}
 
void CDialog1Dlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	if (IsIconic())
	{
		SendMessage(WM_ICONERASEBKGND,
			    reinterpret_cast<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
	{
		CDialog::OnPaint();
	}
	
	HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
		                               strPictureName,
					       IMAGE_BITMAP,
					       0,
					       0,
					       LR_LOADFROMFILE);
	CBitmap bmpPicture;
	CDC mdcPicture;
	CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);

	CRect rctPicture;
	m_Picture.GetWindowRect(&rctPicture);
		
	mdcPicture.CreateCompatibleDC(&dc);
	CBitmap * bmpPrevious = mdcPicture.SelectObject(bmpFromHandle);

	ScreenToClient(&rctPicture);

	dc.BitBlt(rctPicture.left, rctPicture.top,
		  rctPicture.Width(), rctPicture.Height(),
		  &mdcPicture, 0, 0, SRCCOPY);

	dc.SelectObject(bmpPrevious);
	DeleteObject(bmpHandle);
}
//例子2(mfc单文档视图)
void CShowPicture1View::OnFileOpen()
{
	// TODO: Add your command handler code here
	TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
	CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter);

	if( dlg.DoModal() == IDOK )
	{
		strFilename = dlg.GetFileName();
		Invalidate();
	}
}
void CShowPicture1View::OnDraw(CDC* pDC)
{
	CShowPicture1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	if( strFilename != "" )
	{
		BITMAP bmpProperties;

		// Create a bitmap handle using the name of the file
		HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
			                               strFilename,
					               IMAGE_BITMAP,
					               0,
						       0,
						       LR_LOADFROMFILE);

		CBitmap bmpPicture;
		CDC mdcPicture;

	// Convert the Win32 bitmap handle into an MFC bitmap object
		CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);
		bmpPicture.Attach(bmpHandle);

	// Call the Win32 GetObject() function to get the properties
		// of the bitmap and store them in a BITMAP structure
		::GetObject(bmpPicture,
			    sizeof(bmpProperties),
			    &bmpProperties);

		// Create a compatible device context
		mdcPicture.CreateCompatibleDC(pDC);
		// Select the bitmap into the device context
		CBitmap * bmpPrevious = 
				mdcPicture.SelectObject(bmpFromHandle);

		// Using the dimensions store in the BITMAP object,
		// display the picture
		pDC->BitBlt(0, 0,
			    bmpProperties.bmWidth, bmpProperties.bmHeight,
			    &mdcPicture, 0, 0, SRCCOPY);

		// Get the dimensions of the new picture
		SIZE sizeTotal;
		sizeTotal.cx = bmpProperties.bmWidth;
		sizeTotal.cy = bmpProperties.bmHeight;
		// Change the scrolling area/dimensions of the view
		SetScrollSizes(MM_TEXT, sizeTotal);

		// Restore the bitmap
		pDC->SelectObject(bmpPrevious);
		// Delete the Win32 bitmap handle
		DeleteObject(bmpHandle);
	}
}

引用自:	http://www.functionx.com/visualc/bitmaps/DisplayFromFile.htm
 	http://www.functionx.com/visualc/views/DisplayBitmap.htm  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值