打印问题解决方法: 

              利用在服务端web 快照的方式生成图片,然后用户就可以利用图片工具打印甘特图。

               这样向日葵甘特图就需要一个轻量的服务端,服务端代码完全对用户开放。

               下面是C#写的生成甘特截图代码,可以运行查看效果,欢迎大家给出更多建议。

               目前存在问题是生成速度偏慢,而且较耗服务器性能,正在改进之中。

 

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;

namespace SFGantt.Services
{
    /*定义一个页面类型,用来将一个XML文件分隔输出*/
    public class GanttPrint : System.Web.UI.Page
    {

        private void Page_Load(object sender, System.EventArgs e)
        {

            try
            {
                Server.ScriptTimeout = 9000;
                System.Threading.ThreadStart start = new ThreadStart(this.test);
                System.Threading.Thread th = new System.Threading.Thread(start);
                th.SetApartmentState(System.Threading.ApartmentState.STA);//这句关键的
                th.Start();
                th.Join();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }

        private void test()
        {
            try
            {

                WebBrowser MyBrowser = new WebBrowser();

                MyBrowser.Size = new Size(3600, 2200);

                string url = "http://www.51diaodu.cn/sfgantt/examples/SFGantt/ganttprint.htm";

                MyBrowser.Navigate(url);

                while (MyBrowser.IsBusy ||   MyBrowser.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
                Bitmap myBitmap = new Bitmap(3600, 2200);
                Rectangle DrawRect = new Rectangle(0, 0, 3600, 2200);
                MyBrowser.DrawToBitmap(myBitmap, DrawRect);

                string FileName ="sfgantt"+ DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                string tmpRootDir = Server.MapPath(this.Request.ApplicationPath);//获取程序根目录

                //String FilePath = Server.MapPath(FileName);

                myBitmap.Save(tmpRootDir + FileName);
                myBitmap.Dispose();
                MyBrowser.Dispose();
                MyBrowser = null;
                Response.Write("向日葵甘特生成图片"+ FileName );
                Response.Write("<img src=\\" + FileName + " width=100% height=100%>");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


    }
}


 

posted on 2009-03-24 07:08  运筹帷幄  阅读(510)  评论(0编辑  收藏  举报