0

I am creating desktop recording (screen recording) application using Directshow.NET and C#. I am almost done, application is able to record desktop screen. To paint mouse pointer in recording video I have implemented BufferCB from SampleGrabber and with the help of my another post Fliped cursor icon on desktop recording using directshow i am able to paint mouse pointer in correct orientation

Here is my code of BufferCB :

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags =
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)] 
int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
    if (!wait)
    {
        wait = true;
        Rectangle imageBounds = new Rectangle(0, 0, m_videoWidth, m_videoHeight);
        Bitmap bitmap = new Bitmap(m_videoWidth, m_videoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(imageBounds, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
        IntPtr ptr = bitmapData.Scan0;
        bitmap.UnlockBits(bitmapData);
        CopyMemory(ptr, pBuffer, (uint)BufferLen);
        bitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            CURSORINFO cursorInfo;
            cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));

            if (GetCursorInfo(out cursorInfo) && cursorInfo.flags == CURSOR_SHOWING)
            {                        
                IntPtr iconPointer = CopyIcon(cursorInfo.hCursor);
                ICONINFO iconInfo;
                int iconX, iconY;
                if (GetIconInfo(iconPointer, out iconInfo))
                {
                    // calculate the correct position of the cursor
                    iconX = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
                    iconY = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);

                    //GETTING ARGUMENTEXCEPTION AT BELOW LINE                                
                    IntPtr hdc = g.GetHdc();
                    DrawIcon(hdc, iconX, iconY, cursorInfo.hCursor);                                
                    g.ReleaseHdc(hdc);                                
                }                        
            }
            g.DrawImage(companylogo, m_videoWidth - 100 , 20);
        }
        bitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
        bitmapData = bitmap.LockBits(imageBounds, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
        ptr = bitmapData.Scan0;
        bitmap.UnlockBits(bitmapData);                
        CopyMemory(pBuffer, ptr, (uint)BufferLen);
        bitmap.Dispose();
        wait = false;
    }
    return 0;
}

Mouse pointer is getting paint on video but after some time of recording I am getting ArgumentException "Parameter is not valid." at line of code IntPtr hdc = g.GetHdc();

Can anyone help me put to solve this?

StackTrace:
at System.Drawing.Graphics.GetHdc()

Community
  • 1
  • 1
Amogh
  • 4,453
  • 11
  • 45
  • 106
  • Memory leak leading to a point where getting another DC fails? – Roman R. Mar 25 '16 at 10:19
  • 1
    See [this](http://stackoverflow.com/questions/3818440/parameter-is-not-valid-thrown-from-system-drawing-graphics-gethdc-only-on) question. – Jeroen Heier Mar 25 '16 at 10:36
  • @RomanR. why memory leak is happening, as I am using using block which will take care of disposing Graphics object. – Amogh Mar 25 '16 at 10:55
  • @Jeroen I checked that question and link given in answer. I am disposing `bitmap` and I am using `using` block which will take care of disposing `Graphics g`, what else should I do? – Amogh Mar 25 '16 at 11:07
  • Jeroen's link is most likely explaining the leak you have even in better detail. Your managed code is okay, but it internally manages native code resources which are released not on time or with a related issue. You should look into reusing resources rather than doing allocation/releases infinitely. – Roman R. Mar 25 '16 at 11:20
  • Thanks @Jeroen link given by you really helped a lot. `CopyIcon` function is creating so many GDI objects which are not getting released/deleted on time. I modified code and now all is working so fine. :) add answer over here and give me chance to accept it as `answer` :) – Amogh Mar 28 '16 at 11:32

1 Answers1

1

Look at the answer in this question. They discuss the same error message.

Community
  • 1
  • 1
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32