CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    5

    Printing custom control

    Hello,

    I have a custom control, graphChart. I am using this control to draw graph, area , shape etc.

    MainForm is using this control. I want to print some part of graph.

    For example, my graph has grid, labels, shapes. I want to print only shapes.

    If it is whole graph, I can use control.CreateGraphics() but I want to print some part.

    Is there any way to achieve it?

    With regard,

  2. #2
    Join Date
    May 2010
    Posts
    7

    Re: Printing custom control

    What do you mean by print? Do you mean you only want to draw specific element of the control? If so, show the OnPaint code.

  3. #3
    Join Date
    Jun 2010
    Posts
    85

    Re: Printing custom control

    One down and dirty way is to get the image of the control into a bitmap the you can manipulate the image however you want, including drawing over regions that you dont want.

  4. #4
    Join Date
    Jul 2007
    Posts
    5

    Re: Printing custom control

    @Pickels
    You are right. Thats is what I am trying to do.
    Here is my code (psudo code)


    In My chartUserControl

    public Graphics graphicWithoutData;

    private void drawingPanel_Paint_1(object sender, PaintEventArgs e)

    {

    Graphics g = e.Graphics

    DrawGrid(g);

    DrawShape(g);

    graphicWithoutData = g;

    //I even tried with bitmap
    Size s = this.Size;
    BitmapWithoutDistData = new Bitmap(s.Width, s.Height, g);

    DrawData(g);

    }

    when i try to access graphicWithoutData (public member) in my main form, it gave me an exception : Invalid parameter.
    When I try to print BitmapWithoutDistData (public member) in my main form, it was blank
    Is there any wrong in my way of approach to this problem.
    If you know any other way let me know.

    @[email protected]
    Above is my code. But even that bitmap (BitmapWithoutDistData) is blank. i.e when I print it on doc it was blank

  5. #5
    Join Date
    Jun 2010
    Posts
    85

    Re: Printing custom control

    try doing it this way

    Code:
            private void drawingPanel_Paint_1(object sender, PaintEventArgs e)
            {
    
                using (Bitmap bmp = new Bitmap(s.Width, s.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        DrawGrid(g);
                        DrawShape(g);
                    }
                    //At this point bmp should be the image I think you are looking for
                    e.Graphics.DrawImage(bmp) ;
                    DrawData(e.Graphics);
                }
    
            }

  6. #6
    Join Date
    Jul 2007
    Posts
    5

    Re: Printing custom control

    Thanks, thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured