Search code examples
c#wpfbitmapbitmapsource

WPF: Converting a BitmapImage to an array and edit it, then recreating it


I want to develop a Steganography software in WPF, so I need direct access to pixels, I am trying to convert a Bitmap to an array and edit it, the problem is I can only recreate it by BitmapSource (am I right?) and I can't recreate it correctly. I get a Black and White Image instead.

public static void Conceal(BitmapImage CoverPhotoBitmap, BitmapImage HiddenPhotoBitmap, ref BitmapSource ResultPhotoBitmapSource)
{
    int stride = CoverPhotoBitmap.PixelWidth * 4;
    int size = CoverPhotoBitmap.PixelHeight * stride;
    byte[] CoverPhotoPixels = new byte[size];
    CoverPhotoBitmap.CopyPixels(CoverPhotoPixels, stride, 0);

    byte[] HiddenPhotoPixels = new byte[size];
    HiddenPhotoBitmap.CopyPixels(HiddenPhotoPixels, stride, 0);
    ResultPhotoBitmapSource = BitmapSource.Create(CoverPhotoBitmap.PixelWidth, CoverPhotoBitmap.PixelHeight, 96, 96, PixelFormats.Rgb32, null, HiddenPhotoPixels, stride);
}

Solution

  • You may convert your resulting BitmapSource to a BitmapImage. Check the answer to this question: BitmapSource to BitmapImage.

    Not marking as duplicate since it's not the same question, although the answer applies.