troubleshooting Question

LoadPic program help

Avatar of musicc
musicc asked on
C++
6 Comments1 Solution726 ViewsLast Modified:
Hi,
I am having a problem with LoadPic dealing with streaming images. Basically, I modified the code so that it would read jpeg image and then display the image insquence like a movie. But I'm having making it load faster or smoother. My goal to make it display 15 images per sec if possible. Plz take a look.

//---------------part of the code--------------------------------//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
      PAINTSTRUCT ps;
      HDC hdc;
      char buf[64];
      i = i%9;
      sprintf(buf,"frame%06d.jpg",i+1);                              LoadPictureFile(buf);
      hdc = BeginPaint(hWnd, &ps);
      if (gpPicture)
            {
            // get width and height of picture
            long hmWidth;
            long hmHeight;
            gpPicture->get_Width(&hmWidth);
            gpPicture->get_Height(&hmHeight);
            // convert himetric to pixels
            int nWidth      = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
            int nHeight      = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
            RECT rc;
            GetClientRect(hWnd, &rc);
            // display picture using IPicture::Render
            gpPicture->Render(hdc, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
            }
            EndPaint(hWnd, &ps);
            i = i+1;
            return DefWindowProc(hWnd, message, wParam, lParam);
}
//----------------------part of the code end-----------------------------//


My loadpic program
// LoadPic.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <fstream.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int i = 1;

#define MAX_LOADSTRING      100
#define HIMETRIC_INCH      2540
#define MAP_LOGHIM_TO_PIX(x,ppli)   ( ((ppli)*(x) + HIMETRIC_INCH/2) / HIMETRIC_INCH )

// Global Variables:
HINSTANCE hInst;                                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                              // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                  // The title bar text
LPPICTURE gpPicture;
HWND ghWnd;

// Foward declarations of functions included in this code module:
ATOM                        MyRegisterClass(HINSTANCE hInstance);
BOOL                        InitInstance(HINSTANCE, int);
LRESULT CALLBACK      WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
       // TODO: Place code here.
      MSG msg;

      // Initialize global strings
      LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
      LoadString(hInstance, IDC_LOADPIC, szWindowClass, MAX_LOADSTRING);
      MyRegisterClass(hInstance);

      // Perform application initialization:
      if (!InitInstance (hInstance, nCmdShow))
      {
            return FALSE;
      }

      // Main message loop:
      while (GetMessage(&msg, NULL, 0, 0))
      {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
      }

      if (gpPicture)
            gpPicture->Release();

      return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
      WNDCLASSEX wcex;

      wcex.cbSize = sizeof(WNDCLASSEX);

      wcex.style                  = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc      = (WNDPROC)WndProc;
      wcex.cbClsExtra            = 0;
      wcex.cbWndExtra            = 0;
      wcex.hInstance            = hInstance;
      wcex.hIcon                  = LoadIcon(hInstance, (LPCTSTR)IDI_LOADPIC);
      wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
      wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);
      wcex.lpszMenuName      = (LPCSTR)IDC_LOADPIC;
      wcex.lpszClassName      = szWindowClass;
      wcex.hIconSm            = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

      return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
   gpPicture = NULL;

   ghWnd = hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

// This function loads a file into an IStream.
void LoadPictureFile(LPCTSTR szFile)
{
      // open file
      HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
      _ASSERTE(INVALID_HANDLE_VALUE != hFile);
      // get file size
      DWORD dwFileSize = GetFileSize(hFile, NULL);
      _ASSERTE(-1 != dwFileSize);
      LPVOID pvData = NULL;
      // alloc memory based on file size
      HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
      _ASSERTE(NULL != hGlobal);
      pvData = GlobalLock(hGlobal);
      _ASSERTE(NULL != pvData);
      DWORD dwBytesRead = 0;
      // read file and store in global memory
      BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
      _ASSERTE(FALSE != bRead);
      GlobalUnlock(hGlobal);
      CloseHandle(hFile);
      LPSTREAM pstm = NULL;
      // create IStream* from global memory
      HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
      _ASSERTE(SUCCEEDED(hr) && pstm);
      // Create IPicture from image file
      if (gpPicture)
            gpPicture->Release();
      hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
      _ASSERTE(SUCCEEDED(hr) && gpPicture);      
      pstm->Release();
      InvalidateRect(ghWnd, NULL, TRUE);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
      PAINTSTRUCT ps;
      HDC hdc;
      char buf[64];
                        i = i%9;
                        sprintf(buf,"frame%06d.jpg",i+1);                                    
                        LoadPictureFile(buf);
                        hdc = BeginPaint(hWnd, &ps);
                        if (gpPicture)
                        {
                        // get width and height of picture
                        long hmWidth;
                        long hmHeight;
                        gpPicture->get_Width(&hmWidth);
                        gpPicture->get_Height(&hmHeight);
                        // convert himetric to pixels
                        int nWidth      = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
                        int nHeight      = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
                        RECT rc;
                        GetClientRect(hWnd, &rc);
                        // display picture using IPicture::Render
                        gpPicture->Render(hdc, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
                        }
                        EndPaint(hWnd, &ps);
                        i = i+1;
                        return DefWindowProc(hWnd, message, wParam, lParam);
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros