locked
how to convert BitmapImage to writeableBitmap using c++

    Question

  • how to convert BitmapImage to writeableBitmap using c++,

    who can give me some sample, thanks first.

    --King Star.


    winRT

    Tuesday, July 10, 2012 2:49 AM

Answers

  • Hi King star,

    Thank you for explaining where you were confused. In the future, please ask for clarification in the existing thread rather than just repeating the question. If you just repeat the question you will likely get the same answer (as happened here).

    The concept is that you have to remember where you got the original bitmap from. For example, if you loaded it from a URL then remember the URL. You would then load the WriteableBitmap from that source the same way you would load any other WriteableBitmap. For example, you can get a stream to the URL and then pass that to WriteableBitmap->SetSource.

    If you have an image loaded from bytes (for example, from a BitmapDecoder) then you can copy that to the WriteableBitmap's PixelBuffer property via its IBufferByteAccess interface. If you search the forum you will find a number of threads discussing this.

    --Rob

    • Marked as answer by King Star Friday, July 27, 2012 4:00 AM
    Thursday, July 12, 2012 4:27 AM
    Owner
  • Hi King star,

    Thank you for explaining where you were confused. In the future, please ask for clarification in the existing thread rather than just repeating the question. If you just repeat the question you will likely get the same answer (as happened here).

    The concept is that you have to remember where you got the original bitmap from. For example, if you loaded it from a URL then remember the URL. You would then load the WriteableBitmap from that source the same way you would load any other WriteableBitmap. For example, you can get a stream to the URL and then pass that to WriteableBitmap->SetSource.

    If you have an image loaded from bytes (for example, from a BitmapDecoder) then you can copy that to the WriteableBitmap's PixelBuffer property via its IBufferByteAccess interface. If you search the forum you will find a number of threads discussing this.

    --Rob

    Rob:

       thanks first.

       Now my code is :

            byte *pixelArray = new byte[40*40*4];
            WriteableBitmap^ wb = ref new WriteableBitmap(40, 40);
            Windows::Storage::Streams::IBuffer^ buf = wb->PixelBuffer;
            BitmapDecoder^ decoder = ref new BitmapDecoder();

       my trouble is how to copy pixelArray to the wb's PixelBuffer.

    --King Star.



    winRT


    IUnknown* pUnknown = reinterpret_cast<IUnknown*>(wb->PixelBuffer);
    IBufferByteAccess* pBufferByteAccess = nullptr;
    HRESULT hr = pUnknown->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess));
    pUnknown->Release();

    byte* pBuffCopy = nullptr;
    pBufferByteAccess->Buffer(&pBuffCopy);

    now set pixelArray to pBuffCopy, Then we can create WriteableBitmap with pixelArray.

    We can use WriteableBitmap .

    ---King Star.

    Thanks Rob.


    winRT

    • Marked as answer by King Star Friday, July 27, 2012 4:00 AM
    Friday, July 27, 2012 3:59 AM

All replies

  • Hi King Star,

    There is no way to convert a BitmapImage to a WriteableBitmap. You cannot extract image data from the BitmapImage. The only way to get the image in the WriteableBitmap is to initialize the WriteableBitmap from the same source that you initialized the BitmapImage.

    This is the third or fourth time you have asked this. I am not sure if you don't understand the answer or if you are asking the wrong question. Can you give more detail on what you are trying to do? If you are having trouble with English you may want to try posting in the Chinese language forum instead.

    --Rob

    Tuesday, July 10, 2012 4:04 AM
    Owner
  • i create image with byte[]. you mean i can initialize the WriteableBitmap from byte[]. but i don't know how to create WriteableBitmap from byte[].

    i have asked this problem many times. You said there is no way to convert a BitmapImage to a WriteableBitmap, but you did not say how  to initialize the WriteableBitmap from the same source that you initialized the BitmapImage.

    -- King star


    winRT

    Thursday, July 12, 2012 1:09 AM
  • Hi King star,

    Thank you for explaining where you were confused. In the future, please ask for clarification in the existing thread rather than just repeating the question. If you just repeat the question you will likely get the same answer (as happened here).

    The concept is that you have to remember where you got the original bitmap from. For example, if you loaded it from a URL then remember the URL. You would then load the WriteableBitmap from that source the same way you would load any other WriteableBitmap. For example, you can get a stream to the URL and then pass that to WriteableBitmap->SetSource.

    If you have an image loaded from bytes (for example, from a BitmapDecoder) then you can copy that to the WriteableBitmap's PixelBuffer property via its IBufferByteAccess interface. If you search the forum you will find a number of threads discussing this.

    --Rob

    • Marked as answer by King Star Friday, July 27, 2012 4:00 AM
    Thursday, July 12, 2012 4:27 AM
    Owner
  • Hi King star,

    Thank you for explaining where you were confused. In the future, please ask for clarification in the existing thread rather than just repeating the question. If you just repeat the question you will likely get the same answer (as happened here).

    The concept is that you have to remember where you got the original bitmap from. For example, if you loaded it from a URL then remember the URL. You would then load the WriteableBitmap from that source the same way you would load any other WriteableBitmap. For example, you can get a stream to the URL and then pass that to WriteableBitmap->SetSource.

    If you have an image loaded from bytes (for example, from a BitmapDecoder) then you can copy that to the WriteableBitmap's PixelBuffer property via its IBufferByteAccess interface. If you search the forum you will find a number of threads discussing this.

    --Rob

    Rob:

       thanks first.

       Now my code is :

            byte *pixelArray = new byte[40*40*4];
            WriteableBitmap^ wb = ref new WriteableBitmap(40, 40);
            Windows::Storage::Streams::IBuffer^ buf = wb->PixelBuffer;
            BitmapDecoder^ decoder = ref new BitmapDecoder();

       my trouble is how to copy pixelArray to the wb's PixelBuffer.

    --King Star.



    winRT

    Thursday, July 12, 2012 7:51 AM
  • Hi King star,

    Thank you for explaining where you were confused. In the future, please ask for clarification in the existing thread rather than just repeating the question. If you just repeat the question you will likely get the same answer (as happened here).

    The concept is that you have to remember where you got the original bitmap from. For example, if you loaded it from a URL then remember the URL. You would then load the WriteableBitmap from that source the same way you would load any other WriteableBitmap. For example, you can get a stream to the URL and then pass that to WriteableBitmap->SetSource.

    If you have an image loaded from bytes (for example, from a BitmapDecoder) then you can copy that to the WriteableBitmap's PixelBuffer property via its IBufferByteAccess interface. If you search the forum you will find a number of threads discussing this.

    --Rob

    Rob:

       thanks first.

       Now my code is :

            byte *pixelArray = new byte[40*40*4];
            WriteableBitmap^ wb = ref new WriteableBitmap(40, 40);
            Windows::Storage::Streams::IBuffer^ buf = wb->PixelBuffer;
            BitmapDecoder^ decoder = ref new BitmapDecoder();

       my trouble is how to copy pixelArray to the wb's PixelBuffer.

    --King Star.



    winRT


    IUnknown* pUnknown = reinterpret_cast<IUnknown*>(wb->PixelBuffer);
    IBufferByteAccess* pBufferByteAccess = nullptr;
    HRESULT hr = pUnknown->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess));
    pUnknown->Release();

    byte* pBuffCopy = nullptr;
    pBufferByteAccess->Buffer(&pBuffCopy);

    now set pixelArray to pBuffCopy, Then we can create WriteableBitmap with pixelArray.

    We can use WriteableBitmap .

    ---King Star.

    Thanks Rob.


    winRT

    • Marked as answer by King Star Friday, July 27, 2012 4:00 AM
    Friday, July 27, 2012 3:59 AM
  • Note, that as someone said in the thread below - you should actually call Release() on the IBufferByteAccess instead of the IUnknown, so in your case that would be:

    pBufferByteAccess->Release()

    http://social.msdn.microsoft.com/Forums/is/winappswithnativecode/thread/f6503138-59c6-4bc4-95e3-a58290e86b19


    Filip Skakun

    Monday, December 3, 2012 11:43 PM