Solved

Need coach/help on adding JPG to Microsoft Visual C++ 2010 with the MS SDK code

Posted on 2013-01-17
12
402 Views
Last Modified: 2013-02-05
I'm using Microsoft Visual C++ 2010 with the MS SDK. I have an application that reads a .BMP files and displays it. I need to switch to reading JPG without changing the display code.

I need someone to help me add a 3rd party package that does this for me (under $300). I'm willing to pay someone to help with this.

Is there a URL for finding people like this? Or do you know someone?

(I'm not looking for recommendations on packages -- just how to find someone to help)
0
Comment
Question by:DickStone
  • 7
  • 5
12 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
Hi DickStone,

if there's no reason against it you can use Gdiplus (it's part of MS SDK) to load a JPEG into a Gdiplus::Bitmap and convert it to a 'normal' GDI bitmap (HBITMAP).

You can do it somehow like this:
    // the filename has to be a wide-character string!
    const wchar_t* filename= L"c:\\test\\test.jpeg"; 

    Gdiplus::GdiplusStartupInput gdiplusStartupInput; 
    ULONG_PTR gdiplusToken; 
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 
 
    // Load the image 
    Gdiplus::Image* image = Gdiplus::Image::FromFile( filename ); 
 
    // get GDI bitmap handle
    HBITMAP hBmp;
    if ( Gdiplus::Ok != image->GetHBITMAP( Gdiplus::Color( 0, 255, 255, 255 ), &hBmp ) )
    {
      // handle error
      ...
    }
    else
    {
     // hBmp is a valid handle to the loaded bitmap, do what you need to do here
     ..
    }

    // delete the image when done 
    delete image; image = 0; 
 
    // Shutdown Gdiplus 
    Gdiplus::GdiplusShutdown(gdiplusToken); 

Open in new window

Hope that helps,

ZOPPO
0
 

Author Comment

by:DickStone
Comment Utility
What is the .h that goes with this?
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
You need to include gdiplus.h to use Gdiplus:

#include <gdiplus.h>

ZOPPO
0
 

Author Comment

by:DickStone
Comment Utility
I got:

1>  OMLogo.cpp
1>c:\wfree\omlogo.cpp(216): error C2039: 'GetHBITMAP' : is not a member of 'Gdiplus::Image'
1>          c:\program files\microsoft sdks\windows\v7.0a\include\gdiplusheaders.h(372) : see declaration of 'Gdiplus::Image'
1>
1>Build FAILED.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Ah, sorry, my mistake, in the (untested) sample code I used the wrong class Gdiplus::Image, the needed class is Gdiplus::Bitmap, so replacing the class should work, i.e.:
    Gdiplus::Bitmap* image = Gdiplus::Bitmap::FromFile( filename ); 

Open in new window

0
 

Author Comment

by:DickStone
Comment Utility
I replaced the line, as your suggested, and it compiles.

However, when run, that line results in
- image.nativeImage = 0x00000000
- image.lastResult = InvalidParameter
- image.lastStatus = -1163005939

Any idea what's wrong?
Also, there is nothing that tells the read routines that it's a Jpeg. Does it get it from the .jpg ending on the file?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi again,

are you sure the file path is correct and you have permission to access the file? I just re-tested the code and for me it works fine, I onle recieve an InvalidParameter error when I pass a path to a none-existing file.

ZOPPO
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
PS: About the second question I think Gdiplus reads into the file to detect the needed decoder. I tested to load a PNG which I renamed to 'test.jpeg' using the above code, the file was loaded correctly ...
0
 

Author Comment

by:DickStone
Comment Utility
It works!!! The filename was my problem. The file had a double _ and it looked like a single on my screen.

Thanks for hanging with me. I increased the points to 500.

I have one last hurdle. How do I convert from CString to wchar_t* ?
 void ReadFile(CString& name) {
   wchar_t  filename[256];;
   strcpy( filename,  s ) ;   // <--- doesn't work
   ...
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
This depends on your project settings.

If you're using UNICODE you can easily use its LPCTSTR operator, i.e.:
 void ReadFile(CString& name)
 {
  Gdiplus::Bitmap* image = Gdiplus::Bitmap::FromFile( (LPCTSTR)name );
  ...
 }

Open in new window

If you don't use UNICODE you need to convert from ANSI/ASCII to a wide character string. The easiest method to do this I know is to use ATL's A2W helper macro from, i.e.:
#include <atlconv.h>
...
 void ReadFile(CString& name)
 {
  USES_CONVERSION;
  Gdiplus::Bitmap* image = Gdiplus::Bitmap::FromFile( A2W( (LPCTSTR)name ) );
  ...
 }

Open in new window

Another method is to use MultiByteToWideChar. You can find a sample i.e. at http://www.codeproject.com/Articles/2510/Data-Conversions (search for 'CString to WCHAR*')

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:DickStone
Comment Utility
Zoppo has fantastic knowledge of C++ and MS Classes,. He came up with the perfect solution, and hung with me through my errors and my issues with knowledge of conversions. COULDN'T RECOMMEND HIM HIGHLY ENOUGH ! ! !
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Wow! Thanks a lot, that's very nice  :o)

Have a nice day,

best regards,

ZOPPO
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now