2010-11-09 85 views
0

我在這個新的XML世界,因此我有這個XML文件:XML文件爲JPG文件

<?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</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> 

我想利用這個文件,並使用C#創建這個新的.jpg文件VS2008。可能嗎? 在此先感謝

回答

2

是的,這絕對是可以在C#中完成的。這將是我建議的步驟:

  1. 定義XML文件中的基本圖形(橢圓,直線,文本等)和繪圖命令之間的映射在System.Drawing命名空間,看看你是否找到了對應的方法在Graphics類中爲每個「命令」在XML文件中。
  2. 編寫代碼以反序列化XML文檔。
  3. 繪製原語。
  4. 保存爲JPEG圖像。

繪製代碼會是這個樣子:

// 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); 
+0

感謝0xA3執行我能夠創建JPG文件:) – George 2010-11-10 18:34:03