MC73654
MC73654

Reputation: 33

C# WPF rendering true monochromatic image from ink canvas

I am loading an image into an ink canvas, the input image is always monochromatic, I am then drawing on that image with a white pen and intending to save it. When the image is loaded some of the pre-existing lines which I know to be 1 pixel thick have an edge added to them which isn't monochromatic.
The way I have though to fix this is by rendering the bitmap and then discarding all pixels with a value of less than 255.

I have tried to use the pixel format BlackWhite, however this generates the error:


An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll

Additional information: 'BlackWhite' PixelFormat is not supported for this operation.


The line of code rendering the bitmap

RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, System.Windows.Media.PixelFormats.BlackWhite);

I'm not sure if the issue lies in how I loaded it into the ink canvas so that code is also included below

private void LoadImagetoCanvas(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
        Nullable<bool> result = openFileDlg.ShowDialog();
        if (result == true)
        {
            global.canvas1filepath = openFileDlg.FileName;
            System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
            myImage.Source = new BitmapImage(new Uri(global.canvas1filepath));
            BitmapImage bmp = new BitmapImage(new Uri(global.canvas1filepath, UriKind.Absolute));
            global.canvas1imagexpixels = (int)bmp.Width;
            global.canvas1imageypixels = (int)bmp.Height;
            ImageBrush canvas1Background = new ImageBrush();
            canvas1Background.ImageSource = new BitmapImage(new Uri(global.canvas1filepath, UriKind.Relative));
            inkCanvas1.Background = canvas1Background;
        }
    }

Upvotes: 0

Views: 198

Answers (1)

MC73654
MC73654

Reputation: 33

I'll compile an answer here - thanks to Sinatr & Clemens for their help with the theory and examples

The solution is based in the XMAL code for the ink canvas i'm using, I found adding the two following properties removed any edges to the 1 pixel wide lines I was drawing:

RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"

Upvotes: 1

Related Questions