Charts (Part II)
All right, as promised, hereâs the code that gets rid of the jagged edges of the chart we created using Office Web Components. It's hackish, so if you're against that sort of thing, run away.
What we do is basically get the chart as a very large image (1500Ã1500, for example) and then create a high-resolution thumbnail out of it (300Ã300, for example) via GDI+. The over-caffeinated reader would wonder why we bothered with office web components in the first place; why didnât we simply use GDI+ to create the chart? Thereâs no good reason, really, other than that I wanted to tinker with office web components, which Iâve never used before. ;-) So, without further ado, hereâs the code:
//Get the picture
byte[] imageBuffer =
(byte[])chartSpace.
GetPicture(âgifâ, 1500, 1500);
MemoryStream ms =
new
MemoryStream(imageBuffer);
Bitmap original =
(Bitmap)Bitmap.FromStream
(ms, true, true);
//Create the thumbnail
Bitmap thumbnail =
new Bitmap(300, 300);
Graphics g = Graphics.
FromImage(thumbnail);
g.InterpolationMode =
InterpolationMode.HighQualityBilinear;
g.DrawImage(
original,
new Rectangle
(0, 0, 300, 300),
0,
0,
1500,
1500,
GraphicsUnit.Pixel);
g.Dispose();
//Send the image to
the browser
Response.
ContentType = âimage/gifâ;
thumbnail.Save
(Response.OutputStream, ImageFormat.Jpeg);
Pretty self-explanatory, so I wonât bore you with an explanation.