首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

获取跳转网址有关问题,100分有关问题未解决

2013-04-20 
获取跳转网址问题,100分问题未解决100分的问题:http://bbs.csdn.net/topics/390426448这个问题看似简单,实

获取跳转网址问题,100分问题未解决
100分的问题:
http://bbs.csdn.net/topics/390426448

这个问题看似简单,实际上不简单(至少对我这个菜鸟来说很难)。
请求大侠们把实现代码贴出来,谢谢。

两边都回复下,我好给分获取跳转网址有关问题,100分有关问题未解决
[解决办法]
可以结贴给分了


    public class GHtmlToImage
    {
        /// <summary>
        /// 地址
        /// </summary>
        private string _url = "about:blank";
        /// <summary>
        /// 宽
        /// </summary>
        private int _width = 800;
        /// <summary>
        /// 高
        /// </summary>
        private int _height = 600;
        /// <summary>
        /// 缩略图宽
        /// </summary>
        private int _swidth = 320;
        /// <summary>
        /// 缩略图高
        /// </summary>
        private int _sheight = 250;

        private Bitmap _image;
        private Bitmap _thumbnailimage;
        private object _lock = new object();
        private object _lock2 = new object();
        private string _charset = "utf-8";
        public string Url
        {
            set { _url = value; }
        }
        public int Width
        {
            set { _width = value; }
        }
        public int Height
        {
            set { _height = value; }
        }
        public int SWidth
        {
            set { _swidth = value; }
        }
        public int SHeight
        {


            set { _sheight = value; }
        }
        public Bitmap Image
        {
            get { return _image; }
        }
        public Bitmap ThumbnailImage
        {
            get { return _thumbnailimage; }
        }
        public string DocText
        {
            get;
            set;
        }
        public int runtime
        {
            get;
            set;
        }
        public GHtmlToImage()
        {
            Thread.CurrentThread.CurrentUICulture =  Thread.CurrentThread.CurrentCulture;
        }
        public string Charset
        {
            set
            {
                _charset = value; ;
            }
        }
        /*
        [STAThreadAttribute]
        static void Main()
        {
            
        }
         */ 
        public void Init()
        {
            
            lock (_lock)
            {
                
                Thread _thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
                _thread.Name = "Galsun.Fcx.htmlToImage";
                _thread.SetApartmentState(ApartmentState.STA);
                _thread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;


                _thread.Start();
                _thread.Priority = ThreadPriority.Highest;
                TimeSpan ts = new TimeSpan(0, 0, 0, 30);
                _thread.Join();
               
                GC.Collect();
                GC.Collect();
            }
        }
        [STAThread]
        private void _GenerateHtmlToImgImage()
        {
            
            Monitor.Enter(this);
            try
            {
                #region 普通
                WebBrowser _WebBrowser = new WebBrowser();
                _WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                //_WebBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(WebBrowser_ProgressChanged);
                _WebBrowser.ScrollBarsEnabled = false;
                
                _WebBrowser.Navigate(_url);
                System.Windows.Forms.Timer _tm = new System.Windows.Forms.Timer();
                _tm.Interval=1000;
                _tm.Tick += new EventHandler(Time_Tick);
                _tm.Enabled = true;
                //Application.SetCompatibleTextRenderingDefault(false);
                Application.Run();
                //Application.EnableVisualStyles();
                
                
                _WebBrowser.Dispose();
                


                #endregion

                #region 新模式
                /*
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainForm _form = new MainForm();
                _form._url = _url;
                _form._width = _width;
                _form._height = _height;
                _form._swidth = _swidth;
                _form._sheight = _sheight;
                _form.Width = _width;
                _form.Height = _height;
                Application.Run(_form);
                _image = _form._image;
                _thumbnailimage = _form._thumbnailimage;
                 */ 
                #endregion
            }
            finally
            {
                Monitor.Exit(this);

            }
        }
        private void Time_Tick(object sender, EventArgs e)
        {
            if (runtime > 0)
            {
                Thread.Sleep(100);
                Application.ExitThread();
            }
        }
        private void WebBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            WebBrowser _webBrowser = (WebBrowser)sender;
            if (e.CurrentProgress == e.MaximumProgress && _webBrowser.ReadyState == WebBrowserReadyState.Complete)


            {
                Application.ExitThread();
            }
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Monitor.Enter(this);
            try
            {
                
                WebBrowser _webBrowser = (WebBrowser)sender;
                
                if (_webBrowser.ReadyState == WebBrowserReadyState.Complete)
                {
                    //_webBrowser.Document.Encoding = "utf-8";
                     //_charset = _webBrowser.Document.Encoding;
                     //DocText = _webBrowser.DocumentText;
                     //DocText = Encoding.GetEncoding("utf-8").GetString(Encoding.GetEncoding("gbk").GetBytes(_webBrowser.DocumentText));
                    //Content.GB2312UnicodeConverter.ToUnicode(DocText);
                    StreamReader sr;
                    sr = new StreamReader(_webBrowser.DocumentStream, Encoding.GetEncoding(_charset));
                    DocText  = sr.ReadToEnd();
                    sr.Close();
                    if(_webBrowser.Document.Body.ScrollRectangle.Height> _height)
                    {
                        _height=_webBrowser.Document.Body.ScrollRectangle.Height;
                    }
                    _webBrowser.ClientSize = new Size(_width, _height);
                    //_webBrowser.ScrollBarsEnabled = false;


                    //Application.DoEvents();
                    _image = new Bitmap(_webBrowser.Bounds.Width, _webBrowser.Bounds.Height);
                    _webBrowser.BringToFront();
                    _webBrowser.DrawToBitmap(_image, _webBrowser.Bounds);
                    _thumbnailimage = (Bitmap)_image.GetThumbnailImage(_swidth, _sheight, null, IntPtr.Zero);
                    runtime++;
                }
                
            }
            finally
            {
                Monitor.Exit(this);
            }
        }

    }



string url = "http://s.click.taobao.com/t?e=zGU34CA7K%2BPkqB07S4%2FK0CFcRfH0GoT805sipKj3WFgObCOjeMxxCTFxN6Vm7j4ewiKkN9jps1h0xwpsosUSBTJl%2Bsn9DQ1mMZl%2BUXg0Gj6BRg%3D%3D";
                GHtmlToImage _html = new GHtmlToImage();
                _html.Url = url;
                _html.Width = 1280;
                _html.Height = 800;
                _html.Charset = "gbk";
                _html.Init();

                Response.Write(Server.HtmlEncode(_html.DocText));

                Response.End();

热点排行