facing issue of "Parameter is not valid" inside BufferCB - c#

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()

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

Related

Screenshot background window without BitBlt

Morning,
I was trying to screenshot a background window on Windows 11.
The usual methods such as BitBlt and PrintWindow are not working on Windows 11 and will always result in a top most window being capture although passing specific window handle as argument.
Those methods are not working in Windows 11 but flawless on Windows 10.
public static Bitmap PrintWindow(IntPtr hwnd)
{
RECT rc;
GetWindowRect(hwnd, out rc);
Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap = gfxBmp.GetHdc();
PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
gfxBmp.Dispose();
return bmp;
}
And using BitBlt
public static Bitmap BitBltWindow(IntPtr hwnd) {
RECT rc;
GetClientRect(hwnd, out rc);
IntPtr hdcFrom = GetDC(hwnd);
IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
int Width = rc.right;
int Height = rc.bottom;
Bitmap bmp = null;
IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, Width, Height);
if (hBitmap != IntPtr.Zero) {
IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
BitBlt(hdcTo, 0, 0, Width, Height, hdcFrom, 0, 0, CopyPixelOperation.SourceCopy);
SelectObject(hdcTo, hLocalBitmap);
DeleteDC(hdcTo);
ReleaseDC(hwnd, hdcFrom);
bmp = Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return bmp;
}
}
I also tested this in OBS and their BitBlt also fails where the Windows 10 1803 method works fine. But I haven't found out how to replicate this in C# with NET6.0 for windows.
As it relies on WinRT.GraphicsCapture and
var​ ​factory​ ​=​ ​WindowsRuntimeMarshal​.​GetActivationFactory​(​typeof​(​GraphicsCaptureItem​));
that isn't available for in the latest Net6.0 Target.
See: https://github.com/mika-sandbox/dotnet-window-capture/blob/master/Source/WinRT.GraphicsCapture/GraphicsCapture.cs#L6-L131
Are there any alternatives to capture a window?
Regards Artur

(C#) PrintWindow api returns black or partial images. Alternative methods?

I have searched all the topics in stackoverflow and tried all the suggestions, but Printwindow keeps giving me black, partial or practically blank screens. I refer to inactive / minimized applications especially.
I was reading that it could be due to some apps that handle this functionality badly, but this also happens with explorer.exe, and I want to hope that the problem is not attributable to this.
My goal is to create a window selector similar to what appears in Zoom when you do screen sharing.
Is there any alternative method?
For completeness I report the function I used, even if it is the same as reported on some question in stackoverflow.
(two proven methods)
public static Bitmap GetScreenshot(IntPtr hwnd)
{
RECT rc;
GetWindowRect(new HandleRef(null, hwnd), out rc);
Bitmap bmp = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap;
try
{
hdcBitmap = gfxBmp.GetHdc();
}
catch
{
return null;
}
bool succeeded = PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
if (!succeeded)
{
gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
}
IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hwnd, hRgn);
Region region = Region.FromHrgn(hRgn);//err here once
if (!region.IsEmpty(gfxBmp))
{
gfxBmp.ExcludeClip(region);
gfxBmp.Clear(Color.Transparent);
}
gfxBmp.Dispose();
return bmp;
}
public static Bitmap PrintWindow(IntPtr hwnd)
{
RECT rc;
GetWindowRect(hwnd, out rc);
Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap = gfxBmp.GetHdc();
PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
gfxBmp.Dispose();
return bmp;
}

How can I capture the screen and ignore a specific window in the image

I want to write a program that will magnify the middle of the screen, so I used some methods from user32.dll. I managed to make my program capture the screen and refresh the image on the picturebox with 60FPS rate, but when I want to display in real time the bigger image, the program also captures my forms which displays the bigger image, thus leading to an infinite image inside an image loop. I want to capture the screen and ignore my form which displays the bigger image.
I thought about something like I pass it the handle of the window I want to ignore and it will capture anything but the window i specified. This is my code for now:
Bitmap CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Bitmap img = null;
try
{
img = new Bitmap(Image.FromHbitmap(hBitmap), 1920 * 2, 1080 * 2);
using (Graphics g = Graphics.FromImage(img))
{
g.FillRectangle(Brushes.White, new Rectangle(0, 0, 200, 1080));//img.Width - (img.Height/2), img.Height));
// g.FillRectangle(Brushes.White, new Rectangle(img.Width - (img.Height / 2) + img.Height, 0, img.Width - img.Height / 2, img.Height));
}
GDI32.DeleteObject(hBitmap);
}
catch (Exception e)
{ }
return img;
}
The handle I pass it is from this method:
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
I already tried this solution: How can i capture the screen without the Form?
but it didn't work well because the picturebox refreshes at 60Hz rate which causes it to stutter.
To be clear, my idea is to magnify the middle of the screen and then display the magnified image in a square in the middle of the screen (the areas outside the square will be left with their original size).
When you capture the initial image, blank out the area that corresponds to inside your form by drawing a rectangle over the same area. You can get the window position and dimensions via the Form and draw the rectangle over that portion of the captured image.

Getting transparent shell-icons for files and folders in C#

I'm currently working on a small library that enables you to get icons from files and folders. Now, I don't care if it only works on win8+ (cause that's the place I'm going to use it), however, I've run in to a tiny problem with regards to transparency. If you take a look at the following image:
The one I generate (from my library) is to the left, windows explorer is to the right.
Now, as you might see, first off there is 2 black lines in the upper right of the one I generate, second, there is a difference in the background color. So what I'm wondering is this; is there no way to get the exact same image used by windows explorer, or am I simply doing it wrong?
My code (with exception to structs/externs etc. for shortness) bellow, entire code here.
public static class Icon
{
public static Image GetIcon(string fileName, int size)
{
IShellItem shellItem;
Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);
IntPtr hbitmap;
((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);
// get the info about the HBITMAP inside the IPictureDisp
DIBSECTION dibsection = new DIBSECTION();
Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
int width = dibsection.dsBm.bmWidth;
int height = dibsection.dsBm.bmHeight;
// zero out the RGB values for all pixels with A == 0
// (AlphaBlend expects them to all be zero)
for (int i = 0; i < dibsection.dsBmih.biWidth * dibsection.dsBmih.biHeight; i++)
{
IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));
if (rgbquad.rgbReserved == 0)
{
rgbquad.rgbBlue = 0;
rgbquad.rgbGreen = 0;
rgbquad.rgbRed = 0;
}
else
{
;
}
Marshal.StructureToPtr(rgbquad, ptr, false);
}
// create the destination Bitmap object
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// get the HDCs and select the HBITMAP
Graphics graphics = Graphics.FromImage(bitmap);
IntPtr hdcDest = graphics.GetHdc();
IntPtr hdcSrc = Gdi32.CreateCompatibleDC(hdcDest);
IntPtr hobjOriginal = Gdi32.SelectObject(hdcSrc, hbitmap);
// render the bitmap using AlphaBlend
BLENDFUNCTION blendfunction = new BLENDFUNCTION(BLENDFUNCTION.AC_SRC_OVER, 0, 0xFF, BLENDFUNCTION.AC_SRC_ALPHA);
Gdi32.AlphaBlend(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, width, height, blendfunction);
// clean up
Gdi32.SelectObject(hdcSrc, hobjOriginal);
Gdi32.DeleteDC(hdcSrc);
graphics.ReleaseHdc(hdcDest);
graphics.Dispose();
Gdi32.DeleteObject(hbitmap);
return bitmap;
}
}
It seems copying pixel by pixel was the solution. The following seems to be pixel-perfect equal to the explorer one.
public static Image GetIcon(string fileName, int size)
{
IShellItem shellItem;
Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);
IntPtr hbitmap;
((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);
// get the info about the HBITMAP inside the IPictureDisp
DIBSECTION dibsection = new DIBSECTION();
Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
int width = dibsection.dsBm.bmWidth;
int height = dibsection.dsBm.bmHeight;
// create the destination Bitmap object
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
for (int x = 0; x < dibsection.dsBmih.biWidth; x++)
{
for (int y = 0; y < dibsection.dsBmih.biHeight; y++)
{
int i = y * dibsection.dsBmih.biWidth + x;
IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));
if (rgbquad.rgbReserved != 0)
bitmap.SetPixel(x, y, Color.FromArgb(rgbquad.rgbReserved, rgbquad.rgbRed, rgbquad.rgbGreen, rgbquad.rgbBlue));
}
}
Gdi32.DeleteObject(hbitmap);
return bitmap;
}

Create a semi-transparent cursor from an image

Is it possible to create a cursor from an image and have it be semi-transparent?
I'm currently taking a custom image and overylaying the mouse cursor image. It would be great if I could make this semi-transparent, but not necessary. The sales guys love shiny.
Currently doing something like this:
Image cursorImage = customImage.GetThumbnailImage(300, 100, null, IntPtr.Zero);
cursorImage.SetResolution(96.0F, 96.0F);
int midPointX = cursorImage.Width / 2;
int midPointY = cursorImage.Height / 2;
Bitmap cursorMouse = GetCursorImage(cursorOverlay);
Graphics cursorGfx = Graphics.FromImage(cursorImageCopy);
cursorGfx.DrawImageUnscaled(cursorMouse, midPointX, midPointY);
Cursor tmp = new Cursor(cursorImage.GetHicon());
alt text http://members.cox.net/dustinbrooks/drag.jpg
I've tried following example, and it was working fine...
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
[DllImport("user32.dll")]
public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
{
IntPtr ptr = bmp.GetHicon();
IconInfo tmp = new IconInfo();
GetIconInfo(ptr, ref tmp);
tmp.xHotspot = xHotSpot;
tmp.yHotspot = yHotSpot;
tmp.fIcon = false;
ptr = CreateIconIndirect(ref tmp);
return new Cursor(ptr);
}
And i've put this on button click event (you can call from where you like):
Bitmap b = new Bitmap("D:/Up.png");
this.Cursor = CreateCursor(b, 5, 5);
And the Up.png image is saved with 75% opacity in AdobePhotoshop.
On the top of my head (I would try that first):
create new bitmap with same size as original, but with ARGB structure
drawimage: existing bitmap to the new bitmap
access raw bitmap data, and replace A bytes with 128
You should have nice semitransparent bitmap there.
If performance allows, you can scan for fully transparent pixels and set A to zero for them!
If you want to set transparency of a custom mouse cursor bitmap 'on the fly' you may find this function helpful. It uses a color matrix to set the amount of transparency to any given bitmap and will return the modified one. To have just a touch of transparency the TranspFactor should be between 225 and 245, just try it out. (You need to import System.Drawing and System.Drawing.Imaging)
public static Bitmap GetBMPTransparent(Bitmap bmp, int TranspFactor)
{
Bitmap transpBmp = new Bitmap(bmp.Width, bmp.Height);
using (ImageAttributes attr = new ImageAttributes()) {
ColorMatrix matrix = new ColorMatrix { Matrix33 = Convert.ToSingle(TranspFactor / 255) };
attr.SetColorMatrix(matrix);
using (Graphics g = Graphics.FromImage(transpBmp)) {
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr);
}
}
return transpBmp;
}
that is very easy, I don't use API.
the code is
Bitmap img = new Bitmap(new Bitmap(#"image.png"), 30, 30); //this is the size of cursor
Icon icono = Icon.FromHandle(img.GetHicon()); //create the Icon object
Cursor = new Cursor(icono.Handle); //the icon Object has the stream to create a Cursor.
I hope that is your solution

Categories