Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. using the contol.paint event

using the contol.paint event

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin Beukes
    wrote on last edited by
    #1

    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

    L D 2 Replies Last reply
    0
    • M Martin Beukes

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      M 1 Reply Last reply
      0
      • M Martin Beukes

        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

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        The PaintEventArgs are of no use, but you can call DrawToBitmap on the Control as Luc suggested in your Paint 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)

        1 Reply Last reply
        0
        • L Luc Pattyn

          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.


          M Offline
          M Offline
          Martin Beukes
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • M Martin Beukes

            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.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • L Luc Pattyn

              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

              M Offline
              M Offline
              Martin Beukes
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • M Martin Beukes

                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.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.


                M 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.


                  M Offline
                  M Offline
                  Martin Beukes
                  wrote on last edited by
                  #8

                  awesome. thanks. would help a ton

                  L 1 Reply Last reply
                  0
                  • M Martin Beukes

                    awesome. thanks. would help a ton

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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
                    
                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups