新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
偷天換日讓Silverlight支持圖表轉圖片
所以實現(xiàn)圖表轉圖片可以分一下幾步走.

網(wǎng)站建設公司,為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制網(wǎng)站建設服務,專注于企業(yè)網(wǎng)站建設,高端網(wǎng)頁制作,對成都資質代辦等多個行業(yè)擁有豐富的網(wǎng)站建設經(jīng)驗的網(wǎng)站建設公司。專業(yè)網(wǎng)站設計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)seo優(yōu)化優(yōu)化,H5建站,響應式網(wǎng)站。
一.寫一份WPF客戶端程序
思路很簡單
1.1定義一個定時器,然后檢測某目錄的xml文件,這里暫定目錄名字為chart
- timer = new DispatcherTimer();
- timer.Interval = new TimeSpan(0, 0, 2);
- timer.Tick += new EventHandler(timer_Tick);
- timer.Start();
- void timer_Tick(object sender, EventArgs e)
- {
- string[] files = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "chart");
- if (files.Length > 0)
- {
- }
- }
1.2如果有的話,則進行反序列化成Chart對象進行呈現(xiàn)
- void timer_Tick(object sender, EventArgs e)
- {
- string[] files = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "chart");
- if (files.Length > 0)
- {
- LoadXml(files[0]);
- }
- }
- private void LoadXml(string xmlFile)
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(xmlFile);
- StringReader stringReader = new StringReader(doc.InnerXml);
- XmlReader xmlReader = XmlReader.Create(stringReader);
- Chart chart = XamlReader.Load(xmlReader) as Chart;
- chart.AnimationEnabled = false;
- stringReader.Close();
- xmlReader.Close();
- this.Content=chart;
- }
1.3呈現(xiàn)好以后進行截圖
- void timer_Tick(object sender, EventArgs e)
- {
- string[] files = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "chart");
- if (files.Length > 0)
- {
- LoadXml(files[0]);
- PrintPicture(files[0]);
- }
- }
- private void PrintPicture(string fileName)
- {
- this.Dispatcher.BeginInvoke(new Action(() =>
- {
- int Height = (int)this.ActualHeight;
- int Width = (int)this.ActualWidth;
- RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
- bmp.Render(this);
- string file = "C:\\temp\\a.jpg";
- BitmapEncoder encoder;
- encoder = new JpegBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bmp));
- using (Stream stm = File.Create(file))
- {
- encoder.Save(stm);
- }
- File.Delete(fileName);
- }), System.Windows.Threading.DispatcherPriority.Render);
- }
1.4轉換成圖片完畢則刪除此xml文件
二.將編譯好的wpf程序放置在web根目錄,然后啟動此程序
三.使用ajax交互將當前顯示出來的xml傳送到chart目錄下
前端
- $.ajax({
- type: "POST",
- url: "ajaxServer.aspx",
- data: "name=" + vChart.dataUri,
- success: function(msg) {
- alert("Success");
- }
- });
后端
拷貝xml文件或者其他處理方式把xml弄到chart目錄下
- protected void Page_Load(object sender, EventArgs e)
- {
- File.Copy(Server.MapPath(this.Request["name"]), Server.MapPath("../chart/" + this.Request["name"]));
- }
注意點:轉換的時候注意wpf和silverlight的命名空間.也算是一個方法,對付圖表生成圖片是綽綽有余的.小技巧分享一下
新聞名稱:偷天換日讓Silverlight支持圖表轉圖片
URL標題:http://m.5511xx.com/article/dhijhhc.html


咨詢
建站咨詢
