10374 : Print to a bitmap

Question

I would like to enhance the print functionality for GTP. I’d like the productvery much , only the print functionality is a bit poor.

My question is how to obtain the image that is been posed on the printpreview. When a print preview is genarated I can only find information about the graphics object but not the image:

System.Void PrintPage ( System.Drawing.Graphics G, System.Drawing.Rectangle Margins, System.Int32 aGridWidth, System.Int32 aDateScalerHeight, System.Boolean& HasMorePages)

I noticed that there are images(bitmaps) for the grid and datescalar but I’m looking for one image object of the complete gtp (grid + datescalar). Can you please give me some information for my problem?

Answer

You can send in a graphics object that comes from a bitmap like this:

Bitmap b=new Bitmap(w,h);
Graphics g=Graphics.FromImage(b);

And the call PrintPage with that… You will then get the complete print in bitmap!

Sample code:


    // Print to a bitmap file
    Bitmap bm=new Bitmap(CapTimingGantt.Gantt.Width, CapTimingGantt.Gantt.Height);
    Graphics g = Graphics.FromImage(bm);
    g.FillRectangle(new SolidBrush(Color.Tomato), r);
    g.DrawRectangle(Pens.Black, r);
    bool hasMorePages = false;
    Rectangle r = Rectangle.FromLTRB(0, 0, CapTimingGantt.Gantt.Width, CapTimingGantt.Gantt.Height);
    CapTimingGantt.Gantt.PrintInit(null);
    CapTimingGantt.Gantt.PrintPage(g, r, 150, 28, ref hasMorePages);
    CapTimingGantt.Gantt.PrintEnd();
    bm.Save("c:\\temp\\test2.bmp");

 

To print to an EMF instead go like this:


    // Print to emf, enhanced meta file, the windows vector format
    Graphics g = CapTimingGantt.Gantt.CreateGraphics();
    IntPtr pHdc = g.GetHdc();
    Metafile mf = new Metafile("c:\\temp\\test2.emf", pHdc);
    g.ReleaseHdc(pHdc);
    g.Dispose();
    g = Graphics.FromImage(mf);
    bool hasMorePages = false;
       
    Rectangle r = Rectangle.FromLTRB(0, 0, CapTimingGantt.Gantt.Width, CapTimingGantt.Gantt.Height);
    g.FillRectangle(new SolidBrush(Color.Tomato),r);
    g.DrawRectangle(Pens.Black, r);
    CapTimingGantt.Gantt.PrintInit(null);
    CapTimingGantt.Gantt.PrintPage(g, r, 150, 28, ref hasMorePages);
    CapTimingGantt.Gantt.PrintEnd();
   
    g.Dispose();

 

We also get questions on how to print a bitmap that is larger than what is on screen.

And this is ofcourse totally analouge, just create a larger bitmap and a larger rectangle...

 

 

 

// Print to a bitmap file

Bitmap bm = new Bitmap(gantt1.Width*10, gantt1.Height*3);

Graphics g = Graphics.FromImage(bm);

Rectangle r = Rectangle.FromLTRB(0, 0, gantt1.Width * 10, gantt1.Height * 3);

bool hasMorePages = false;

gantt1.PrintInit(null);

gantt1.PrintPage(g, r, 150, 28, ref hasMorePages);

gantt1.PrintEnd();

bm.Save("c:\\temp\\test2.bmp");

pictureBoxImageMaps.Image = bm;

 
 
Further note: When printing from a webpage and you use collisiondetection the print sequence should look like this:

      Gantt_ASP1.Gantt.TurnOffAllCollisionDetect=false;   <---  ADD THIS< ?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

      Gantt_ASP1.Gantt.PrintInit(null);

      Gantt_ASP1.Gantt.PrintPage(g, r, 150, 28, ref hasMorePages);

      Gantt_ASP1.Gantt.PrintEnd();

Leave a Reply