Displaying a JPEG image using Windows GUI

vegaseat 2 Tallied Votes 1K Views Share

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.

// load and display BMP, GIF, JPG, WMF, EMF, or ICO images
// image file Audi.jpg should be in the working directory
// filename is specified in FormLoad() and can be changed
// BCX generated code is modified for the PellesC compiler
// free C compiler with a sweet IDE from: 
// http://smorgasbordet.com/pellesc/index.htm
// free BCX basic to C translator (includes PellesC) from: 
// http://www.rjpcomputing.com/programming/bcx/devsuite.html


// needed headers
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  
#include <ctype.h>
#include <io.h>      // _access()
#include <ocidl.h>   // LPPICTURE
#include <olectl.h>  // OleLoadPicture()
#include <ole2.h>    // CreateStreamOnHGlobal

// these are the needed libraries
#pragma comment(lib,"uuid.lib")
#pragma comment(lib,"ole32.lib")
#pragma comment(lib,"oleaut32.lib")

// macro
#define Show(Window)  RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);

// globals
static HINSTANCE BCX_hInstance;
static int     BCX_ScaleX;
static int     BCX_ScaleY;
static char    BCX_ClassName[80];
static HWND    Form1;
static HWND    Jpg1;

// prototypes
HWND    BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
HWND    BCX_OlePicture(char*,HWND=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0);
STDAPI  OleLoadPicture(LPSTREAM, LONG, BOOL, REFIID, LPVOID *);

void    FormLoad (void);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

  
// standard main for Windows Graphics User Interface (GUI)
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
{
   WNDCLASS Wc;
   MSG      Msg;
   // *****************************
   strcpy(BCX_ClassName,"JPEG_FILE1");
   // ************************************
   // Scale Dialog Units To Screen Units
   // use of pixels has not been specified
   // ************************************
   RECT rc          =  {0,0,4,8};
   MapDialogRect       (NULL,&rc);
   BCX_ScaleX       =  rc.right/2;
   BCX_ScaleY       =  rc.bottom/4;
   BCX_hInstance    =  hInst;
   // ******************************************************
   Wc.style         =  CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   Wc.lpfnWndProc   =  WndProc;
   Wc.cbClsExtra    =  0;
   Wc.cbWndExtra    =  0;
   Wc.hInstance     =  hInst;
   Wc.hIcon         =  LoadIcon(NULL,IDI_WINLOGO);  // standard icon
   Wc.hCursor       =  LoadCursor(NULL,IDC_ARROW);  // standard cursor
   Wc.hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
   Wc.lpszMenuName  =  NULL;
   Wc.lpszClassName =  BCX_ClassName;
   RegisterClass(&Wc);

   FormLoad();
   // message loop
   while(GetMessage(&Msg,NULL,0,0))
   {
      HWND hActiveWindow = GetActiveWindow();
      if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
      {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
      }
   }
   return Msg.wParam;
}


// create the Windows form
HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
{
   HWND  A;
   // assign a default style
   if (!Style)
   {
     Style= WS_MINIMIZEBOX  |
     WS_SIZEBOX      |
     WS_CAPTION      |
     WS_MAXIMIZEBOX  |
     WS_POPUP        |
     WS_SYSMENU;
   }
   A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
     Style,
     X*BCX_ScaleX,
     Y*BCX_ScaleY,
     (4+W)*BCX_ScaleX,
     (12+H)*BCX_ScaleY,
     NULL,(HMENU)NULL,BCX_hInstance,NULL);
   // assign a default font
   SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
     (LPARAM)MAKELPARAM(FALSE,0));
   return A;
}


// this is the meat!
HWND BCX_OlePicture(char* szFile,HWND hWnd,int id,int X,int Y,int W,int H,
     int Res,int Style,int Exstyle)
{
  HWND       A;
  HBITMAP    hBitmap;
  DWORD      dwFileSize;
  DWORD      dwBytesRead;
  HANDLE     hFile;
  LPPICTURE  gpPicture;
  LPVOID     lpPicData;
  LPVOID     pvData;
  LPSTREAM   pstm;
  LPVOID     hrlp;
  HGLOBAL    hGlobal;

  // assign a default style
  if (!Style) Style=WS_CHILD|WS_VISIBLE|WS_TABSTOP|SS_BITMAP;
  A=CreateWindowEx(Exstyle,"static",NULL,Style,X*BCX_ScaleX,Y*BCX_ScaleY,0,0,hWnd,(HMENU)(HMENU)id,BCX_hInstance,NULL);
  // in case image is resource, fancier than needs to be
  if (Res)
  {
    hrlp=FindResource(BCX_hInstance,MAKEINTRESOURCE(Res),RT_RCDATA);
    if (hrlp!=0)
    {
      dwFileSize=SizeofResource(BCX_hInstance,hrlp);
      hGlobal=LoadResource(BCX_hInstance,hrlp);
      if (hGlobal!=0)
      {
          lpPicData=LockResource(hGlobal);
          pvData=NULL;
          hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
          pvData=GlobalLock(hGlobal);
          CopyMemory(pvData,lpPicData,dwFileSize);
          GlobalUnlock(hGlobal);
      }
   }
   else
     return NULL;
  }
  else
  {
    hFile=CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
    dwFileSize=GetFileSize(hFile,NULL);
    if(dwFileSize==-1)
    return NULL;
    pvData=NULL;
    hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
    pvData=GlobalLock(hGlobal);
    ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,NULL);
    GlobalUnlock(hGlobal);
    CloseHandle(hFile);
  }
  CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
  OleLoadPicture(pstm,0,FALSE,&IID_IPicture,(LPVOID*)&gpPicture);
  pstm->lpVtbl->Release(pstm);
  gpPicture->lpVtbl->get_Handle(gpPicture,(OLE_HANDLE*)&hBitmap);
  if (W || H) 
    hBitmap = CopyImage(hBitmap,IMAGE_BITMAP,W*BCX_ScaleX,H*BCX_ScaleY,LR_COPYRETURNORG);
  SendMessage(A,(UINT)STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
  if (W || H) 
    SetWindowPos(A,HWND_TOP,X*BCX_ScaleX,Y*BCX_ScaleY,W*BCX_ScaleX,H*BCX_ScaleY,SWP_DRAWFRAME);
  return A;
}


// details like title, corner coordinates,width,height,filename to load
void FormLoad (void)
{
  char filename[80];
  
	Form1=BCX_Form("Display a JPEG from a file ... ",0,0,260,145);
  
  // change filename to whatever image file you have!!!!!!!!!!!!!!!!!!!!!!!
  strcpy(filename,"Audi.jpg");
  if ( _access(filename,0) != 0)
    ExitProcess(0);
  
  //  width, height = 0,0  adjusts automatically to image 
  Jpg1=BCX_OlePicture(filename,Form1,115,1,1,0,0);
  Show(Form1);
}


// message handler
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  // cleanup and exit program
	if (Msg == WM_DESTROY)
  {
    UnregisterClass(BCX_ClassName,BCX_hInstance);
    PostQuitMessage(0);
  }
  return DefWindowProc(hWnd,Msg,wParam,lParam);
}

// ******************* credit to Kevin *************************
//   Created with BCX -- The BASIC To C Translator (ver 5.02)
//  BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
// *************************************************************
maldini 0 Newbie Poster

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

ashutosh_singh 0 Newbie Poster

What all do I need to run this code???
It is giving number of errors while compilation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.