?
Solved

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

Posted on 2013-01-17
12
Medium Priority
?
423 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 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

770 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