简体   繁体   中英

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.

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).

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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