Search code examples
c#mvvmwinui-3bitmapimagewinui

Remove Black Background from BitmapImage WinUI 3


First time app developer, I am trying to remove the black from the back of a scanned image. This image is sent by multiple different scanners and saved to the desktop as a PNG. I am trying to take that saved image and remove the black background and display the image in a desktop app.

I have found multiple ways of manipulating a Bitmap but these aren't working for a BitmapImage which is what I am using for WinUI 3.

I have also seen WriteableBitmap but I am unable to use CopyPixel() or any other methods provided.

This is the code I currently have to display the image in the app:

BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bi.UriSource = new Uri(back);
this.Front.Source = bi;

An example of what I am looking for in WinUI 3:

Convert Specific BitmapImage Colour to Transparent

Any help is greatly appreciated, thanks!


Solution

  • Turns out VisualStudio was bugging out and didn't show me the Install System.Drawing.Common until restart :(

    Was able to solve this with:

    Bitmap bitmap = new Bitmap(front);
    bitmap.MakeTransparent(Color.Black);
    bitmap.Save(front);