The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Reading Image File(JPEG)
Discuss Reading Image File(JPEG) in the C Programming forum on Dev Shed. Reading Image File(JPEG) C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 30th, 2003, 02:40 AM
|
|
Registered User
|
|
Join Date: May 2003
Posts: 28
Time spent in forums: 3 m 32 sec
Reputation Power: 0
|
|
|
Reading Image File(JPEG)
Hi
How can I Open an Image files in C, like jpeg, bmp, gif
Thx
Sumesh
|

May 30th, 2003, 11:44 AM
|
|
Junior Member
|
|
Join Date: May 2003
Location: Russia
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Jpeg-Gif-Bmp sample for LCC, BC, VC
// LCC - link with uuid.lib
#define LccVersion // only for LCC Compiler
#include <windows.h>
#include <olectl.h>
#define MAX_LOADSTRING 100
#define HIMETRIC_INCH 2540
#define MAP_LOGHIM_TO_PIX(x,ppli) ( ((ppli)*(x) + HIMETRIC_INCH/2) / HIMETRIC_INCH )
#define FileName "test.jpg"
LPPICTURE gpPicture;
HANDLE hBitmap ;
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void LoadPictureFile(LPCTSTR szFile);
//---------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "JpegTest" ;
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
int WinSize = 500;
int Left = 200;
int Top = 100;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = NULL ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (
szAppName, // pointer to registered class name
"Transparent Clock", // pointer to window name
WS_OVERLAPPEDWINDOW, // window style
Left, // horizontal position
Top , // vertical position
WinSize, // window width
WinSize, // window height
NULL, // handle to parent or owner window
NULL, // handle to menu or child-window identifier
hInstance, // handle to application instance
NULL // pointer to window-creation data
) ;
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc, hCompatibleDC ;
PAINTSTRUCT ps ;
RECT rect;
HANDLE hOldBitmap ;
BITMAP Bitmap;
switch (iMsg)
{
case WM_CREATE :
LoadPictureFile(FileName);
return 0 ;
case WM_PAINT:
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);
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
//------------------------------------------------------------------------------------------------//
// This function loads a file
void LoadPictureFile(LPCTSTR szFile)
{
// open file
HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
LPVOID pvData = NULL;
// alloc memory based on file size
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
pvData = GlobalLock(hGlobal);
DWORD dwBytesRead = 0;
// read file and store in global memory
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
// Create IPicture from image file
if (gpPicture)
gpPicture->Release();
#ifdef LccVersion
hr = OleLoadPicture(pstm, dwFileSize, FALSE, &IID_IPicture, (LPVOID *)&gpPicture);
#else
hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
#endif
pstm->Release();
// InvalidateRect(ghWnd, NULL, TRUE);
}
|

June 21st, 2003, 09:03 PM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Compile Error
This code gives a 'Render not a member of IPICTURE' error 2039.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|