【发布时间】:2021-03-07 02:37:49
【问题描述】:
我正在将图像加载到墨水画布中,输入图像始终是单色的,然后我用白笔在该图像上绘图并打算保存它。
加载图像时,一些我知道是 1 像素厚的预先存在的线条添加了一个边缘,这不是单色的。
我解决这个问题的方法是渲染位图,然后丢弃值小于 255 的所有像素。
我曾尝试使用 BlackWhite 像素格式,但这会产生错误:
PresentationCore.dll 中出现“System.ArgumentException”类型的未处理异常
附加信息:此操作不支持“BlackWhite”像素格式。
渲染位图的代码行
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, System.Windows.Media.PixelFormats.BlackWhite);
我不确定问题是否在于我如何将它加载到墨迹画布中,以便代码也包含在下面
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;
}
}
【问题讨论】:
-
“添加了边缘” - 听起来像是缩放问题,请参阅 this。
-
或者抗锯齿,看这里:stackoverflow.com/q/6550731/1136211
-
缩放看起来是一个很好的解决方案,但它只适用于直线,然后线条是对角线的灰色阴影。有没有把渲染的位图变成单色的功能?
标签: c# wpf image-processing bitmap inkcanvas