当前位置:编程学习 > C#/ASP.NET >>

如何定位到iframe然后截图iframe里头的内容并存在一定位置!!!

以下是截图的程序,怎样修改可以定位到当前界面的iframe控件然后截图iframe里头的内容并存在一定位置!!!
            
Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
R.Width = 540;
R.Height = 400;
System.Drawing.Image img = new Bitmap(R.Width, R.Height);
Graphics G = Graphics.FromImage(img);
G.CopyFromScreen(new Point(510, 160), new Point(0, 0), new Size(R.Width, R.Height));
IntPtr dc = G.GetHdc();
G.ReleaseHdc(dc);
G.Dispose();
img.Save("c:\\a.jpg");
--------------------编程问答--------------------

//网页快照
Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("这里是地址,可以取iframe的src", 600, 600, 600, 600);
MemoryStream ms = new MemoryStream();
m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可  
byte[] buff = ms.ToArray();
Response.BinaryWrite(buff);
--------------------编程问答--------------------
引用 1 楼 hrabeyond 的回复:

//网页快照
Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("这里是地址,可以取iframe的src", 600, 600, 600, 600);
MemoryStream ms = new MemoryStream();
m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可  
byte[] buff = ms.ToArray();
Response.BinaryWrite(buff);
+1 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 给你完善了下:
前端:

<iframe id="frame" runat="server" src="http://www.csdn.net" width="800" height="600"></iframe>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />


后端:

protected void Button1_Click(object sender, EventArgs e)
{
    //网页快照
    Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail(frame.Attributes["src"], 600, 600, 600, 600);
    MemoryStream ms = new MemoryStream();
    m_Bitmap.Save("c:\\1.jpg");
}
--------------------编程问答-------------------- 我帮楼上贴这个类:
//WebSiteThumbnail.cs文件,在BS项目中需要添加对System.Windows.Forms的引用
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace TestWebApp
{
    public class WebSiteThumbnail
    {
        Bitmap m_Bitmap;
        string m_Url;
        int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
        public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        {
            m_Url = Url;
            m_BrowserHeight = BrowserHeight;
            m_BrowserWidth = BrowserWidth;
            m_ThumbnailWidth = ThumbnailWidth;
            m_ThumbnailHeight = ThumbnailHeight;
        }
        public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        {
            WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
            return thumbnailGenerator.GenerateWebSiteThumbnailImage();
        }
        public Bitmap GenerateWebSiteThumbnailImage()
        {
            Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
            m_thread.SetApartmentState(ApartmentState.STA);
            m_thread.Start();
            m_thread.Join();
            return m_Bitmap;
        }
        private void _GenerateWebSiteThumbnailImage()
        {
            WebBrowser m_WebBrowser = new WebBrowser();
            m_WebBrowser.ScrollBarsEnabled = false;
            m_WebBrowser.Navigate(m_Url);
            m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
            m_WebBrowser.Dispose();
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser m_WebBrowser = (WebBrowser)sender;
            m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
            m_WebBrowser.ScrollBarsEnabled = false;
            m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
            m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
        }
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,