I'm new in this xml world therefore I have this xml file:
<?xml version="1.0"?>
<Graphics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Graphics>
<PropertiesGraphicsEllipse>
<Left>56</Left>
<Top>43.709333795560333</Top>
<Right>225</Right>
<Bottom>193.70933379556033</Bottom>
<LineWidth>1</LineWidth>
<ObjectColor>
<A>255</A>
<R>0</R>
<G>0</G>
<B>0</B>
<ScA>1</ScA>
<ScR>0</ScR>
<ScG>0</ScG>
<ScB>0</ScB>
</ObjectColor>
</PropertiesGraphicsEllipse>
<PropertiesGraphicsLine>
<Start>
<X>345</X>
<Y>21.709333795560333</Y>
</Start>
<End>
<X>371</X>
<Y>279.70933379556033</Y>
</End>
<LineWidth>6</LineWidth>
<ObjectColor>
<A>255</A>
<R>182</R>
<G>0</G>
<B>0</B>
<ScA>1</ScA>
<ScR>0.4677838</ScR>
<ScG>0</ScG>
<ScB>0</ScB>
</ObjectColor>
</PropertiesGraphicsLine>
<PropertiesGraphicsText>
<Text>Hola Mundo</Text>
<Left>473</Left>
<Top>109.70933379556033</Top>
<Right>649</Right>
<Bottom>218.70933379556033</Bottom>
<ObjectColor>
<A>255<开发者_如何学Python/A>
<R>21</R>
<G>208</G>
<B>0</B>
<ScA>1</ScA>
<ScR>0.007499032</ScR>
<ScG>0.630757153</ScG>
<ScB>0</ScB>
</ObjectColor>
<TextFontSize>12</TextFontSize>
<TextFontFamilyName>Verdana</TextFontFamilyName>
<TextFontStyle>Normal</TextFontStyle>
<TextFontWeight>Normal</TextFontWeight>
<TextFontStretch>Normal</TextFontStretch>
</PropertiesGraphicsText>
</Graphics>
</Graphics>
I'm trying to take this file and create a new .jpg file from this one using C# VS2008. Is it possible? Thanks in advance
Yes, this is definitely possible to do in C#. These would be the steps that I would suggest:
- Define a mapping between the graphics primitives (ellipse, line, text, etc) in the XML file and drawing commands in the
System.Drawing
namespace and see whether you find a corresponding method in theGraphics
class for each "command" in the XML file. - Write code to deserialize the XML document.
- Draw the primitives.
- Save to a JPEG image.
The code for drawing would look something like this:
// create a bitmap object with a default size
Bitmap bmp = new Bitmap(1024, 768);
// get a graphics object where we are able to draw on
Graphics g = Graphics.FromImage(bmp);
// for each PropertiesGraphicsEllipse draw an ellipse
// g.DrawEllipse(...);
// for each PropertiesGraphicsLine draw a line
// g.DrawLine(...);
// for each PropertiesGraphicsText write text
g.DrawString("Hola Mundo", new Font("Verdana", 12),
new SolidBrush(Color.FromArgb(255, 21, 208, 0)), new PointF(473F, 109.7F));
// save as JPEG
bmp.Save(@"C:\tmp\image.jpg", ImageFormat.Jpeg);
精彩评论