locked
[UWP]How to convert a HBITMAP to a Universal App BitmapSource? RRS feed

  • Question

  • I'm trying to create a Windows 10 C++ Universal Windows App, which uses some existing c++ libraries.

    I have a method which returns an image in a HBITMAP, how can I get this into a BitmapSource? so that I can ultimately display the image in my app?

    From Googling I found Imaging::CreateBitmapSourceFromHBitmap, but this only seems to apply to C# WFP apps, I have not been able to find an equivalent method that will work in C++ UWP.


    • Edited by Barry Wang Friday, February 19, 2016 3:35 AM title tag
    Friday, February 5, 2016 12:23 PM

Answers

  • Hi JonesJ76,

    Yes you are right, CreateBitmapSourceFromHBitmap Method does not support in Windows Runtime Apps.

    >>I have a method which returns an image in a HBITMAP

    May I know if this method will executed from your UWP app or from a service? Basically I saw most of the creation of the HBITMAP are desktop APIs like GetHBITMAP, is it possible that the generate a BITMAP object instead of the HBITMAP, since HBITMAP is just a HANDLE object.

    If the method is executed within the app, I would then recommend you check if it use some API that cannot pass WACK validation.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Barry Wang Thursday, February 18, 2016 9:10 AM
    • Marked as answer by Barry Wang Friday, February 19, 2016 3:35 AM
    Monday, February 8, 2016 5:22 AM
  • Yes, basically you need to check if the API is supported when using them (probably some third party code library directly use the Win32 or Desktop APIs), if the documentation does write support Windows Store apps (below is a example), then we can use this API, otherwise you will got WACK fail.

    >> So it looks like I'll need to find an alternate method of creating a Bitmap using only UWP supported API methods.

    Definitely yes, Windows Runtime Apps runs in a sandbox and it only allow some fewer desktop APIs, perhaps you can tell us what is your requirement, we can try to find some solution together :)

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Barry Wang Thursday, February 18, 2016 9:10 AM
    • Marked as answer by Barry Wang Friday, February 19, 2016 3:35 AM
    Thursday, February 11, 2016 1:10 AM

All replies

  • Hi JonesJ76,

    Yes you are right, CreateBitmapSourceFromHBitmap Method does not support in Windows Runtime Apps.

    >>I have a method which returns an image in a HBITMAP

    May I know if this method will executed from your UWP app or from a service? Basically I saw most of the creation of the HBITMAP are desktop APIs like GetHBITMAP, is it possible that the generate a BITMAP object instead of the HBITMAP, since HBITMAP is just a HANDLE object.

    If the method is executed within the app, I would then recommend you check if it use some API that cannot pass WACK validation.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Barry Wang Thursday, February 18, 2016 9:10 AM
    • Marked as answer by Barry Wang Friday, February 19, 2016 3:35 AM
    Monday, February 8, 2016 5:22 AM
  • Thank you for your response.

    The application is a UWP app, I was not aware that some APIs were not supported when creating a UWP app.

    As you recommended I have ran WACK validation, and it turns out I may have other issues, as my bitmap creation currently uses CreateDIBSection and SetBitmapBits from gdi32.dll which WACK has reported as unsupported.

    So it looks like I'll need to find an alternate method of creating a Bitmap using only UWP supported API methods.

    Tuesday, February 9, 2016 3:56 PM
  • Yes, basically you need to check if the API is supported when using them (probably some third party code library directly use the Win32 or Desktop APIs), if the documentation does write support Windows Store apps (below is a example), then we can use this API, otherwise you will got WACK fail.

    >> So it looks like I'll need to find an alternate method of creating a Bitmap using only UWP supported API methods.

    Definitely yes, Windows Runtime Apps runs in a sandbox and it only allow some fewer desktop APIs, perhaps you can tell us what is your requirement, we can try to find some solution together :)

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Barry Wang Thursday, February 18, 2016 9:10 AM
    • Marked as answer by Barry Wang Friday, February 19, 2016 3:35 AM
    Thursday, February 11, 2016 1:10 AM