简体   繁体   English

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

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

I have a canvas that has a bitmap image of a document and several textboxes in various places on it. 我有一个画布,其中有一个文档的位图图像,并在其不同位置上有几个文本框。 The text boxes exist to block out the text behind it but also may contain text over top (imagine blocking out text in a confidential document). 存在文本框以阻止其后面的文本,但也可能在顶部包含文本(可以想象将机密文档中的文本阻止)。 This all needs to be saved as a tiff file. 所有这些都需要另存为tiff文件。

I have been able to save the image easily however saving the text boxes over top has proved to be the real challenge. 我已经能够轻松保存图像,但是事实证明,将文本框保存在顶部是真正的挑战。 Here is what I have currently. 这是我目前所拥有的。

//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();

When I don't add the document bitmap to the tiffEncoder, it saves a black image, which tells me that it isn't converting the text boxes correctly (or at least the way I want it to). 当我不将文档位图添加到tiffEncoder时,它会保存黑色图像,这告诉我它没有正确转换文本框(或至少是我想要的方式)。

So is this even possible? 那有可能吗?

Why do you render each control individually? 为什么要分别渲染每个控件? Also you are not using renderBitmap but frame to add to the encoder (not sure where that comes from). 另外,您不是使用renderBitmap而是将frame添加到编码器中(不确定来自何处)。 This should render the canvas and all controls on it: 这应该呈现画布及其上的所有控件:

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);
}

Also note that the canvas must be visible (so window must not be minimized) otherwise WPF won't bother rendering it. 还要注意,画布必须是可见的(因此不得最小化窗口),否则WPF不会麻烦渲染它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM