Click here to Skip to main content
15,887,135 members
Articles / Desktop Programming / MFC
Article

An Image (GIF, JPEG, BMP, ICO, WMF and EMF) Viewer

Rate me:
Please Sign up or sign in to vote.
3.68/5 (15 votes)
25 Oct 2002 286.3K   7.1K   57   47
A sample that can load, display, and print graphics files.

ImgViewer

Introduction

This sample is an MFC application using the document/view architecture that can load, display and print graphics files such as GIF, JPEG, BMP (bitmap), ICO (icon), WMF (metafile) and EMF (Win32-enhanced metafile). It shows

  • how to use the OleLoadPicturePath() function and the IPicture interface to load and display those graphics files.

  • (Look into the ImgViewerDoc.cpp, ImgViewerView.cpp and MainFrm.cpp files.)
  • how to support more than one file extension per MFC document type.

  • (Look into the MulExtDocTmp.cpp, MulExtDocTmp.h and ImgViewer.cpp files.)

References:

Please see the following article in the Microsoft Knowledge Base:

Q218972 How To Load and Display Graphics Files w/LOADPIC.EXE
(Note that LOADPIC.EXE does not handle palettes. As a result, it won't display a 256-color image correctly in 256-color mode. Also, it uses the OleLoadPicture() function instead of the OleLoadPicturePath() function.)

Q141921 HOWTO: How to Support Two File Extensions per MFC Document Type
Q198538 DocMgrEx.exe Assoc Multiple File Extensions w/1 Doc Type

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugFilterExt value need Empty... T_T;; Pin
shint15-Feb-16 16:53
shint15-Feb-16 16:53 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 20:57
professionalManoj Kumar Choubey26-Feb-12 20:57 
QuestionMultipage EMF viewer Pin
Loris Rinaldo10-Nov-11 4:22
Loris Rinaldo10-Nov-11 4:22 
AnswerRe: Multipage EMF viewer Pin
Chen Su10-Nov-11 15:14
Chen Su10-Nov-11 15:14 
GeneralRe: Multipage EMF viewer Pin
Loris Rinaldo18-Nov-11 10:19
Loris Rinaldo18-Nov-11 10:19 
GeneralLINK : fatal error LNK1104: cannot open file "uafxcwd.lib" Pin
Bee cute3-Mar-11 3:26
Bee cute3-Mar-11 3:26 
AnswerRe: LINK : fatal error LNK1104: cannot open file "uafxcwd.lib" Pin
Albert Holguin19-Apr-11 7:13
professionalAlbert Holguin19-Apr-11 7:13 
Generalcontact Pin
lihuai26-Jan-10 22:30
lihuai26-Jan-10 22:30 
GeneralImage disappear Pin
Md. Mazharul Islam Khan15-Mar-07 23:09
Md. Mazharul Islam Khan15-Mar-07 23:09 
GeneralRe: Image disappear Pin
Chen Su16-Mar-07 8:40
Chen Su16-Mar-07 8:40 
GeneralImage disappear Pin
Md. Mazharul Islam Khan15-Mar-07 23:08
Md. Mazharul Islam Khan15-Mar-07 23:08 
Generalplease help me make program contrast bmp Pin
HoangManh4-Jun-06 17:45
HoangManh4-Jun-06 17:45 
GeneralHot to print Images Pin
gonetil6-Apr-05 5:43
gonetil6-Apr-05 5:43 
GeneralRe: Hot to print Images Pin
Chen Su6-Apr-05 16:17
Chen Su6-Apr-05 16:17 
GeneralRotate a picture Pin
Alex Evans3-Jan-05 16:35
Alex Evans3-Jan-05 16:35 
GeneralIPicutre data Pin
Marcin Kowalski19-Oct-04 6:55
Marcin Kowalski19-Oct-04 6:55 
Generalplease help me correct this Pin
ipichet13-Apr-04 22:41
ipichet13-Apr-04 22:41 
I wrote a code look like this to use in my little work. Its duty is
1. Load BMP image as resource by LoadBitmap function (4 images)
2. Shows one image on screen
3. Make some change by using the images
4. Show result

Every thing works well(But it is very slow when run). However after I changed the code by adding WriteDIB() (I want to save the result image into file) and compiled it, it can perform on its duty except for sainge the file(there is notthing happen). The program still run without any error.

////////////////////////////////////////////////////////////////////
// Invert.cpp
#include <afxwin.h>
#include <math.h>
#include "resource.h"

struct pixel{
public: // datamembers
BYTE red, green, blue;
};

class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMyFrameWnd : public CFrameWnd
{
private:
CBitmap m_bmSource0D, m_bmSource4D, m_bmSource8D, m_bmSource12D, m_bmSourceMask;
CDC m_dcSource0D, m_dcSource4D, m_dcSource8D, m_dcSource12D, m_dcSourceMask;
CDC m_dcOffScreen;
BITMAP m_bmp;
COLORREF m_crC0D, m_crC4D, m_crC8D, m_crC12D, m_crCMask, m_crCOut; // Pixel Color
BYTE m_nR; // Red
BYTE m_nG; // Green
BYTE m_nB; // Blue
int Id, Id1, Id2, Id3, Id4;
int m_nX;
int m_nY;
double phi;

public:
CBitmap m_bmOffScreen;

public:
BOOL WriteDIB(HBITMAP hBitmap);
double FindArctan2Reverse(double upper, double lower);
double FindArctan2(double upper, double lower);
double FindArctan(double upper, double lower);
double checkdirection(double direct);
CMyFrameWnd();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

BOOL CMyWinApp::InitInstance()
{
m_pMainWnd = new CMyFrameWnd();
m_pMainWnd->ShowWindow(SW_MAXIMIZE);
m_pMainWnd->UpdateWindow();

return TRUE;
}

CMyFrameWnd::CMyFrameWnd()
{
Create(NULL, "Invert", WS_POPUPWINDOW | WS_CAPTION, CRect(0, 0, 512, 480));
CenterWindow(NULL);

}

afx_msg int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CPaintDC dc(this);

// Source0D
m_bmSource0D.LoadBitmap(IDB_BITMAP0D);
m_dcSource0D.CreateCompatibleDC(&dc);
m_dcSource0D.SelectObject(&m_bmSource0D);

// Source4D
m_bmSource4D.LoadBitmap(IDB_BITMAP4D);
m_dcSource4D.CreateCompatibleDC(&dc);
m_dcSource4D.SelectObject(&m_bmSource4D);

// Source8D
m_bmSource8D.LoadBitmap(IDB_BITMAP8D);
m_dcSource8D.CreateCompatibleDC(&dc);
m_dcSource8D.SelectObject(&m_bmSource8D);

// Source12D
m_bmSource12D.LoadBitmap(IDB_BITMAP12D);
m_dcSource12D.CreateCompatibleDC(&dc);
m_dcSource12D.SelectObject(&m_bmSource12D);

// SourceMask
m_bmSourceMask.LoadBitmap(IDB_BITMAPMASK);
m_dcSourceMask.CreateCompatibleDC(&dc);
m_dcSourceMask.SelectObject(&m_bmSourceMask);

// Off-Screen
m_bmOffScreen.LoadBitmap(IDB_BITMAP0D);
m_dcOffScreen.CreateCompatibleDC(&dc);
m_dcOffScreen.SelectObject(m_bmOffScreen);


m_bmSource0D.GetBitmap(&m_bmp);

return 0;
}

afx_msg void CMyFrameWnd::OnPaint()
{
CPaintDC dc(this);
dc.BitBlt(0, 0, m_bmp.bmWidth, m_bmp.bmHeight, &m_dcOffScreen, 0, 0, SRCCOPY);
}

afx_msg void CMyFrameWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
const double pi = 3.1416;
for (m_nY = 0; m_nY < m_bmp.bmHeight; m_nY++)
{
for (m_nX = 0; m_nX < m_bmp.bmWidth; m_nX++)
{
if(m_crCMask = m_dcSourceMask.GetPixel(m_nX, m_nY) == 0x000000)
{
m_nR = 0x62;
m_nG = 0x6E;
m_nB = 0x69;

m_crCOut = m_nR + (m_nG << 8) + (m_nB << 16);
m_dcOffScreen.SetPixel(m_nX, m_nY, m_crCOut);
}
else
{
m_crC0D = m_dcSource0D.GetPixel(m_nX, m_nY);
m_crC4D = m_dcSource4D.GetPixel(m_nX, m_nY);
m_crC8D = m_dcSource8D.GetPixel(m_nX, m_nY);
m_crC12D = m_dcSource12D.GetPixel(m_nX, m_nY);

Id1 = GetBValue(m_crC0D);
Id2 = GetBValue(m_crC4D);
Id3 = GetBValue(m_crC8D);
Id4 = GetBValue(m_crC12D);

double numerator = Id1 - Id3;
double denominator = Id2 - Id4;

phi = (pi/8) - 0.25 * FindArctan(numerator, denominator);

checkdirection(phi);

int m_nConvertToGrayScale = (int)((phi * 500 / pi) + 0.5);

if(m_nConvertToGrayScale > 255) m_nConvertToGrayScale = 255;
if(m_nConvertToGrayScale < 0) m_nConvertToGrayScale = 0;

m_nR = m_nConvertToGrayScale;
m_nG = m_nConvertToGrayScale;
m_nB = m_nConvertToGrayScale;

m_crCOut = m_nR + (m_nG << 8) + (m_nB << 16);
m_dcOffScreen.SetPixel(m_nX, m_nY, m_crCOut);
}
}
}
m_dcSource0D.BitBlt(0, 0, m_bmp.bmWidth, m_bmp.bmHeight, &m_dcOffScreen, 0, 0, SRCCOPY);
RedrawWindow(NULL, NULL, RDW_INVALIDATE);
}

CMyWinApp MyWinApp;


double CMyFrameWnd::checkdirection(double direct)
{
const double pi = 3.1416;

if(Id3 < Id1 && Id3 < Id2 && Id3 < Id4)
{
if(Id4 < Id2)
direct = direct + (pi / 4);
}
else if(Id4 < Id1 && Id4 < Id2 && Id4 < Id3)
{
direct = direct + (pi / 4);
}
else
{
direct = direct;
}

return direct;
}

double CMyFrameWnd::FindArctan(double upper, double lower)
{
double fraction = upper / lower;
double resultatan = atan(fraction);

return resultatan;

}

afx_msg void CMyFrameWnd::OnRButtonUp(UINT nFlags, CPoint point)
{

CBitmap m_bmpbuff;
pixel m_pBmp;

m_bmpbuff.CreateBitmapIndirect(&m_bmp);

for (m_nY = 0; m_nY < m_bmp.bmHeight; m_nY++)
{
for (m_nX = 0; m_nX < m_bmp.bmWidth; m_nX++)
{
m_pBmp.red = 123;
m_pBmp.green = 123;
m_pBmp.blue = 123;
m_bmpbuff.SetBitmapBits(sizeof(pixel), &m_pBmp);
}
}

HBITMAP hBmp = (HBITMAP)m_bmpbuff;

WriteDIB(hBmp);
}

BOOL CMyFrameWnd::WriteDIB(HBITMAP hBitmap)
{
BITMAPFILEHEADER bmphdr;
LPBITMAPINFOHEADER bmpInfo;
if (!hBitmap)
return FALSE;
CString szFile;
szFile = "PSD";
CFile file;
if(!file.Open(szFile, CFile::modeWrite|CFile::modeCreate))
AfxMessageBox("Not Done Save");
return FALSE;
bmpInfo = (LPBITMAPINFOHEADER)hBitmap;
int nColors = 1 << bmpInfo->biBitCount;
// Fill in the fields of the file header
bmphdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
bmphdr.bfSize = GlobalSize (hBitmap) + sizeof( bmphdr );
bmphdr.bfReserved1 = 0;
bmphdr.bfReserved2 = 0;
bmphdr.bfOffBits = (DWORD) (sizeof( bmphdr ) + bmpInfo->biSize + nColors * sizeof(RGBQUAD));

// Write the file header
file.Write( &bmphdr, sizeof(bmphdr) );

// Write the DIB header and the bits
file.Write(bmpInfo, GlobalSize(hBitmap));

return TRUE;
}
Generalmake a 2-d graphics editor for window Pin
anurag somani10-Apr-04 0:39
anurag somani10-Apr-04 0:39 
GeneralDialog instead of Document ... Pin
Anonymous12-Jan-04 22:36
Anonymous12-Jan-04 22:36 
GeneralRe: Dialog instead of Document ... Pin
Chen Su13-Jan-04 8:18
Chen Su13-Jan-04 8:18 
GeneralBug in the Program Pin
siquin4-Oct-03 16:55
siquin4-Oct-03 16:55 
GeneralRe: Bug in the Program Pin
Chen Su4-Oct-03 18:42
Chen Su4-Oct-03 18:42 
GeneralRe: Bug in the Program Pin
tico_tech6-Oct-03 10:23
tico_tech6-Oct-03 10:23 
QuestionHow 2 Save The Loaded File and then reload when required Pin
Vikrant Vikrant17-Jul-03 2:41
Vikrant Vikrant17-Jul-03 2:41 
Generalhelp me Pin
Anonymous5-Nov-02 14:53
Anonymous5-Nov-02 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.