繁体   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