|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Jan 2010
Messaggi: 149
|
[C#] Screenshot continuo consumo cpu alta!!
Salve, ho un problema con questa parte di codice:
Codice:
Try pobjCapture = New RDC2_Library.Capture.cDirectX() Catch ex As Exception pobjCapture = New RDC2_Library.Capture.cGDI() End Try pobjCapture.GetSnapshot() pintState_Size = pobjCapture.Length pintState_Buffer = Runtime.InteropServices.Marshal.AllocHGlobal(pintState_Size) pobjCapture.ReleaseSnapshot() pobjThread_Capture = New Threading.Thread(AddressOf Thread_Capture) pobjThread_Capture.IsBackground = True pobjThread_Capture.Start() aspetto risposte vi prego è urgente stò realizzando un progetto!! |
![]() |
![]() |
![]() |
#2 | |
Junior Member
Iscritto dal: Jul 2011
Messaggi: 14
|
Quote:
comunque sia, segui questo tutorial: http://www.youtube.com/watch?v=Z-P874sfzTI il tuo codice è proprio brutto... |
|
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Jan 2010
Messaggi: 149
|
Si hai pienamente ragione è vb:net scusami, ieri non ci avevo proprio fatto caso ero pienamente consapevole che era vb:net ma ho scritto c# perchè cercavo sui motori di ricerca alcune cose in c#.. comunque ho visto il video e ho provato in quest'altro modo, con processore amd 965 dal codice di prima arrivava a 20% ora a 9-10%.. quindi penso che su un processore intel celeron consumi intorno ai 50-60% che sono ancora alti!! penso che il problema sia proprio lo screen continuo.. perchè in pratica uso lo screen continuo arrivando al punto da diventare come un video e vedere il desktop a distanza con un client e un server.. se il problema è proprio lo screen continuo c'è un'altro metodo??
ho provato ad usare tipo programmi professionali come teamviewer e non consumano nulla! cosa usano loro per non arrivare a percentuali di cpu alte?? |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: May 2004
Città: Londra (Torino)
Messaggi: 3692
|
In C# vai con questo.
Codice:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; namespace SCreenCapture { class Program { static void Main(string[] args) { ScreenCapture sc = new ScreenCapture(); sc.CaptureScreenToFile("Prova.tiff", ImageFormat.Tiff); } public class ScreenCapture { /// <summary> /// Creates an Image object containing a screen shot of the entire desktop /// </summary> /// <returns></returns> public Image CaptureScreen() { return CaptureWindow(User32.GetDesktopWindow()); } /// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> public Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return img; } /// <summary> /// Captures a screen shot of a specific window, and saves it to a file /// </summary> /// <param name="handle"></param> /// <param name="filename"></param> /// <param name="format"></param> public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) { Image img = CaptureWindow(handle); img.Save(filename, format); } /// <summary> /// Captures a screen shot of the entire desktop, and saves it to a file /// </summary> /// <param name="filename"></param> /// <param name="format"></param> public void CaptureScreenToFile(string filename, ImageFormat format) { Image img = CaptureScreen(); img.Save(filename, format); } /// <summary> /// Helper class containing Gdi32 API functions /// </summary> private class GDI32 { public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); } /// <summary> /// Helper class containing User32 API functions /// </summary> private class User32 { [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; } [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("user32.dll")] public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); } } } }
__________________
Se pensi che il tuo codice sia troppo complesso da capire senza commenti, e' segno che molto probabilmente il tuo codice e' semplicemente mal scritto. E se pensi di avere bisogno di un nuovo commento, significa che ti manca almeno un test. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:56.