views:

410

answers:

2

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.

+1  A: 

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
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
Are you creating the bitmap before or after painting?
leppie
Doing everything in the PrintPage event. I edited the sample code in an attempt to clarify.
Tom Bushell
+1  A: 

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
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
See this question: http://stackoverflow.com/questions/597037/how-to-copy-one-graphics-object-into-another
C. Ross
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