Share via


IXRWriteableBitmap (Compact 2013)

3/28/2014

This C++ class represents a bitmap that an application can write to and update at run time.

Syntax

class IXRWriteableBitmap : public IXRBitmapSource

Methods

Method

Description

IXRWriteableBitmap::GetPixels

Retrieves an array of pixels inside the bitmap surface area that the inherited methods IXRBitmapSource::GetPixelHeight and IXRBitmapSource::GetPixelWidth define.

IXRWriteableBitmap::Init

Initializes a new instance of IXRWriteableBitmap.

IXRWriteableBitmap::Invalidate

Requests XAML for Windows Embedded to redraw the entire bitmap.

IXRWriteableBitmap::Lock

Locks a bitmap so that only the current thread can access it until you call IXRWriteableBitmap::Unlock.

IXRWriteableBitmap::Render

Renders an element as a bitmap.

IXRWriteableBitmap::Unlock

Unlocks a bitmap so that other threads can access it.

Thread Safety

Members of this class are thread safe if you previously called IXRApplication::CreateHostFromXaml and supplied it with an XRWindowCreateParams structure that has AllowsMultipleThreadAccess set to true.

Remarks

To render a bitmap once, call IXRWriteableBitmap::Init. To render a bitmap multiple times, call IXRWriteableBitmap::Render.

To write to a rendered bitmap, first retrieve the pixels of the bitmap by calling IXRWriteableBitmap::GetPixels and then modify those pixels. For example, to assign new colors to pixels, you can use the RGBA macro.

After you write to the bitmap, call IXRWriteableBitmap::Invalidate to invalidate the old copy of the bitmap and force re-rendering of the modified bitmap.

When you create a class instance, use an IXRWriteableBitmapPtr smart pointer instead of a raw interface pointer. For more information, see XRPtr<Interface>.

Inheritance Hierarchy

IXRBitmapSource

     IXRWriteableBitmap

Example

The following example code creates a writeable bitmap, retrieves its pixel array, and then changes the alpha value of the pixels that match a specific RGB color code.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "windows.h"
#include "XamlRuntime.h"
#include "XRPtr.h"

void ConvertImageToWriteableBitmap(IXRApplication* pApplication, IXRImage* pImage)
{

  // Create writeable bitmap object and initialize variables
  IXRWriteableBitmapPtr pWriteableBitmap;
  pApplication->CreateObject(&pWriteableBitmap);
  
  bool BackBuffer = true;
  float Height = 250;
  float Width = 250;

  int ArrayLength = (int)Width * (int)Height;
  UINT* pPixels = new UINT[ArrayLength];

  // Set dimensions of pImage, and set writeable bitmap as image source
  pImage->SetHeight(Height);
  pImage->SetWidth(Width);
  pImage->SetSource(pWriteableBitmap);

  // Render bitmap using the image dimensions
  pWriteableBitmap->Init((int)Width, (int)Height, BackBuffer);

  // Lock thread access to the bitmap
  pWriteableBitmap->Lock();

  // Retrieve the bitmap's pixels
  pWriteableBitmap->GetPixels(&pPixels);

  // Adjust the alpha component of the bitmap's pixels
  if(pPixels)
  {
       
       int pixelColorSolid    = RGBA(51, 0, 204, 0);
       int pixelColorAlpha    = RGBA(51, 0, 204, 150);
       
       for(int i = 0; i < ArrayLength; i++)
       {
            if(pPixels[i] == pixelColorSolid)
            {
                 pPixels[i] = pixelColorAlpha;
            }
       }
  }

  // Draw the bitmap
  pWriteableBitmap->Invalidate();

  // Unlock the bitmap so that other threads can access it
  pWriteableBitmap->Unlock();
}

This code example assumes that an IXRApplication instance has already been initialized. After you initialize the IXRApplication instance, you must add an IXRImage object to the visual tree, and then you can call the example method ConvertImageToWriteableBitmap, which takes both objects as input parameters, in your application code. All visual elements, including those parsed from XAML, are stored in a visual tree.

.NET Framework Equivalent

System.Windows.Media.Imaging.WriteableBitmap

Requirements

Header

XamlRuntime.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

Classes for UI Element Management