C#WPF从墨水画布渲染真实单色图像

C#WPF从墨水画布渲染真实单色图像,c#,wpf,image-processing,bitmap,inkcanvas,C#,Wpf,Image Processing,Bitmap,Inkcanvas,我将图像加载到墨水画布中,输入图像始终是单色的,然后我用白色笔在该图像上绘制并打算保存它。 加载图像时,我知道一些1像素厚的预先存在的线条会添加一条非单色的边缘。 我解决这个问题的方法是渲染位图,然后丢弃所有值小于255的像素 我尝试使用黑白像素格式,但这会产生错误: PresentationCore.dll中发生类型为“System.ArgumentException”的未处理异常 其他信息:此操作不支持“黑白”像素格式 显示位图的代码行 RenderTargetBitmap rtb =

我将图像加载到墨水画布中,输入图像始终是单色的,然后我用白色笔在该图像上绘制并打算保存它。 加载图像时,我知道一些1像素厚的预先存在的线条会添加一条非单色的边缘。
我解决这个问题的方法是渲染位图,然后丢弃所有值小于255的像素

我尝试使用黑白像素格式,但这会产生错误:


PresentationCore.dll中发生类型为“System.ArgumentException”的未处理异常

其他信息:此操作不支持“黑白”像素格式


显示位图的代码行

RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, System.Windows.Media.PixelFormats.BlackWhite);
我不确定问题是否在于如何将其加载到ink画布中,以便代码也包含在下面

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;
        }
    }
private void LoadImagetoCanvas(对象发送方、路由目标方)
{
Microsoft.Win32.OpenFileDialog openFileDlg=新建Microsoft.Win32.OpenFileDialog();
Nullable result=openFileDlg.ShowDialog();
如果(结果==真)
{
global.canvas1filepath=openFileDlg.FileName;
System.Windows.Controls.Image myImage=新建System.Windows.Controls.Image();
myImage.Source=新的位图图像(新的Uri(global.canvas1filepath));
BitmapImage bmp=新的BitmapImage(新Uri(global.canvas1filepath,UriKind.Absolute));
global.canvas1imagexpixels=(int)bmp.Width;
global.canvas1ImagePixels=(int)bmp.Height;
ImageBrush canvas1Background=新的ImageBrush();
canvas1Background.ImageSource=新的位图图像(新Uri(global.canvas1filepath,UriKind.Relative));
inkCanvas1.Background=canvas1Background;
}
}

我将在这里编译一个答案-感谢Sinatr&Clemens在理论和示例方面的帮助

该解决方案基于我正在使用的墨水画布的XMAL代码,我发现添加以下两个属性会删除我正在绘制的1像素宽线条的任何边:

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

“添加一条边”-听起来像是缩放问题,请参见。或者抗锯齿,请参见此处:缩放看起来是一个很好的解决方案,但它只适用于直线,然后直线是灰色的对角阴影。是否有将渲染位图变为单色的功能?