簡體   English   中英

C# WPF 從墨水畫布渲染真正的單色圖像

[英]C# WPF rendering true monochromatic image from ink canvas

我正在將圖像加載到墨水畫布中,輸入圖像始終是單色的,然后我用白筆在該圖像上繪制並打算保存它。 當圖像加載時,一些我知道是 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;
        }
    }

我會在這里編譯一個答案 - 感謝 Sinatr & Clemens 對理論和示例的幫助

該解決方案基於我正在使用的墨水畫布的 XMAL 代碼,我發現添加以下兩個屬性刪除了我正在繪制的 1 像素寬線的任何邊緣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM