If I have a richTextBox and run DrawToBitmap on it, it doesn\'t draw any of the text inside of the richTextBox.
Bitmap b = new Bitmap(rtb.Width, rtb.Height
I tested the methods above, and whenever I load the saved Bitmap into an ImageViewer(Like Paint), it would have the SaveFileDialog-UI faded into the background of the text. Luckily, I found an easy fix:
SaveFileDialog bfsd = new SaveFileDialog();
var rtb = richTextBox1;
bfsd.Filter = "Bitmap (*.bmp)|*.bmp|All Files (*.*)|*.*";
bfsd.Title = "Save your text as a Bitmap File";
rtb.Update(); // Ensure RTB fully painted
Bitmap bmp = new Bitmap(rtb.Width, rtb.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.CopyFromScreen(rtb.PointToScreen(Point.Empty), Point.Empty, rtb.Size);
}
if (bfsd.ShowDialog()==DialogResult.OK)
{
Draw:
try
{
bmp.Save(bfsd.FileName);
bmp.Dispose();
}
catch (Exception)
{
DialogResult dr = MessageBox.Show("An error ocurred while attempting to save your Image...", "Error! Error!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dr == DialogResult.Retry)
{
goto Draw;
}
else if (dr == DialogResult.Cancel)
{
return;
}
}
Save
(Don't worry, it won't actually save the image until you press Save
)Pressing Cancel
doesn't effect the process, because when you press the Button
or MenuStripItem
to save it, it will update & re-paint it :)
I implemented a try
-catch
method, so that it will catch an error if one occurs, rather than the app just (Not Responding)
The catch
method is a Retry
Button
It will catch the error, and give you the choice to either Cancel
the whole Operation
, or Retry
I used a goto
to be able to just rewind, and make another attempt to save the file, rather than having the SaveFileDialog
appear again.
I hope this helps you :)