PDA

View Full Version : [C#] Screenshot continuo consumo cpu alta!!


kulosia
15-09-2011, 04:13
Salve, ho un problema con questa parte di 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()


ogni volta che compilo e avvio l'eseguibile la cpu con un amd 965 consuma 20% di cpu!! Percentuale troppo alta.. infatti se lo uso sul portatile con intel celeron da 20% arriva a 100%.. l'errore è in questa parte di codice in qundo cattura le immagini del desktop, e per vedere se effettivamente è qui il problema ho tolto pobjThread_Capture.Start() e infatti da 20% si riduce a 0% "Che differenza.." qualcuno può aiutarmi a modificare questa parte di codice per capire come fare ad usare meno cpu??

aspetto risposte vi prego è urgente stò realizzando un progetto!!

BrutalBass
15-09-2011, 12:34
Salve, ho un problema con questa parte di 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()


ogni volta che compilo e avvio l'eseguibile la cpu con un amd 965 consuma 20% di cpu!! Percentuale troppo alta.. infatti se lo uso sul portatile con intel celeron da 20% arriva a 100%.. l'errore è in questa parte di codice in qundo cattura le immagini del desktop, e per vedere se effettivamente è qui il problema ho tolto pobjThread_Capture.Start() e infatti da 20% si riduce a 0% "Che differenza.." qualcuno può aiutarmi a modificare questa parte di codice per capire come fare ad usare meno cpu??

aspetto risposte vi prego è urgente stò realizzando un progetto!!

Quello è VB:NET non C#

comunque sia, segui questo tutorial:

http://www.youtube.com/watch?v=Z-P874sfzTI

il tuo codice è proprio brutto...

kulosia
15-09-2011, 13:41
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??

gugoXX
21-09-2011, 22:19
In C# vai con questo.


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);
}
}
}
}