In C#.NET, how can I anti-alias an image object.

jinksk1957
jinksk1957 used Ask the Experts™
on
In C#.NET, how can I anti-alias an image object and copy it to the clipboard so that the target app (Word) recognizes the anti-aliasing of the lines?

I have a diagraming application that allows a user to create a polar grid diagram use to plot wind envelopes. I am using the Syncfusion Diagraming control for Windows Forms that allows you to export the diagram's content to an image object. However, there seems to be no way to preserve the anti-aliasing of the original diagram when the image object is sent through the Clipboard to Microsoft Word.

Am I doing something wrong or is this impossible? I have seen other software do this: Visio to Word, et.al.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The only way I would think you could do this is if you can stream the image in a vector-based format.  The only one I think .NET supports out of the box is WMF.  Something like:

    bmp.Save(myStream, ImageFormat.Wmf);

Otherwise, if you can access the underlying Graphics object used to draw the chart to the image, you may be able to use:

   gfx.SmoothingMode = SmoothingMode.AntiAlias;

Otherwise, you just have to create the image in as large a resolution as you dare and let the target app treat it as it chooses.  If you try to display it in an area smaller than the image data, Word should resize it nicely enough.  If you try to display it in an area larger than the image data, it will start to look blocky.

J.

Author

Commented:
jimbobmcgee;

    Your answer looks promising; however, I can't get directly at the underlying Graphics object of the Diagram control. They do provide two export (to memory) methods:

    ExportDiagramAsImage(bool respectBounds)
    ExportDiagramAsGraphics(Graphics gfx)

I can easily find the Clipboard.SetImage(Image image) method to copy as an image; however, there is no equivalent Clipboard.SetGraphics(Graphics gfx) method. Do I use Clipboard.SetDataObject() instead?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

I think (but am not too sure) that you are expected to create the Graphics object yourself, then use the ExportDiagramAsGraphics routine to draw it.  If that is the case, you may try (completely untested!):
   
using (Image img = new Bitmap(width, height)) // maybe a better constructor
using (Graphics g = Graphics.FromImage(img))  // maybe FromBitmap
{
    g.SmoothingMode = SmothingMode.AntiAlias;
    myDiagram.ExportDiagramAsGraphics(g);
    Clipboard.SetImage(img);
}

Open in new window

Otherwise, you are just going to have to use a significantly bigger width/height combo and let Word resize it down.  It will always look better sizing a large image down than a small image up.

J.

Author

Commented:
Thanks jimbob! That works great. Now I've just got to figure out how to just get what I want from their Diagram control to copy and not the whole page. Thanks again for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial