Click here to Skip to main content
Click here to Skip to main content

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

By , 25 Oct 2002
 

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

About the Author

Chen Su
Software Developer
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey26 Feb '12 - 20:57 
Nice
QuestionMultipage EMF viewermemberLoris Rinaldo10 Nov '11 - 4:22 
I'm dealing with what I think is a multipage EMF file. Does your viewer handle this type of files?
If so would it be possible for you to send me the compiled version?
 
thanks
Loris
AnswerRe: Multipage EMF viewermemberChen Su10 Nov '11 - 15:14 
I am not sure. It depends on whether IPicture supports multipage EMF files. The EXE is already included in the ZIP file. You can give it a try.
GeneralRe: Multipage EMF viewermemberLoris Rinaldo18 Nov '11 - 10:19 
Thanks, I've tried.
 
The bad news is it only views the first page ... no sign of the rest.
 
The good news is that I found this other piece of software that does the job perfectly:
http://www.lvbprint.de/html/splviewer1.html
 

thanks for you time,
Loris
 
PS why do you write "Download source" when there also is the compiled exe version? Wouldn't "Download source and binary" be better?
GeneralLINK : fatal error LNK1104: cannot open file "uafxcwd.lib"memberBee cute3 Mar '11 - 3:26 
When I rebuild all I have a error
 
"LINK : fatal error LNK1104: cannot open file "uafxcwd.lib""
 

Plz help me to solve that problem !!!
 
If u have a simple project to load image JPG, PLZ post to help me.
 
I do not know how to load image JPG.
 
I have been finding for many days ago but I am not unsuccessful.
 
Regard !!!
 
Thanks 4 all !!!!
AnswerRe: LINK : fatal error LNK1104: cannot open file "uafxcwd.lib"memberAlbert Holguin19 Apr '11 - 7:13 
search your system for the library location, then make sure the linker has a path to the library location identified in the "library default locations" studio options
Generalcontactmemberlihuai26 Jan '10 - 22:30 
Hello. Be able to do with you by e-mail?
My e-mail:ljlwyy213@sina.com
GeneralImage disappearmembermazhar_cse15 Mar '07 - 23:09 
I can load bitmap image in Dialog or Sdi document. But if another window overlap my imageviewer, image is disappeared.
 
How can i solve the problem.
GeneralRe: Image disappearmemberChen Su16 Mar '07 - 8:40 
You need to put your drawing code in OnPaint().
GeneralImage disappearmembermazhar_cse15 Mar '07 - 23:08 
I can load bitmap image in Dialog or Sdi document. But if another window overlap my imgeviewer, image is disappeared.
 
How can i solve the problem.
Generalplease help me make program contrast bmpmemberHoangManh4 Jun '06 - 17:45 
your help me!!!
make contrast bmp or contrast tiff
GeneralHot to print Imagesmembergonetil6 Apr '05 - 5:43 
I am printing images (using Visual C++, MFC application) but they're too small, or too clear. What is going on?
GeneralRe: Hot to print ImagesmemberChen Su6 Apr '05 - 16:17 
Check out the printing code in this sample (CImgViewerView::OnPrepareDC() in ImgViewerView.cpp).
GeneralRotate a picturememberAlex Evans3 Jan '05 - 16:35 
Any idea, how can I ROTATE 90, 180 etc using an IPicture object, once it is loaded and showing on screen...
 
Thanks
Alex
GeneralIPicutre datamemberkowal19 Oct '04 - 6:55 
How to get image's data (pixels value) directly from the IPicture object.
Thanks.
Generalplease help me correct thismemberipichet13 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
#include
#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 windowmemberanurag somani10 Apr '04 - 0:39 
i am doing a project on 2-d graphics editor-something like paintbrush.well i am a novice at it.can anyone experienced give directions??
 
anurag
GeneralDialog instead of Document ...sussAnonymous12 Jan '04 - 22:36 
what would be the coding to load the bmp in a static control of a Dialog tather than in a Document ?
 
Thanks in advance
 
DD
GeneralRe: Dialog instead of Document ...memberChen Su13 Jan '04 - 8:18 
You can use LoadImage() to load the bitmap and call CStatic::SetBitmap() to set the bitmap.
GeneralBug in the Programmembersiquin4 Oct '03 - 16:55 
I was testing this program and noticed that if I open 3 or more image files the program crashes or gives me an error message.
To recreate the problem I clicked on the toolbar button "Open" , then I get my dialog box which gives me the option to open a file. If I do this more than 3 times with the same files I will get an error message saying "cannot open file it is not supported ..."
I have tried it on dfferent computers on a DELL Dimension V333 and on a Compaq Pressario and I get the same problem.
Unsure | :~
GeneralRe: Bug in the ProgrammemberChen Su4 Oct '03 - 18:42 
I cannot reproduce the problem. What OS are you using? Does it happen with any image files or only a specific image file?
GeneralRe: Bug in the Programmembertico_tech6 Oct '03 - 10:23 
I recompiled the code and run the program again and could not reproduce the problem. It must have been something wrong with my compiler. By the way I'm using visual c++ 6.0.
Sorry, false alarm Smile | :)

QuestionHow 2 Save The Loaded File and then reload when requiredmemberVikrant Vikrant17 Jul '03 - 2:41 
Can someone suggest how 2 Save the Image Loaded in a format which can be reloaded whenever required.
I do not wish to copy the original file ,if i have all the image data in my program.
Generalhelp mesussAnonymous5 Nov '02 - 14:53 
I have made an application using Dialog Boxes. I want to add a printing feature
to print the whole Dialog Box.
 
Can u help me in this?
 
my email x.i.peng@163.com
GeneralRe: help mememberChristian Graus5 Nov '02 - 15:10 
Let's see.....
 
1. The question has little to nothing to do with this article.
2. You're not committed enough to this community to get an account or even to indicate that you're going to check back for a reply.
3. You didn't ask in the proper forums.
4. You didn't say please.
 
On what level do you think this is the way to solve your problem ?
 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: help mesussAnonymous5 Nov '02 - 15:24 
sorry.
i forget it!

GeneralRe: help mememberChristian Graus5 Nov '02 - 15:33 
I'm not trying to discourage you from asking for help, just suggesting that you're going the wrong way about it. Apart from anything else, this article has nothing to do with your problem, and everyone who has voted for it, gave it a 1. On what basis then would this be the person to ask, when you can ask the whole site in the programming forum ?

 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: help mesussAnonymous18 Dec '07 - 5:39 
Tbh you're a bit of an ass. He doesn't speak particularly good English and I think it's fairly obvious the answer to all 4 of your particularly helpful comments is that it's pretty confusing when you don't understand what's going on. Maybe you should show a little more consideration rather than making snide comments.
Generalcan't load BMPsussxipeng31 Oct '02 - 20:48 
it can load JPG but not BMP
help me
GeneralRe: can't load BMPmemberChristian Graus31 Oct '02 - 21:31 
::LoadImage will load a bmp.

 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: can't load BMPsussAnonymous31 Oct '02 - 23:46 
how can i use LoadImage?
can you tell me an example?
GeneralRe: can't load BMPmemberChristian Graus1 Nov '02 - 0:40 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/introductiontoresources/resourcereference/resourcefunctions/loadimage.asp[^]
 
There's a bit at the bottom on LoadBitmap, which now that I think of it, supercedes LoadImage. So check out both before deciding which to use.

 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: can't load BMPsussAnonymous1 Nov '02 - 1:30 
sorry,my english is poor,
my meaning is how to use it the current sample instead of OleLoadPicturePath function
GeneralRe: can't load BMPmemberChristian Graus1 Nov '02 - 6:34 
I'm sorry, I've not looked at the sample. To be honest, it's probably easier overall if you look at some other articles on CP which are more descriptuve of what they do and offer all these formats to you. However, in this case, whatever the load function is, you'd just parse for .bmp on the filename and use LoadBitmap to load the image instead in that case.
 
Unless you need W95 support, I'd suggest my GDI+ articles to be your best bet.

 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: can't load BMPmemberChen Su1 Nov '02 - 6:50 
It does load .BMP.
 
Can Microsoft Paint load your .BMP? Have you tried some other .BMP files?
GeneralRe: can't load BMPsussAnonymous1 Nov '02 - 17:25 
Microsoft Paint can load my BMP
some .bmp can be loaded,but others can not
GeneralRe: can't load BMPmemberChen Su1 Nov '02 - 18:53 
Send me the BMP please.
GeneralRe: can't load BMPmemberAnonymous2 Nov '02 - 4:30 
http://218.22.145.131/temp.bmp
it can be opened by Ms Paint
tell me if you give it ,and i will delete it
GeneralRe: can't load BMPmemberChen Su2 Nov '02 - 12:22 
I've got the BMP file. Neither OleLoadPicturePath() nor LoadImage() are able to load this BMP file. This BMP file is a 16-bit bitmap without a color table. It seems that the Windows API functions cannot load the bitmaps of this format. The Windows Picture and Fax Viewer in Windows XP cannot load it either. If you really need to load this format, you can read the file and create the bitmap using CreateDIBSection(). Or you may save the bitmap as other formats.
 
I give solutions to problems rather than bullsh*t.
GeneralerrorsussAnonymous31 Oct '02 - 15:35 
LINK : fatal error LNK1104: cannot open file "uafxcwd.lib"
Error executing link.exe.
GeneralRe: errormemberChristian Graus31 Oct '02 - 15:42 
That's a Microsoft lib, and therefore your problem, not the authors. It looks to me from a glance at MSDN that it should be visible to the compiler without any SDK updates, but that might be wrong.
 

 
Christian
 
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
 
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
 
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

GeneralRe: errormemberChen Su31 Oct '02 - 15:55 
You don't have the MFC static libraries installed. Two options:
 
1. Install the MFC static libraries.
 
2. Change the project setting to "Use MFC in a Shared DLL".
GeneralAt least!memberDaniel Turini26 Oct '02 - 21:46 
Someone with taste!
 
Opus 35 is a nice Sonata from Chopin, but SO long! If you're playing it, congrats!
Oh, and Opus 35 has 4 nice movements, don't stick to the Marche Funèbre, the other 3 movements are much more beautiful.
 
But the code could add something new...

 
lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
Generalthis is poormemberMr. Pickles26 Oct '02 - 19:13 
there is nothing new or unqiue here.
GeneralRe: this is poormemberShog926 Oct '02 - 19:11 
Mr. Pickles wrote:
there is nothing new or unqiue here.
 
I disagree! This is the first CP article i've seen with the sole screenshot consisting of a scaled musical score...
 

Shog9
------
So they took me down to the gallows
And this boy, he said to me:
"Why do you smile, when the rope's around your neck?"
I said, "I tell you boy, when i get back..."

GeneralRe: this is poormemberBlack Cat27 Oct '02 - 3:04 
Actually, the author used to be the number one contributor on MFC at the www.Experts-Exchange.com site. His typical answer to a programming question is just a reference number for a KB article (so his style is consistent at least Wink | ;-) ).
 
Just wondering if EE is already dead or dying? Wink | ;-)

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by Chen Su
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid