Solved

Loading and display JPEG image from image stream

Posted on 2009-05-13
11
1,262 Views
Last Modified: 2013-12-14
Hi Experts,

Is there anyway I can use MFC or Win32 programming to read a buffer containing JPEG data I extract from the HTTP contents data/ socket stream, render it and display on my application?  Some simple step and API used examples are very much appreciated.
0
Comment
Question by:cskiong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 9

Expert Comment

by:JohnGaby
ID: 24377263
You can use the Image class of GDI+ to load a jpeg image from a stream:

http://msdn.microsoft.com/en-us/library/ms535371(VS.85).aspx
0
 

Author Comment

by:cskiong
ID: 24403118
Hi JohnGaby,

I have tried to find more info about GDI+ to load a jpeg image from data stream, however I still didn't find what I'm looking for.  I have extract the image data and store into char arrays, what I want is a way to load the data to a Bitmap object and display it.

Steps and Win32/MFC functions used to do as stated, are very much appreciated.
Thank you!
0
 
LVL 9

Expert Comment

by:JohnGaby
ID: 24403503
Below is a code snippet which reads a jpeg file into memory, and then uses GDI+ to display the jpeg image from the memory block.  If you use GDI+, be sure that you call the GdiplusStartup function before using any of the classes.

http://msdn.microsoft.com/en-us/library/aa378980(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms534077(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms534462(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms534453(VS.85).aspx
#include <gdiplus.h>
#include <stdio.h>
 
...
 
HGLOBAL LoadFileIntoMemory(LPTSTR pPath)
{
    FILE * fp;
    HGLOBAL hMem    = 0;
 
    if (!_tfopen_s(&fp, pPath, _T("rb")))
    {
        int size;
 
        fseek(fp, 0, SEEK_END);
        size    = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
        if (hMem = GlobalAlloc(GMEM_MOVEABLE, size))
        {
            LPVOID p = GlobalLock(hMem);
 
            fread(p, size, 1, fp);
 
            GlobalUnlock(hMem);
        }
 
        fclose(fp);
    }
 
    return(hMem);
}
 
void OnPaint(HWND hWnd)
{
    PAINTSTRUCT ps;
    HDC hdc;
 
    hdc = BeginPaint(hWnd, &ps);
 
    HGLOBAL hMem;
 
    if (hMem = LoadFileIntoMemory(_T("c:\\test.jpg")))
    {
        IStream * pStream;
 
        if (CreateStreamOnHGlobal(hMem, TRUE, &pStream) == S_OK)
        {
            Image       image(pStream);
            Graphics    graphics(hdc);
 
            graphics.DrawImage(&image, 0, 0);
 
            pStream->Release();
        }
    }
 
    EndPaint(hWnd, &ps);
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 9

Expert Comment

by:JohnGaby
ID: 24403510
I ment to put it in my code snippet, but you will also need a

using namespace Gdiplus;

to use the classes without prepending them with 'Gdiplus'
0
 

Author Comment

by:cskiong
ID: 24404843
Hi JohnGaby,
Thanks for your references and sample codes.  The code looks fine, but yet to suit my need.  I do not want to load a local image file, nor to create a image file then load it into memory stream using LoadFileIntoMemory(), because I need to real time receiving many different char array from a web server, if I save them into local image file, then load it, and replace with another new local image, it will be not efficient to my application.

Is there a way to load the char array into IStream, or other mehod that can directly convert the char array into bitmap (jpeg???) .  

Maybe just some direct and simple step by step example (function/ API called) will do the great job.  (Sorry for this request, but I'm running out of time to perform the task.)

Thanks!
0
 
LVL 9

Accepted Solution

by:
JohnGaby earned 300 total points
ID: 24404939
Well maybe, just maybe, you might try allocating a block of memory using GlobalAlloc as I have done, and then, instead of reading a file, copy your bytes to that block.  Then you can use the CreateStreamOnHGlobal function.
0
 

Author Comment

by:cskiong
ID: 24406509
Hi JohnGaby,
Thanks for the suggetion, I'll try and let you know when I have the result.  However, with your code sample, how can I use the MFC Image Control (CStatic) instead of using the graphics.DrawImage to display the image?

Thank you!
0
 
LVL 9

Expert Comment

by:JohnGaby
ID: 24406904
I cannot help you with that.
0
 

Author Comment

by:cskiong
ID: 24409017
Hi JohnGaby,
Thanks for your suggestion, I copied the image data buffer to the global allocate memory space, then decode it using OleLoadPicture then call the LPPICTURE render().  
Finally it works.

Just a final question, the image is up side down now, is that I need to manupulate the image data before I render, or should I flip it horizontally.  Please advise.

Really appreciate your help!
0
 
LVL 9

Expert Comment

by:JohnGaby
ID: 24409051
I am not familiar with the use of OleLoadPicture, so I cannot tell you how to flip the image.  It seems to me, however, that if the source is a jpeg image, then the source must be storing it upside down, and ithat is what that you need to fix.
0
 

Author Closing Comment

by:cskiong
ID: 31581089
I found the issue is due to the parameter set in the Render, so the problem is fixed.

Thanks JohnGaby for your advices, you really save my life.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question