0

I am currently working on a C# application that aims to do some computation and output graphs in a pdf file.

I use the Zedgraph library to draw my graphs so something like graphPane.AddCurve(PointPairList). Now I tried to output these graphs to pdf file via MigraDoc package.

Currently, I have a script that map the Zedgraph to bitmap then paste it on the pdf file. So something like this:

private Bitmap getBitMap()
{

  ZedGraphControl graph = new ZedGraphControl();

  newGraph = graphPane.Clone();

  SizeF s = new SizeF(3.5f, 4.5f);
  newGraph.Scale(s);

  newGraph.DrawToBitmap(bit, new Rectangle(0, 0, newGraph.Width,    newGraph.Height));

  return bit;

}

The problem is that this give me a slightly pixellated image on the pdf page. And I need this graph to be in a very high quality. So are there anything I can change the improve the quality or do i have to change my entire approach for such thing.

Thank you so much in advance.

TaW
  • 53,122
  • 8
  • 69
  • 111
bchu1392
  • 59
  • 2
  • 8

1 Answers1

0

By default a Bitmap you create has your current screen resolution which could be as low as 75dpi, more common 96dpi; more modern monitors have 120dpi or more, but a good print quality starts 150dpi. For really crips images you want 300dpi and to allow zooming you may want to have 600dpi or more..

So you need to create and fill a bitmap with a larger size and take control of its dpi resolution.

Assuming your size of 3.5f x 4.5f is inches, for 300dpi you need a Bitmap with 1050 x 1350 pixels.

So you should create such a Bitmap..:

Bitmap bmp = new Bitmap(1050, 1350);

..and set the resolution:

bmp.SetResolution(300, 300);

To fill it up your control must have the same size:

newGraph.ClientSize = bmp.Size;

Now DrawToBitmap should create an image that is crisp and fit to zoom in..

Note that it does not matter if the control is too large to fit on the screen; DrawToBitmap will still work.

Update In addidtion to a sufficient resolution it is of interest to draw quality lines etc.. A speciality of ZedGraph is that one can turn on Antialiasing, either for individual lines:

curve_x.Line.IsAntiAlias = true;

or other elements:

myPane.XAxis.Scale.FontSpec.IsAntiAlias = true;

or the whole Chart:

zedGraphControl1.IsAntiAlias = true;

All examples are taken from this post.

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Yay!!!, I think it works now, Thank you so much buddy. Just another point that I also discovered in case anybody else has the same problem. There is a function lineItem.Line.isAntiAlias which handles the pixellation from the zedgraphControl. – bchu1392 Mar 26 '16 at 03:41
  • Yes, antialiasing can be turnened on for ZedGraph diagramms. See [here](http://stackoverflow.com/questions/17287666/isantialias-creates-border-around-graph)! It seems to be available at several levels.. - If you are happy with the answer, please consider consider [accepting](http://stackoverflow.com/help/accepted-answer) it..! – TaW Mar 26 '16 at 06:54
  • I found another problem in applying this method. The print out pdf size is huge. Is there anyway we can scale the output so that the quality for 300x300dpi still apply? I only have half a page to fit this image in – bchu1392 Apr 07 '16 at 04:40
  • Hm, you need to check the numbers you get. Inserting a bitmap with 300 dpi should be honored by the pdf lib, but I'm not really familiat with it and its Scale function.. You your image is 1050, 1350 pixels? – TaW Apr 07 '16 at 05:32