2011-12-12 39 views
0

我試圖導出MSChart的圖像。從基於MSChart的應用程序導出引發矢量格式的例外

我發現了精彩的功能

[System.Windows.Forms.DataVisualization.Charting.Chart.SaveImage][1] 

但是我遇到的的imageformat參數的幾個問題。具體而言,我希望用戶能夠以任何格式保存圖像,其格式爲WIC編碼器。爲此,我得到了圖像編解碼器的列表,並將它們全部提供給用戶。

因此,當用戶選擇他們的文件時,我抓住擴展名並在列表中找到與擴展名匹配的編解碼器。然後,我創建相關的imageformat如下:

ImageFormat imgFmt = new ImageFormat(codec.FormatID); 

我然後調用下面的圖表導出:

exportChart.SaveImage(mSaveFileDialog.FileName, imgFmt); 

然而,這會拋出一個異常,如果我選擇的EMF或WMF,如下所示:

A first chance exception of type 'System.ArgumentNullException' occurred in System.Drawing.dll 
An unhandled exception of type 'System.ArgumentNullException' occurred in System.Drawing.dll 

Additional information: Value cannot be null. 

最奇怪的事情是,如果我創造我的圖片格式如下:

ImageFormat imgFmt = ImageFormat.Emf; 

然後它寫一個EMF沒有問題。此外,如果我這樣做(與原始imgFmt):

imgFmt.Equals(ImageFormat.Emf) 

然後它返回true,但仍然引發異常。

有沒有人知道爲什麼會發生這種情況並解決我的問題?

回答

1

我認爲內部圖表控件使用您提供的ImageFormat來選擇ImageCodecInfo。由於無法找到所述編解碼器信息而引發此異常。

ImageFormat fmt = ImageFormat.Emf; 
ImageFormat fmt2 = new ImageFormat(ImageFormat.Emf.Guid); 
Console.WriteLine(fmt.ToString()); // gives: Emf 
Console.WriteLine(fmt2.ToString()); // gives: [ImageFormat: b96b3cac-0728-11d3-9d7b-0000f81ef32e] 

如果您致電兩種格式的ToString(),你會看到,他們不回同樣的事情。選擇編碼器的代碼很可能不會預期從GUID構建的ImageFormat,只是查找已知格式名稱的列表。

縱觀mschart樣本,此函數的首選參數類型是枚舉ChartImageFormat,而不是Imageformat。

這並不能解決您的問題,但希望使它不那麼神祕。

+0

錯誤。我希望能夠導出到SVG ...看起來我需要一個EMF到SVG轉換器... – Goz

相關問題