Solved

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

Posted on 2013-01-17
12
415 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow process to read Excel 15 163
Problem to DocumentFormat 8 146
Authentication of Web Services 3 110
Input parameteres to DragOver 2 58
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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