Direct2D bitmaps are hardware device dependent resources and not generally easily accessible from the CPU side of the system. Getting to the pixels of the ID2D1Bitmap
is hence difficult, it is not designed neither is it intended to be used that way. Any mechanism that gets raw bytes out of a Direct2D bitmap usually does so by creating a WIC bitmap out of it to access the byte stream. This way you can write it into a file or even a byte array and use some interop to access it in C#. If you want to write it to a file and reading it in WPF, this mechanism would work. If you have the bitmap data in a IWICBitmapSource
object at some time during processing, and if you can cache it, the job would be easier.
Converting a System.Drawing.Bitmap
to a System.Windows.Media.ImageSource
is answered here : Converting Bitmap to ImageSource (Thanks to @MSL who pointed out the above answer in comments).
At this point, the way you use it is a little unclear, a bit more of information on the intended use or a short sample would really help here.
EDIT 1:
Take a look at this SO Qn: Save ID2D1Bitmap to file and the comments for a version of code that saves a ID2D1Bitmap
into a file. It essentially does the conversion to IWICBitmap
and writes it out to a file. At this step, since IWICBitmap
inherits from IWICBitmapSource
you can even use the CopyPixels()
method to dump the bitmap data into a byte array, should you wish to do so.
Now if you are going with the approach of saving it to a file, getting a System.Drawing.Bitmap
is as simple as:
bitmap = new Bitmap("<YOUR_BITMAP_FILENAME>")
And you can convert it to a BitmapImage for use in WPF as:
using(MemoryStream memstream = new MemoryStream())
{
bitmap.Save(memstream, ImageFormat.Bmp);
memstream.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memstream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}