• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

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

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
DickStone
Asked:
DickStone
  • 7
  • 5
1 Solution
 
ZoppoCommented:
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
 
DickStoneAuthor Commented:
What is the .h that goes with this?
0
 
ZoppoCommented:
You need to include gdiplus.h to use Gdiplus:

#include <gdiplus.h>

ZOPPO
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
DickStoneAuthor Commented:
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
 
ZoppoCommented:
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
 
DickStoneAuthor Commented:
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
 
ZoppoCommented:
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
 
ZoppoCommented:
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
 
DickStoneAuthor Commented:
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
 
ZoppoCommented:
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
 
DickStoneAuthor Commented:
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
 
ZoppoCommented:
Wow! Thanks a lot, that's very nice  :o)

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now