簡體   English   中英

是否可以將文本框轉換為BitmapImage或BitmapFrame?

[英]Is it possible to convert text boxes to a BitmapImage or BitmapFrame?

我有一個畫布,其中有一個文檔的位圖圖像,並在其不同位置上有幾個文本框。 存在文本框以阻止其后面的文本,但也可能在頂部包含文本(可以想象將機密文檔中的文本阻止)。 所有這些都需要另存為tiff文件。

我已經能夠輕松保存圖像,但是事實證明,將文本框保存在頂部是真正的挑戰。 這是我目前所擁有的。

//The document bitmap is the first item in the canvas
foreach (Control redaction in canvas.Children)
{
    Size sizeOfControl = new Size(redaction.Width, redaction.Height);
    renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
    renderBitmap.Render(redaction);
    tiffEncoder.Frames.Add(BitmapFrame.Create(frame));
}
FileStream fs = new FileStream(outputFileName, FileMode.Create);
tiffEncoder.Save(fs);
fs.Flush();
fs.Close();

當我不將文檔位圖添加到tiffEncoder時,它會保存黑色圖像,這告訴我它沒有正確轉換文本框(或至少是我想要的方式)。

那有可能嗎?

為什么要分別渲染每個控件? 另外,您不是使用renderBitmap而是將frame添加到編碼器中(不確定來自何處)。 這應該呈現畫布及其上的所有控件:

var bm = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height,
                                96d, 96d, PixelFormats.Pbgra32);
tiffEncoder.Frames.Add(BitmapFrame.Create(bm));

using (var fs = new FileStream(outputFileName, FileMode.Create))
{
    tiffEncoder.Save(fs);
}

還要注意,畫布必須是可見的(因此不得最小化窗口),否則WPF不會麻煩渲染它。

暫無
暫無

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

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