C # plot a vector chart (Metafile, WMF, EMF)

Source: Internet
Author: User

The main function is to use C # To draw a vector graph, export it to Word, Excel, and PowerPoint, and edit it again. The solution is as follows:

First, you should determine the format that can be used to edit images in the office document. after learning the relevant materials, we learned that office documents mainly support three vector graph formats: 1. VML graphics, 2. WMF file format. 3. EMF file format. Because VML diagrams are generally described in HTML language and are not easy to operate, the WMF and EMF file formats are used.

The next step is to let C # generate a WMF or EMF vector Image Based on the provided data; learn the GDI + drawing class library provided by C #, And the GDI + can draw a vector image, the sample code is as follows:
/// <Summary>
/// Export as an EMF or WMF File
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Returns> Successful </returns>
Private bool Export (string filepath)
{
Try
{
Bitmap BMP = new Bitmap (220,220 );
Graphics GS = graphics. fromimage (BMP );
Metafile mf = new Metafile (filepath, Gs. gethdc ());

Graphics G = graphics. fromimage (MF );

Draw (g );

G. Save ();
G. Dispose ();
Mf. Dispose ();

Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Draw the image
/// </Summary>
/// <Param name = "G"> graphics object used for drawing </param>
Private void draw (Graphics g)
{
Hatchbrush HB = new hatchbrush (hatchstyle. lightupwarddiagonal, color. Black, color. White );

G. fillellipse (brushes. Gray, 10f, 10f, 200,200 );
G. drawellipse (new pen (color. Black, 1f), 10 F, 10 F, 200,200 );

G. fillellipse (Hb, 30f, 95f, 30,30 );
G. drawellipse (new pen (color. Black, 1f), 30f, 95f, 30,30 );

G. fillellipse (Hb, 160f, 95f, 30,30 );
G. drawellipse (new pen (color. Black, 1f), 160f, 95f, 30,30 );

G. fillellipse (Hb, 95f, 30f, 30,30 );
G. drawellipse (new pen (color. Black, 1f), 95f, 30f, 30,30 );

G. fillellipse (Hb, 95f, 160f, 30,30 );
G. drawellipse (new pen (color. Black, 1f), 95f, 160f, 30,30 );

G. fillellipse (brushes. Blue, 60f, 60f, 100,100 );
G. drawellipse (new pen (color. Black, 1f), 60f, 60f, 100,100 );

G. fillellipse (brushes. blanchedalmond, 95f, 95f, 30,30 );
G. drawellipse (new pen (color. Black, 1f), 95f, 95f, 30,30 );

G. drawrectangle (new pen (system. Drawing. Brushes. Blue, 0.1f), 6, 208,208 );

G. drawline (new pen (color. Black, 0.1f), 110f, 110f, 220f, 25f );
G. drawstring ("Profile", new font ("", 9f), brushes. Green, 220f, 20f );
}

The generated image is as follows:

After amplification:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.