0

I have a RichTextBox that is a letterbox view of a long set of data. This means I have the vertical scroll bar to move what I see up and down. What I am trying to do is save the whole data as a bitmap even that not visible in the control. I can save the control as a bitmap but that is only part of the data.

Code to save control is borrowed from someone else which does indeed save the control view (thanks to whoever wrote it):

private void btn_SaveBitmap_Click(object sender, EventArgs e)
{
    SaveControlToBitmap(rchtxtbx_braille, "MyBitmap.bmp");
}

public void SaveControlToBitmap(Control control, string fileName)
{
    //Get graphics from the control  
    Graphics g = control.CreateGraphics();

    Bitmap bitmap = new Bitmap(control.Width, control.Height);

    control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));

    bitmap.Save(fileName);
    bitmap.Dispose();
}

This bitmap does not contain the data that is not visible in the control. I think somehow I need to find the start of the data and the end and turn that into a bitmap rather than turning the control itself into a bitmap but I am probably wrong. How can I do this please?

I have my laptops display set to 125% and this in itself may cause some issue. Please ask if this is not clear and I will try explain different way.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
user3884423
  • 556
  • 1
  • 5
  • 20
  • Why bitmap? If it's an export feature, then welcome to a beautiful world of reports (you need to generate one, I like PDF). – Sinatr Apr 21 '21 at 13:48
  • I don't think RTB supports DrawToBitmap. And `Graphics g = control.CreateGraphics();` is almost always useless. For other controls: DrawToBitmap will copy all element you draw __in the Paint event__ only. You may use CopyFromScreen as a workaround, but you would have to handle scrolling and stitching. I would consider writing a proper Print routine.. [Also look here](https://stackoverflow.com/questions/4974276/richtextbox-drawtobitmap-does-not-draw-containing-text) – TaW Apr 21 '21 at 13:52
  • Hm, I may have misspoken. In the link above EricLaw reports that younger versions of RTB will correctly work with DrawToBitmap. That doesn't mean you can use an invalid Graphics obejct.. So maybe to old trick of enlarging the control to hold all content will be good enough.. – TaW Apr 21 '21 at 13:58
  • Sinatr: I could save to PDF or any other type of file (jpg, png etc), how do you do that? – user3884423 Apr 21 '21 at 14:05
  • Take the `RichEditPrinter` class from here: [How to use PrintDocument with a scrollable Panel?](https://stackoverflow.com/a/57257205/7444103) – Jimi Apr 22 '21 at 01:27

0 Answers0