Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WaveformGraph to Image

Hello,

I am interested in converting a WaveformGraph's System.Drawing.Graphics object used in the PrintPage handler to a System.Drawing.Image object. Here's what I'm trying to do:

Graphics g = e.Graphics;
Rectangle bounds = e.MarginBounds;
waveformGraph1.Draw( new ComponentDrawArgs( g, bounds ) ) ;

Image img = g.?? ;

Can this be done or is there a better method of doing this?

Thanks in advance!
Derek
0 Kudos
Message 1 of 16
(5,694 Views)
Derek,

I am assuming e.Graphics is the Graphics object from the event arguments of the PrintPage handler.

I don't think there is a way to go from a Graphics object to an Image. You can create another Graphics object from an existing Image. For this example, assume that the Image that you are trying to create is a Bitmap. Then, your code would be modified to look something like this:

Graphics g = e.Graphics;
Rectangle bounds = e.MarginBounds;
waveformGraph1.Draw( new ComponentDrawArgs( g, bounds ) ) ;

// This will draw the printed output to an image
// of type bitmap. You can substitute Bitmap
// to be any other Image derived type.
Bitmap img = new Bitmap(bounds);
Graphics imgGraphics = Graphics.FromImage(img);
waveformGraph1.Draw( new
ComponentDrawArgs( imgGraphics, bounds ) );

Hope this helps.

Abhishek Ghuwalewala
Measurement Studio
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 2 of 16
(5,691 Views)
You don't convert a Graphics object to an image - the Graphics class provides the ability to draw to a device context where that context can be an image, the screen, a printer, etc. What you can do, though, is create an image, then get a Graphics object from the image, then drawing operations on that Graphics object will be directed to the image. So, for example, if you wanted to dynamically create an image and draw the WaveformGraph to it, you could do this:

using NationalInstruments.UI;
using System.Drawing.Imaging;

// ...

Size imageSize = new Size(300, 200);

using (Bitmap bitmap = new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format32bppArgb))
using (Graphics g = Graphics.FromImage(bitmap))
{
waveformGraph1.Draw(new Compon
entDrawArgs(g, new Rectangle(new Point(0, 0), imageSize)));

// ...
}

- Elton
Message 3 of 16
(5,691 Views)
Hi Elton,

This is exactly what I want! I found a .NET Report Printing library that makes it very easy to print text, headers, footers, columns, tables, and images (via System.Image), but it does it separately from the PrintPage event handler. I couldn't figure out how to get the Image independently from this event.

Thanks again,
Derek
0 Kudos
Message 4 of 16
(5,691 Views)
Hi Elton,

I finally had a chance to go back and work on this, but had a question. Your code does exactly what I need, but the image is not very good. I've attached an image of a portion of the app running and the print preview using your suggested method. It's very grainy. Any ideas on how to fix this?

Thanks,
Derek
Download All
0 Kudos
Message 5 of 16
(5,692 Views)
Hi Elton,

I've attached a sample that demostrates this. The screenshots above don't really show the problem.

Thanks,
Derek
0 Kudos
Message 6 of 16
(5,695 Views)
Has anyone found a better solution to this problem?

The print preview results are a little out-of-focus.
Philip Newman
General Dynamics
Electric Boat
0 Kudos
Message 7 of 16
(5,425 Views)
Try making the size of the bitmap the same size as the graph control


private Image GetWaveformGraphImage()
        {
            Size imageSize = waveformGraph1.Size ;
            Image bitmap = new Bitmap( imageSize.Width, imageSize.Height, PixelFormat.Format32bppArgb ) ;
            using ( Graphics g = Graphics.FromImage( bitmap ) )
            {
                waveformGraph1.Draw( new ComponentDrawArgs( g, new Rectangle(new Point(0, 0), imageSize ) ) );
            }
            return bitmap;
        }

I think this should clear up the graph image.
Hope this helps.
Bilal Durrani
NI
0 Kudos
Message 8 of 16
(5,417 Views)
great idea
thanks
Philip Newman
General Dynamics
Electric Boat
0 Kudos
Message 9 of 16
(5,410 Views)
hi everybody
I have used waveformGraph in my code and I can show the data I give on the screen ok but does anyone know how I can get data that is shown on the screen anytime?
I mean is there any function I can use the get the data of the waveformGraph?
thank you
0 Kudos
Message 10 of 16
(5,318 Views)