Developer Reference Drawing Charts To Files

It is really easy to draw some chart data on a .NET Bitmap object. You can then do all sorts of things with it. In this sample code we will save the image as a .png file.

By using different .NET enumeration values you can save to a number of other image types. (bmp, emf, exif, gif, icon, jpg, png, tiff, wmf).

So first we initialise the library, open a path to a SENC holding our chart data and create a draw object to draw our chart. The code's in C#...

// start the library up...
ENCX.Library encx = new ENCX.Library();
encx.Construct("", "");
// open the senc... (hardcoded path here)
ENCX.S57Manager encMan = new ENCX.S57Manager();
encMan.OpenSenc(encx, "c:\\program files\\encx sdk\\senc_demo");
// create drawing object and associate it with the manager of the senc
ENCX.S57Draw encDraw = new ENCX.S57Draw();
encDraw.SetManager(encMan);
// This is a better pixel size - real size for a 100 DPI screen.
encDraw.PixelSizeMetres = 0.000254;

Then we set the size of the view to be 600 by 600 pixels and set the scale to 1:10000. Specifying an invarient point of (-1,-1) here is convenient shorthand for "the centre of the view". (-1,-1) is correct in many situations (but if the user was zooming using the mouse then it would be better to keep the invarient point where the mouse was).

We then set the position to a hardcoded value which works for the test data supplied with the download. As a general tip when adjusting scale and position, change the scale first.

const int nWidth = 600;
const int nHeight = 600;
// set size of view
ENCX.PixelSize pixelSize = new ENCX.PixelSizeClass();
pixelSize.Height = nHeight;
pixelSize.Width = nWidth;
encDraw.SetSize(pixelSize);
                
ENCX.PixelPoint ptCentre = new ENCX.PixelPoint();
ptCentre.X = -1;
ptCentre.Y = -1;
                
encDraw.SetDisplayScale(10000, ptCentre);
                
ENCX.GeoPoint geoPt = new ENCX.GeoPoint();
geoPt.Lat = 53.57252;
geoPt.Lon = 0.10460;
encDraw.SetPosition(geoPt, ptCentre);

Now we have an S57Draw object all initialised and ready to draw some chart.

We create a Bitmap, get a Graphics from it and then get a device context handle from the Graphics. We give the device context handle to the ENCX.S57Draw object as a parameter to the call to Draw(..). This gets us a Bitmap object initialised with a chunk of chart.

Bitmap image = new Bitmap(nWidth, nHeight);
Graphics g = Graphics.FromImage(image);
System.IntPtr iphDC = g.GetHdc();
encDraw.Draw(iphDC.ToInt32());
g.ReleaseHdc(iphDC);

All that remains is to save the image...

image.Save("chart.png", ImageFormat.Png);

You will also need the using statements in your code for this to build (and a reference to ENCX in your project)...

using System;
using ENCX;
using System.Drawing;
using System.Drawing.Imaging;