Loading a Bitmap and Saving it as a JPEG file


Author : V.Girish

Environment: Compiled using VC6.0 Sp3 and tested using Win95/98 WinNT4.0 and Win 2000

This uses the Intel Jpeg library which can be downloaded from The Intel Site

To load a bitmap, show it in the view and then save it to a JPEG file, follow these steps. :-



 1) Remember to include #include "Intel.h" in your View class header file.

2) In your View's header file, add the following variables
	CDC m_MemDC;
	CBitmap m_bmpView;
	int m_nBmpWidth,m_nBmpHeight;

	CString m_strFileName;
3) In your view class constructor, set
	m_nBmpWidth  = 0; and
	m_nBmpHeight = 0; 
4) In the OnDraw function of your view, add this code.
	CIntelDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if(m_nBmpWidth>0 && m_nBmpHeight >0)
		pDC->BitBlt(0,0,m_nBmpWidth,m_nBmpHeight,&m_MemDC,0,0,SRCCOPY);

	SetScrollSizes(MM_TEXT, CSize(m_nBmpWidth,m_nBmpHeight));
	CChildFrame *pParentFrame = (CChildFrame *)this->GetParentFrame();
	pParentFrame->RecalcLayout();
	ResizeParentToFit(FALSE);
5) In the resource, add a sample bitmap with the ID as IDB_BMP_BKGND

6) In the OnInitialUpdate function of your view, add this code.
	CDC *pDC = this->GetDC();

	m_MemDC.CreateCompatibleDC(pDC);
	m_bmpView.LoadBitmap(IDB_BMP_BKGND);
	m_MemDC.SelectObject(&m_bmpView);

	BITMAP Bitmap;
	m_bmpView.GetBitmap(&Bitmap);

	m_nBmpHeight = Bitmap.bmHeight;
	m_nBmpWidth = Bitmap.bmWidth;

	CSize size(m_nBmpWidth,m_nBmpHeight);
	SetScrollSizes(MM_TEXT,size);	

	SetScaleToFitSize(size);

	this->ReleaseDC(pDC);
7) In the OnFileOpen function, add this code in the View class.
	static char BASED_CODE szFilter[] = "Bitmap Files (*.bmp)|*.bmp||";
  
	CFileDialog FileDlg(TRUE,"Graphic Files",NULL,OFN_OVERWRITEPROMPT,szFilter); // select filename

	if(FileDlg.DoModal()==IDOK)
	{
		CString strFileName;
		m_strFileName = FileDlg.GetPathName();

		CDC *pDC = this->GetDC();

		if(m_MemDC)
			m_MemDC.DeleteDC();

		if(m_bmpView.m_hObject!=NULL)
		{
			m_bmpView.Detach();
			m_bmpView.DeleteObject();
		}
		

		m_MemDC.CreateCompatibleDC(pDC);
		
		HBITMAP hBitmap = NULL; 
		hBitmap = (HBITMAP)LoadImage(NULL, m_strFileName, IMAGE_BITMAP, 0, 0, 
					LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); 

		m_bmpView.Attach(hBitmap);

		m_MemDC.SelectObject(&m_bmpView);

		BITMAP Bitmap;
		m_bmpView.GetBitmap(&Bitmap);

		m_nBmpHeight = Bitmap.bmHeight;
		m_nBmpWidth = Bitmap.bmWidth;

		CSize size(m_nBmpWidth,m_nBmpHeight);
		SetScrollSizes(MM_TEXT,size);	

		this->ReleaseDC(pDC);
	}

	RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW|RDW_ERASE);
8) In the OnFileSave function, add this code.
	char *input_filename = (char *) (LPCTSTR)m_strFileName;
	char *output_filename = "c:\\output.jpg";
	
	JPEG_CORE_PROPERTIES jcprops;
	ZeroMemory( &jcprops, sizeof( JPEG_CORE_PROPERTIES ) );
	
	BeginWaitCursor();

	if( ijlInit( &jcprops) != IJL_OK )
        {
	  TRACE( "Can't initialize Intel(R) JPEG library\n" );
          AfxThrowUserException();
	}
	

	HANDLE hbitmapfile;
	hbitmapfile = CreateFile(input_filename,GENERIC_READ,0,0,OPEN_EXISTING,0,0);

	HANDLE hFileMappingObject;

	hFileMappingObject = CreateFileMapping(hbitmapfile,0,PAGE_READONLY,0,0,0);

	unsigned char *bitmapptr;

	bitmapptr = (unsigned char *)MapViewOfFile(hFileMappingObject,FILE_MAP_READ,0,0,0);

	BITMAPFILEHEADER *bmfh = (BITMAPFILEHEADER *) bitmapptr;
	BITMAPINFOHEADER *bmih = (BITMAPINFOHEADER *)(bitmapptr + sizeof(BITMAPFILEHEADER));

	long DIBLineSize = (bmih->biWidth * 3)/4*4;

	jcprops.DIBChannels = 3;
	jcprops.DIBColor = IJL_BGR;
	jcprops.DIBHeight = -bmih->biHeight;
	jcprops.DIBWidth = bmih->biWidth;
	jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth,jcprops.DIBChannels);

	jcprops.JPGHeight = bmih->biHeight;
	jcprops.JPGWidth = bmih->biWidth;

	jcprops.JPGFile = output_filename;

	jcprops.DIBBytes = bitmapptr + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

	ijlWrite(&jcprops,IJL_JFILE_WRITEWHOLEIMAGE);

	ijlFree(&jcprops);

	UnmapViewOfFile(bitmapptr);
	CloseHandle(hFileMappingObject);
	CloseHandle(hbitmapfile);
9) Copy the ijl15.dll to your debug/release folder path

10) In the Project-->Settings menu, go to the link tab and add ijl15.lib in the Object/Library modules edit box.

11) Compile your program and execute it. Thats all folks. Have a nice time.

Downloads

Download demo project - 33KB
Download the Intel Jpeg library

NOTE : I havent added any testing here. Be sure that you test every operation carefully
before you actually develop a product and ship it to your customers.
Hosted by www.Geocities.ws

1