是否可以将文本框转换为BitmapImage或BitmapFrame?

时间:2012-01-06 18:16:43

标签: c# wpf

我有一个画布,它有一个文档的位图图像,并在其上的不同位置有几个文本框。存在文本框以阻挡其背后的文本,但也可能包含顶部的文本(想象阻止机密文档中的文本)。这一切都需要保存为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时,它会保存一个黑色图像,告诉我它没有正确转换文本框(或者至少按照我想要的方式)。

这甚至可能吗?

1 个答案:

答案 0 :(得分:0)

为什么要单独渲染每个控件?此外,您没有使用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不会打扰它。

相关问题