简体   繁体   中英

How can I generate paths in .png files using C#?

I need to parse an XML file of coordinates and create a .png (from scratch) in which I draw paths between the coordinates. I also need to be able to smooth the corners when paths change direction (maybe using beziers).

How can I do this programmatically in C#?

Thanks

C# doesn't have any notion of graphics—you'll need a choose a vector graphics library to do the work for you. Since you're already in .NET, I'd suggest WPF to construct the image, and the Bitmap classes to export the final result as a PNG.

You can find a great introduction to WPF vector graphics here .

You can paint into a bitmap using the Graphics object and DrawBezier method: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawbezier.aspx

And then use the Save method to store it as png.

I can't paste you code right now because I don't have a dev environment to make a sample, but it should be something like:

  1. Create a Bitmap with the size you need:

     Bitmap bitmap = new Bitmap(width,height); 
  2. Get a Graphics object from your bitmap :

     Graphics graphics= Graphics.FromImage(bitmap); 
  3. Use the graphics object to draw (with DrawBezier if it's your case)

     graphics.DrawBezier(pen, a,b,c); 
  4. Call to Save method indicating png format:

     bitmap.Save(path,ImageFormat.Png); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM