Solved

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

Posted on 2013-01-17
12
416 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
[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
  • 7
  • 5
12 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 38792025
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
ID: 38822992
What is the .h that goes with this?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38825730
You need to include gdiplus.h to use Gdiplus:

#include <gdiplus.h>

ZOPPO
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:DickStone
ID: 38837132
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 31

Expert Comment

by:Zoppo
ID: 38838446
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
ID: 38845657
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 38850238
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 31

Expert Comment

by:Zoppo
ID: 38850242
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
ID: 38855134
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 31

Expert Comment

by:Zoppo
ID: 38855270
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
ID: 38855680
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 31

Expert Comment

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

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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