|
|
|
C# ถามเรื่องการ capture หน้าจอครับ เวลารันผ่าน server แล้วมัน capture ได้หน้าดำ |
|
|
|
|
|
|
|
Code (C#)
public class GD
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parent /*HWND*/, IntPtr next /*HWND*/, string sClassName, IntPtr sWindowTitle);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.Dll")]
public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
public const int GW_CHILD = 5;
public const int GW_HWNDNEXT = 2;
public void CapDisplay()
{
int heightsize;
int widthsize;
int screenHeight;
int screenWidth;
bool chkWriteURL = true, chkShowGuides = false, IsDTDDocument = true;
Cursor.Current = Cursors.WaitCursor;
SHDocVw.WebBrowser m_browser = null;
SHDocVw.ShellWindows shellWindows = shellWindows = new SHDocVw.ShellWindowsClass();
//Find first availble browser window.
//Application can easily be modified to loop through and capture all open windows.
string filename;
foreach (SHDocVw.WebBrowser browser in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(browser.FullName).ToLower();
if (filename.Equals("iexplore"))
{
m_browser = browser;
break;
}
if (filename.Equals("firefox"))
{
m_browser = browser;
break;
}
}
if (m_browser == null)
{
MessageBox.Show("No Browser Open");
return;
}
//Assign Browser Document
mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_browser.Document;
mshtml.IHTMLDocument3 doc3 = (mshtml.IHTMLDocument3)myDoc;
//URL Location
string myLocalLink = myDoc.url;
int URLExtraHeight = 0;
int URLExtraLeft = 0;
//Adjustment variable for capture size.
//if (chkWriteURL.Checked == true)
// URLExtraHeight = 25;
//TrimHeight and TrimLeft trims off some captured IE graphics.
int trimHeight = 3;
int trimLeft = 3;
//Use UrlExtra height to carry trimHeight.
URLExtraHeight = URLExtraHeight - trimHeight;
URLExtraLeft = URLExtraLeft - trimLeft;
if (!IsDTDDocument)
{
myDoc.body.setAttribute("scroll", "yes", 0);
//Get Browser Window Height
heightsize = (int)myDoc.body.getAttribute("scrollHeight", 0);
widthsize = (int)myDoc.body.getAttribute("scrollWidth", 0);
//Get Screen Height
screenHeight = (int)myDoc.body.getAttribute("clientHeight", 0);
screenWidth = (int)myDoc.body.getAttribute("clientWidth", 0);
}
else
{
doc3.documentElement.setAttribute("scroll", "Yes", 0);
//Get Browser Window Height
heightsize = (int)doc3.documentElement.getAttribute("scrollHeight", 0);
widthsize = (int)doc3.documentElement.getAttribute("scrollWidth", 0);
//Get Screen Height
screenHeight = (int)doc3.documentElement.getAttribute("clientHeight", 0);
screenWidth = (int)doc3.documentElement.getAttribute("clientWidth", 0);
}
//Get bitmap to hold screen fragment.
Bitmap bm = new Bitmap(screenWidth, screenHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
//Create a target bitmap to draw into.
Bitmap bm2 = new Bitmap(widthsize + URLExtraLeft, heightsize + URLExtraHeight - trimHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics g2 = Graphics.FromImage(bm2);
Graphics g = null;
IntPtr hdc;
System.Drawing.Image screenfrag = null;
int brwTop = 0;
int brwLeft = 0;
int myPage = 0;
IntPtr myIntptr = (IntPtr)m_browser.HWND;
//Get inner browser window.
int hwndInt = myIntptr.ToInt32();
IntPtr hwnd = myIntptr;
hwnd = GetWindow(hwnd, GW_CHILD);
StringBuilder sbc = new StringBuilder(256);
//Get Browser "Document" Handle
while (hwndInt != 0)
{
hwndInt = hwnd.ToInt32();
GetClassName(hwndInt, sbc, 256);
if (sbc.ToString().IndexOf("Shell DocObject View", 0) > -1) //IE6
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
break;
}
if (sbc.ToString().IndexOf("TabWindowClass", 0) > -1) //IE7
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
break;
}
if (sbc.ToString().IndexOf("Frame Tab", 0) > -1) // IE8
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "TabWindowClass", IntPtr.Zero);
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
break;
}
hwnd = GetWindow(hwnd, GW_HWNDNEXT);
}
//Get Screen Height (for bottom up screen drawing)
while ((myPage * screenHeight) < heightsize)
{
if (!IsDTDDocument)
myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0);
else
doc3.documentElement.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0);
++myPage;
}
//Rollback the page count by one
--myPage;
int myPageWidth = 0;
while ((myPageWidth * screenWidth) < widthsize)
{
if (!IsDTDDocument)
myDoc.body.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0);
else
doc3.documentElement.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0);
if (!IsDTDDocument)
brwLeft = (int)myDoc.body.getAttribute("scrollLeft", 0);
else
brwLeft = (int)doc3.documentElement.getAttribute("scrollLeft", 0);
for (int i = myPage; i >= 0; --i)
{
//Shoot visible window
g = Graphics.FromImage(bm);
hdc = g.GetHdc();
if (!IsDTDDocument)
myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * i, 0);
else
doc3.documentElement.setAttribute("scrollTop", (screenHeight - 5) * i, 0);
if (!IsDTDDocument)
brwTop = (int)myDoc.body.getAttribute("scrollTop", 0);
else
brwTop = (int)doc3.documentElement.getAttribute("scrollTop", 0);
PrintWindow(hwnd, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
screenfrag = System.Drawing.Image.FromHbitmap(bm.GetHbitmap());
g2.DrawImage(screenfrag, brwLeft + URLExtraLeft, brwTop + URLExtraHeight);
}
++myPageWidth;
}
//Draw Standard Resolution Guides
if (chkShowGuides == true)
{
// Create pen.
int myWidth = 1;
Pen myPen = new Pen(Color.Navy, myWidth);
Pen myShadowPen = new Pen(Color.NavajoWhite, myWidth);
// Create coordinates of points that define line.
float x1 = -(float)myWidth - 1 + URLExtraLeft;
float y1 = -(float)myWidth - 1 + URLExtraHeight;
float x600 = 600.0F + (float)myWidth + 1;
float y480 = 480.0F + (float)myWidth + 1;
float x2 = 800.0F + (float)myWidth + 1;
float y2 = 600.0F + (float)myWidth + 1;
float x3 = 1024.0F + (float)myWidth + 1;
float y3 = 768.0F + (float)myWidth + 1;
float x1280 = 1280.0F + (float)myWidth + 1;
float y1024 = 1024.0F + (float)myWidth + 1;
// Draw line to screen.
g2.DrawRectangle(myPen, x1, y1, x600 + myWidth, y480 + myWidth);
g2.DrawRectangle(myPen, x1, y1, x2 + myWidth, y2 + myWidth);
g2.DrawRectangle(myPen, x1, y1, x3 + myWidth, y3 + myWidth);
g2.DrawRectangle(myPen, x1, y1, x1280 + myWidth, y1024 + myWidth);
// Create font and brush.
Font drawFont = new Font("Arial", 12);
SolidBrush drawBrush = new SolidBrush(Color.Navy);
SolidBrush drawBrush2 = new SolidBrush(Color.NavajoWhite);
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.FitBlackBox;
// Draw string to screen.
g2.DrawString("600 x 480", drawFont, drawBrush, 5, y480 - 20 + URLExtraHeight, drawFormat);
g2.DrawString("800 x 600", drawFont, drawBrush, 5, y2 - 20 + URLExtraHeight, drawFormat);
g2.DrawString("1024 x 768", drawFont, drawBrush, 5, y3 - 20 + URLExtraHeight, drawFormat);
g2.DrawString("1280 x 1024", drawFont, drawBrush, 5, y1024 - 20 + URLExtraHeight, drawFormat);
}
//Write URL
if (chkWriteURL == true)
{ //Backfill URL paint location
SolidBrush whiteBrush = new SolidBrush(Color.White);
Rectangle fillRect = new Rectangle(0, 0, widthsize, URLExtraHeight + 2);
Region fillRegion = new Region(fillRect);
g2.FillRegion(whiteBrush, fillRegion);
SolidBrush drawBrushURL = new SolidBrush(Color.Black);
Font drawFont = new Font("Arial", 12);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.FitBlackBox;
g2.DrawString(myLocalLink, drawFont, drawBrushURL, 0, 0, drawFormat);
}
//Reduce Resolution Size
double myResolution = Convert.ToDouble(1) * 0.01;
int finalWidth = (int)((widthsize + URLExtraLeft));
int finalHeight = (int)((heightsize + URLExtraHeight));
Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics gFinal = Graphics.FromImage((System.Drawing.Image)finalImage);
gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight);
//Get Time Stamp
DateTime myTime = DateTime.Now;
String format = "MM.dd.hh.mm.ss";
//Create Directory to save image to.
Directory.CreateDirectory("C:\\IECapture");
//Write Image.
EncoderParameters eps = new EncoderParameters(1);
long myQuality = Convert.ToInt64(200);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, myQuality);
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
finalImage.Save(@"c:\\IECapture\Captured_" + myTime.ToString(format) + ".jpg", ici, eps);
//Clean Up.
myDoc = null;
g.Dispose();
g2.Dispose();
gFinal.Dispose();
bm.Dispose();
bm2.Dispose();
finalImage.Dispose();
Cursor.Current = Cursors.Default;
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
private void lnkOpenCapture_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
Process.Start("explorer.exe", "C:\\IECapture");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
GD g = new GD();
g.CapDisplay();
}
คือผมสร้างในเครื่องของผมแล้วอัพขึ้น wcf แล้วให้เครื่องอื่นเอาลิ้งไปเปิดแล้ว capture หน้าจอ ผลที่ได้หน้าจอที่ได้เป็นสีดำ พอรันในเครื่องตัวเองมันก็ capture ได้ไม่มีปัญหาอะไร อยากทราบว่าจะแก้ไขปัญหายังไงครับ
Tag : - - - -
|
|
|
|
|
|
Date :
2010-04-26 13:26:57 |
By :
sailomwind |
View :
2051 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดันนนนนนนนนนน ไม่มีใครตอบเลย T_T
|
|
|
|
|
Date :
2010-04-26 13:44:21 |
By :
sailomwind |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|