Why when using a timer and inside using Bitmaps it's slowing everything ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
If i just run my application and drag the Form around the screen its moving regulary fast smooth .
Once i click the button its moving very slow and also stuttering .
This is the code in Form1 :using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScreenShotDemo;
using System.Drawing.Imaging;
using System.IO;
using System.Diagnostics;
using DannyGeneral;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using Utilities;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Xml.Serialization;
using System.Threading;


namespace ScreenVideoRecorder
{
public partial class Form1 : Form
{
bool clicked;
MagnifierMainForm mf;
MagnifierForm magnifierform;
ScreenCapture sc;
Bitmap bitmap;
Ffmpeg ffmp;
bool controlDown = false;
private globalKeyboardHook gkh = new globalKeyboardHook();
private MouseHookListener m_MouseHookManager;

public Form1()
{
InitializeComponent();



ffmp = new Ffmpeg();
sc = new ScreenCapture();

}

Then the button click event :private void StartRecording_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

And the timer1 tick event :private void timer1_Tick(object sender, EventArgs e)
{
using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
{
ffmp.PushFrame(bitmap);
}
}

Now i tried each time to mark unuse // parts of the code and found that the problem is with this code :using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
{
}

The timer is set to 40ms interval . The ScreenCapture class take a screenshot of the entire screen .
This is the ScreenCapture class :using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ScreenShotDemo
{
public class ScreenCapture
{
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINTAPI ptScreenPos;
}

[StructLayout(LayoutKind.Sequential)]
struct POINTAPI
{
public int x;
public int y;
}

[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);

[DllImport("user32.dll")]
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);

const Int32 CURSOR_SHOWING = 0x00000001;

public static Bitmap CaptureScreen(bool CaptureMouse)
{
Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

try
{
using (Graphics g = Graphics.FromImage(result))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

if (CaptureMouse)
{
CURSORINFO pci;
pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));

if (GetCursorInfo(out pci))
{
if (pci.flags == CURSOR_SHOWING)
{
DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
g.ReleaseHdc();
}
}
}
}
}
catch
{
result = null;
}

return result;
}
}
}

I used this :using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))

The question is if there is any other way to do it in the timr tick event ?
Maybe the problem is with the ScreenCapture class code ?
But for sure this part of the code make everything very slow .

View the full article
 
Back
Top