public BitmapSource ToBitmapSource()
{
string mapping = "RGB";
MediaPixelFormat format = MediaPixelFormats.Rgb24;
if (HasAlpha)
{
mapping = "BGRA";
format = MediaPixelFormats.Bgra32;
}
if (ColorSpace == ColorSpace.CMYK)
{
mapping = "CMYK";
format = MediaPixelFormats.Cmyk32;
}
int step = format.BitsPerPixel / 8;
int stride = Width * step;
using (PixelCollection pixels = GetPixels())
{
byte[] bytes = pixels.ToByteArray(mapping);
return BitmapSource.Create(Width, Height, 96, 96, format, null, bytes, stride);
}
}
#endif