Displaying a JPEG image using Windows GUI

Please support our C advertiser: Programming Forums
Nov 13th, 2004
Views: 14,760
Thread Rating: 1 votes, 3.0000 average.
AddThis Social Bookmark Button
This program shows how to display a JPEG (also GIF,BMP,WMF etc.) image using some Windows Graphical User Interface C code. The program uses the uuid.lib file that comes with many C compilers.
c Syntax
  1. // load and display BMP, GIF, JPG, WMF, EMF, or ICO images
  2. // image file Audi.jpg should be in the working directory
  3. // filename is specified in FormLoad() and can be changed
  4. // BCX generated code is modified for the PellesC compiler
  5. // free C compiler with a sweet IDE from:
  6. // http://smorgasbordet.com/pellesc/index.htm
  7. // free BCX basic to C translator (includes PellesC) from:
  8. // http://www.rjpcomputing.com/programming/bcx/devsuite.html
  9.  
  10.  
  11. // needed headers
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <io.h> // _access()
  18. #include <ocidl.h> // LPPICTURE
  19. #include <olectl.h> // OleLoadPicture()
  20. #include <ole2.h> // CreateStreamOnHGlobal
  21.  
  22. // these are the needed libraries
  23. #pragma comment(lib,"uuid.lib")
  24. #pragma comment(lib,"ole32.lib")
  25. #pragma comment(lib,"oleaut32.lib")
  26.  
  27. // macro
  28. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  29.  
  30. // globals
  31. static HINSTANCE BCX_hInstance;
  32. static int BCX_ScaleX;
  33. static int BCX_ScaleY;
  34. static char BCX_ClassName[80];
  35. static HWND Form1;
  36. static HWND Jpg1;
  37.  
  38. // prototypes
  39. HWND BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
  40. HWND BCX_OlePicture(char*,HWND=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0);
  41. STDAPI OleLoadPicture(LPSTREAM, LONG, BOOL, REFIID, LPVOID *);
  42.  
  43. void FormLoad (void);
  44. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  45.  
  46.  
  47. // standard main for Windows Graphics User Interface (GUI)
  48. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
  49. {
  50. WNDCLASS Wc;
  51. MSG Msg;
  52. // *****************************
  53. strcpy(BCX_ClassName,"JPEG_FILE1");
  54. // ************************************
  55. // Scale Dialog Units To Screen Units
  56. // use of pixels has not been specified
  57. // ************************************
  58. RECT rc = {0,0,4,8};
  59. MapDialogRect (NULL,&rc);
  60. BCX_ScaleX = rc.right/2;
  61. BCX_ScaleY = rc.bottom/4;
  62. BCX_hInstance = hInst;
  63. // ******************************************************
  64. Wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  65. Wc.lpfnWndProc = WndProc;
  66. Wc.cbClsExtra = 0;
  67. Wc.cbWndExtra = 0;
  68. Wc.hInstance = hInst;
  69. Wc.hIcon = LoadIcon(NULL,IDI_WINLOGO); // standard icon
  70. Wc.hCursor = LoadCursor(NULL,IDC_ARROW); // standard cursor
  71. Wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  72. Wc.lpszMenuName = NULL;
  73. Wc.lpszClassName = BCX_ClassName;
  74. RegisterClass(&Wc);
  75.  
  76. FormLoad();
  77. // message loop
  78. while(GetMessage(&Msg,NULL,0,0))
  79. {
  80. HWND hActiveWindow = GetActiveWindow();
  81. if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
  82. {
  83. TranslateMessage(&Msg);
  84. DispatchMessage(&Msg);
  85. }
  86. }
  87. return Msg.wParam;
  88. }
  89.  
  90.  
  91. // create the Windows form
  92. HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
  93. {
  94. HWND A;
  95. // assign a default style
  96. if (!Style)
  97. {
  98. Style= WS_MINIMIZEBOX |
  99. WS_SIZEBOX |
  100. WS_CAPTION |
  101. WS_MAXIMIZEBOX |
  102. WS_POPUP |
  103. WS_SYSMENU;
  104. }
  105. A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
  106. Style,
  107. X*BCX_ScaleX,
  108. Y*BCX_ScaleY,
  109. (4+W)*BCX_ScaleX,
  110. (12+H)*BCX_ScaleY,
  111. NULL,(HMENU)NULL,BCX_hInstance,NULL);
  112. // assign a default font
  113. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  114. (LPARAM)MAKELPARAM(FALSE,0));
  115. return A;
  116. }
  117.  
  118.  
  119. // this is the meat!
  120. HWND BCX_OlePicture(char* szFile,HWND hWnd,int id,int X,int Y,int W,int H,
  121. int Res,int Style,int Exstyle)
  122. {
  123. HWND A;
  124. HBITMAP hBitmap;
  125. DWORD dwFileSize;
  126. DWORD dwBytesRead;
  127. HANDLE hFile;
  128. LPPICTURE gpPicture;
  129. LPVOID lpPicData;
  130. LPVOID pvData;
  131. LPSTREAM pstm;
  132. LPVOID hrlp;
  133. HGLOBAL hGlobal;
  134.  
  135. // assign a default style
  136. if (!Style) Style=WS_CHILD|WS_VISIBLE|WS_TABSTOP|SS_BITMAP;
  137. A=CreateWindowEx(Exstyle,"static",NULL,Style,X*BCX_ScaleX,Y*BCX_ScaleY,0,0,hWnd,(HMENU)(HMENU)id,BCX_hInstance,NULL);
  138. // in case image is resource, fancier than needs to be
  139. if (Res)
  140. {
  141. hrlp=FindResource(BCX_hInstance,MAKEINTRESOURCE(Res),RT_RCDATA);
  142. if (hrlp!=0)
  143. {
  144. dwFileSize=SizeofResource(BCX_hInstance,hrlp);
  145. hGlobal=LoadResource(BCX_hInstance,hrlp);
  146. if (hGlobal!=0)
  147. {
  148. lpPicData=LockResource(hGlobal);
  149. pvData=NULL;
  150. hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
  151. pvData=GlobalLock(hGlobal);
  152. CopyMemory(pvData,lpPicData,dwFileSize);
  153. GlobalUnlock(hGlobal);
  154. }
  155. }
  156. else
  157. return NULL;
  158. }
  159. else
  160. {
  161. hFile=CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
  162. dwFileSize=GetFileSize(hFile,NULL);
  163. if(dwFileSize==-1)
  164. return NULL;
  165. pvData=NULL;
  166. hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
  167. pvData=GlobalLock(hGlobal);
  168. ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,NULL);
  169. GlobalUnlock(hGlobal);
  170. CloseHandle(hFile);
  171. }
  172. CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
  173. OleLoadPicture(pstm,0,FALSE,&IID_IPicture,(LPVOID*)&gpPicture);
  174. pstm->lpVtbl->Release(pstm);
  175. gpPicture->lpVtbl->get_Handle(gpPicture,(OLE_HANDLE*)&hBitmap);
  176. if (W || H)
  177. hBitmap = CopyImage(hBitmap,IMAGE_BITMAP,W*BCX_ScaleX,H*BCX_ScaleY,LR_COPYRETURNORG);
  178. SendMessage(A,(UINT)STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
  179. if (W || H)
  180. SetWindowPos(A,HWND_TOP,X*BCX_ScaleX,Y*BCX_ScaleY,W*BCX_ScaleX,H*BCX_ScaleY,SWP_DRAWFRAME);
  181. return A;
  182. }
  183.  
  184.  
  185. // details like title, corner coordinates,width,height,filename to load
  186. void FormLoad (void)
  187. {
  188. char filename[80];
  189.  
  190. Form1=BCX_Form("Display a JPEG from a file ... ",0,0,260,145);
  191.  
  192. // change filename to whatever image file you have!!!!!!!!!!!!!!!!!!!!!!!
  193. strcpy(filename,"Audi.jpg");
  194. if ( _access(filename,0) != 0)
  195. ExitProcess(0);
  196.  
  197. // width, height = 0,0 adjusts automatically to image
  198. Jpg1=BCX_OlePicture(filename,Form1,115,1,1,0,0);
  199. Show(Form1);
  200. }
  201.  
  202.  
  203. // message handler
  204. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  205. {
  206. // cleanup and exit program
  207. if (Msg == WM_DESTROY)
  208. {
  209. UnregisterClass(BCX_ClassName,BCX_hInstance);
  210. PostQuitMessage(0);
  211. }
  212. return DefWindowProc(hWnd,Msg,wParam,lParam);
  213. }
  214.  
  215. // ******************* credit to Kevin *************************
  216. // Created with BCX -- The BASIC To C Translator (ver 5.02)
  217. // BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
  218. // *************************************************************
  219.  
ashutosh_singh | Newbie Poster | Apr 26th, 2006
What all do I need to run this code???
It is giving number of errors while compilation.  
maldini | Newbie Poster | Jun 8th, 2005
OleLoadPicture is murder on the CPU if you want to display jpeg at a fast rate. Do you know of a function which would not take up so much cpu time.
Thanks
 

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:10 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC