1

This is the high level problem I'm trying to solve...

I have a 3rd party plotting/graphing (IoComp Plot), and I want to embed a high quality (at least 600 dpi) bitmap of the Plot control in reports created by another 3rd party report package (Combit List & Label).

This is the approach that seems most promising so far...

---Edit---:

After trying many other approachs, the only one that I think I can make work is to create a hidden instance of the Plot control, with everything scaled up to printer size (approx 5 times screen size). That includes width and height, font sizes, line widths - every visible component of the control. Yuck!

------

I can get a Graphics object of the proper resolution from the plot control's PrintPage event, but converting it to a Bitmap so the report package will be happy is proving to be the major stumbling block. Several hours of searching has led to other people who asked the same question, but no viable answers.

The only promising lead I've found suggested using one of the Bitmap constructors, which takes a Graphics instance as a parameter.

However, it's not working for me. It creates Bitmap, but with no content from the Plot control - it is a pure black image.

Here's my code (edited to show drawing of red line to Graphics object):

void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
        // Draw a red line on the Graphics object. When printed, this
        // line is shown as part of the normal Plot graphics.
        Pen myPen;
        myPen = new Pen(Color.Red);
        e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
        myPen.Dispose();

        // Create a bitmap from the Graphics object
        Bitmap bm = new Bitmap(1000, 1000, e.Graphics);

        // Save to disk 
        // DOES NOT WORK - CREATES FILE THAT IS PURE BLACK (VIEWED
        // WITH "PAINT" PROGRAM)
        bm.Save(@"C:\Bicw_Dev\Bic.Net\FrontEnd\GraphicsToBmp.bmp", ImageFormat.Bmp);
        bm.Dispose();
}

Can anyone suggest why this doesn't work? Is this even a valid approach?

Also, please note:

As far as I can determine (and I've spent quite a bit of time looking) there is no way to get a high resolution, print quality Bitmap from the Plot control directly!

I stress this because several others who asked the question got code samples in response that solved the opposite problem - converting a Bitmap to a Graphics.

I need to convert a Graphics object to a Bitmap object.

And if anyone can suggest an alternate approach that allows me to get a print quality image of my plots into my reports, please feel free. (For example, I can get a low quality (72 bpi) Bitmap from the Plot control, and have considered trying to stretch it - but I've never seen that approach work well in other applications).

Thanks,

-Tom Bushell

Edit in response to comment:

As an experiment, I added the following:

Pen myPen; 
myPen = new Pen(Color.Red); 
e.Graphics.DrawLine(myPen, 0, 0, 200, 200); 
myPen.Dispose(); 

This caused a red line to be drawn over the plot graphics when I printed my plot. But it had no effect on the Bitmap - it's still pure black.

VladL
  • 12,769
  • 10
  • 63
  • 83
Tom Bushell
  • 5,865
  • 4
  • 45
  • 60

3 Answers3

1

However, it's not working for me. It creates Bitmap, but with no content from the Plot control - it is a pure black image.

Well you never do paint to the graphics, what do you expect?

You are suppose to do the actual drawing for the output in that event.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • As an experiment, I added the following: Pen myPen; myPen = new Pen(Color.Red); e.Graphics.DrawLine(myPen, 0, 0, 200, 200); myPen.Dispose(); This caused a red line to be drawn over the plot graphics when I printed my plot. But it has no effect on the Bitmap - it's still pure black. -Tom – Tom Bushell Sep 16 '09 at 14:52
  • Are you creating the bitmap before or after painting? – leppie Sep 16 '09 at 14:57
  • Doing everything in the PrintPage event. I edited the sample code in an attempt to clarify. – Tom Bushell Sep 16 '09 at 15:28
1

The constructor you're using does not copy any graphics, only sets the resolution equal to the graphics resolution. See msdn. As Leppie points out, you have to actually draw something to the bitmap. I suggest getting another graphics object for the item you just created.

Graphics g = Graphics.FromImage(bmp);
//Make the background white
g.FillRectangle(Brushes.White, 0, 0, 1000, 1000);
C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • OK, that makes sense. I'm assuming the plot control has already drawn to the Graphics object - how can I copy the contents to the new Bitmap object? – Tom Bushell Sep 16 '09 at 15:02
  • See this question: http://stackoverflow.com/questions/597037/how-to-copy-one-graphics-object-into-another – C. Ross Sep 16 '09 at 15:05
  • Thanks for the suggestion. I tried adding "plot1.DrawToBitmap(bm, (new Rectangle(0, 0, 1000, 1000)));" before saving the Bitmap. I now see the plot image in the file, but it's at screen resolution (72 dpi). I need print resolution (600 dpi). – Tom Bushell Sep 16 '09 at 15:49
1

It is not easy to control the DPI in the Print(Preview) system.

But maybe you are going about it the wrong way. What you want is to create a (large) bitmap and then 'trick' the control in to using that bitmap as if it was the screen. See this list of links.
No need for PrintDocument.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Henk - thanks for the links. I went through them, and found a promising one that imported the GDI (not GDI+) BitBlt function, and used it to copy from a Graphics object to a Bitmap associated with another Graphics. But didn't work for me, not sure why. Can you point me at a more specific link on how to "trick" a control? – Tom Bushell Sep 16 '09 at 21:03
  • I meant like the top one: Painting a Control onto a Graphics object , http://blog.tcx.be/2004/12/painting-control-onto-graphics-object.html – H H Sep 16 '09 at 23:25
  • I re-read that article, and tested the SendMessage approach that was described. Same problem as with some of the other approaches - the bitmap created is only screen resolution. I need _printer_ resolution. – Tom Bushell Sep 17 '09 at 22:00