using the contol.paint event
-
Hi, I am wondering if anyone coul help. I need use the paint event of a control to retun an image of that control. I dont even know where to start. Regards, Martin
-
Hi, I am wondering if anyone coul help. I need use the paint event of a control to retun an image of that control. I dont even know where to start. Regards, Martin
Martin Beukes wrote:
I need use the paint event of a control to retun an image of that control.
I don't think so. Either you want some special painting to occur in a Control, then you have to implement a Paint handler (this[^] would show the essentials), or you want a bitmap from an existing Control, then you would use
Control.DrawToBitmap()
which works fine for simple Controls, and needs quite some help for some Controls, such as RichTextBox (DrawToBitmap probably does call Paint itself, but that is not really relevant). :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
-
Hi, I am wondering if anyone coul help. I need use the paint event of a control to retun an image of that control. I dont even know where to start. Regards, Martin
The
PaintEventArgs
are of no use, but you can callDrawToBitmap
on theControl
as Luc suggested in yourPaint
handler... something like this (untested):private void Control_Paint(object sender, PaintEventArgs e)
{
Control control = sender as Control;
if (control != null)
{
using(MemoryStream memoryStream = new MemoryStream())
using (Bitmap bitmap = new Bitmap(memoryStream))
{
control.DrawToBitmap(bitmap, control.Bounds);
// bitmap is now in memoryStream
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Martin Beukes wrote:
I need use the paint event of a control to retun an image of that control.
I don't think so. Either you want some special painting to occur in a Control, then you have to implement a Paint handler (this[^] would show the essentials), or you want a bitmap from an existing Control, then you would use
Control.DrawToBitmap()
which works fine for simple Controls, and needs quite some help for some Controls, such as RichTextBox (DrawToBitmap probably does call Paint itself, but that is not really relevant). :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
Hi Luc, It is almost af if you can read minds. It is a richtextbox that i need an image of. There is no
Control.DrawToBitmap()
on the control and i have looked pretty much everywhere. I came accross this article [Here] which gave me the idea but none of the execution. If you know of any way to get an image of a RichTextBox without using a screen capture, it would be much appreciated. -
Hi Luc, It is almost af if you can read minds. It is a richtextbox that i need an image of. There is no
Control.DrawToBitmap()
on the control and i have looked pretty much everywhere. I came accross this article [Here] which gave me the idea but none of the execution. If you know of any way to get an image of a RichTextBox without using a screen capture, it would be much appreciated.Just to make sure, here are my assumptions: you have a RichTextBox, you did not add any explicit Paint or OnPaint method, it shows OK on the screen, but now you want to capture it to a bitmap without scraping the screen? if so I have that covered in C#. Is that OK? BTW: why do you want a bitmap? and do you need printing? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
modified on Monday, January 11, 2010 6:09 PM
-
Just to make sure, here are my assumptions: you have a RichTextBox, you did not add any explicit Paint or OnPaint method, it shows OK on the screen, but now you want to capture it to a bitmap without scraping the screen? if so I have that covered in C#. Is that OK? BTW: why do you want a bitmap? and do you need printing? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
modified on Monday, January 11, 2010 6:09 PM
Ok, I have 2 RTB with diffent contents I need to print these two RTBs contents on a single page My plan is to use the GDI to an image of each RTB and then cmbine and print them. Now the problem. more tha half my users will be on those godaweful netbooks with a screen resolution of 1024x6 and an A4 page and the my two RTBs is 1045x718 plus margins. So the problem is that i need to capture the whole control and not just what is onscreen.
-
Ok, I have 2 RTB with diffent contents I need to print these two RTBs contents on a single page My plan is to use the GDI to an image of each RTB and then cmbine and print them. Now the problem. more tha half my users will be on those godaweful netbooks with a screen resolution of 1024x6 and an A4 page and the my two RTBs is 1045x718 plus margins. So the problem is that i need to capture the whole control and not just what is onscreen.
I see. That is typical for the more complex controls: they hold a lot of data, not all of it visible at once, and you want to print more or all of it. You can try and solve that through DrawToBitmap (when available and functional), however that isn't very easy to do it right; it might be easier to skip the image and print directly. I have an RTB derivative that offers both: Print (not tested) and DrawToBitmap (tested and OK). It is C# code. Is that OK? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
-
I see. That is typical for the more complex controls: they hold a lot of data, not all of it visible at once, and you want to print more or all of it. You can try and solve that through DrawToBitmap (when available and functional), however that isn't very easy to do it right; it might be easier to skip the image and print directly. I have an RTB derivative that offers both: Print (not tested) and DrawToBitmap (tested and OK). It is C# code. Is that OK? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
awesome. thanks. would help a ton
-
awesome. thanks. would help a ton
Here you go:
////////////////////////////////////////////////////////////////////////////////////////////////
//
// CopyRight 2009, www.perceler.com, Luc Pattyn
//
// Version 1.0
//
// use freely for non-commercial use as long as this notice remains present;
// ask me before using commercially.
//
////////////////////////////////////////////////////////////////////////////////////////////////using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;namespace LP_Core {
/// /// LP\_RichTextBox is an extended RichTextBox offering Print functionality (courtesy Microsoft) /// and a proprietary method to draw all the content to a bitmap /// /// "http://support.microsoft.com/kb/812425" \[System.ComponentModel.DesignerCategory("Code")\] class LP\_RichTextBox : RichTextBox { //Convert the unit used by the .NET framework (1/100 inch) //and the unit used by Win32 API calls (twips 1/1440 inch) private const double anInch = 14.4; \[StructLayout(LayoutKind.Sequential)\] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } \[StructLayout(LayoutKind.Sequential)\] private struct CHARRANGE { public int cpMin; //First character of range (0 for start of doc) public int cpMax; //Last character of range (-1 for end of doc) } \[StructLayout(LayoutKind.Sequential)\] private struct FORMATRANGE { public IntPtr hdc; //Actual DC to draw on public IntPtr hdcTarget; //Target DC for determining text formatting public RECT rc; //Region of the DC to draw to (in twips) public RECT rcPage; //Region of the whole DC (page size) (in twips) public CHARRANGE chrg; //Range of text to draw (see earlier declaration) } private const int WM\_USER = 0x0400; private const int EM\_FORMATRANGE = WM\_USER + 57; \[DllImport("USER32.dll")\] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); // Render the contents of the RichTextBox for printing // Return the last character printed + 1 (printing start from this point for next page) // WARNING: I have not tested this method, I only used it as a starting point // to create the next method! public int Print(int charFrom, int charTo, PrintPageEventArgs e) { //Calculate the area to render and print RECT rect