Embed in word

M

MRe

Hi,

Is there any way to write a graphical program in CSharp that could be
embedded into Word2000. I've read .NET doesn't support creation of ActiveX
controls (unless done using C++) but is there any other option, like an
existing ActiveX that can embed some CSharp program.

What I'd like to do is; whenever I'm writing an assignment for college, I
usually want to add some diagram that isn't easy to create with the tools
available to me, so I write a program to do it, take a snapshot, and put it
into the doc as a bitmap. However, when printing, the bitmap comes out
blurry, so if I could embed the program and send vector data to the printer,
that would be awesome!

Thanks for any help,
Kind regards,
Eliott
 
L

Lebesgue

You actually don't need to embed anything into word to accomplish the task
you have described. Just draw your graphics as vectors instead of bitmap and
no blurring will ocurr.

It's possible with GDI, but you'll need to apply a little hack:

using (Graphics g = this.CreateGraphics()) //or Graphics from your paint
event
{
IntPtr dc = g.GetHdc();

using (Metafile metafile = new Metafile("foo.wmf", dc)) //your vector image
will be stored as foo.wmf
{
using (Graphics graphics = Graphics.FromImage(metafile))
{
graphics.DrawEllipse(Pens.Red, new Rectangle(0, 0, 50, 50));
//do your drawing here

//metafile.Save("boo.wmf", ImageFormat.Wmf); //I first guessed this
should allow you to save the image but this would throw a "generic error"
from GDI, so "foo.wmf" will be the only place where the image gets saved
}
}
}

Just insert the result (foo.wmf) into word as any other image - but you can
scale this one without any blur.

This is the only way I've found that seems to work. Plain image.Save(name,
ImageFormats.Wmf) saves it as png, since there is no wmf encoder in GDI+.
This seems a little too hacky for me, but it works. If there's anyone who
knows how to save images as wmf in .NET in a "more straightforward way" I'd
be very glad to hear from him.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top