在使用DirectShow桌面录制Fliped光标图标 [英] Fliped cursor icon on desktop recording using directshow
问题描述
使用Directshow.NET我已经开发了一个应用程序,它会记录桌面屏幕,来得到我们需要通过我们自己的画鼠标指针鼠标指针。所以我加了 SampleGrabber
ADN在 BufferCB
我写了下面的代码:
Using Directshow.NET I have developed an application which will record the desktop screen, to get mouse pointer we need to paint mouse pointer by our own. So I added SampleGrabber
adn in BufferCB
I have written below code:
public const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern bool DrawIcon(IntPtr hdc, int x, int y, IntPtr hIcon);
[DllImport("user32.dll")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
Graphics g;
Bitmap v;
v = new Bitmap(m_videoWidth, m_videoHeight, m_stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, pBuffer);
g = Graphics.FromImage(v);
CURSORINFO cursorInfo;
cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
if (GetCursorInfo(out cursorInfo))
{
if (cursorInfo.flags == CURSOR_SHOWING)
{
var iconPointer = CopyIcon(cursorInfo.hCursor);
ICONINFO iconInfo;
int iconX, iconY;
if (GetIconInfo(iconPointer, out iconInfo))
{
iconX = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
iconY = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);
DrawIcon(g.GetHdc(), iconX, iconY, cursorInfo.hCursor);
g.ReleaseHdc();
g.Dispose();
v.Dispose();
}
}
}
return 0;
}
此代码是画鼠标光标,但是光标翻转Y轴。
This code is painting the mouse cursor but cursor is flipped on Y axis.
这可能是因为 BufferCB
如果我们转换的pbuffer
中的位图,则该帧被翻转Y轴。后在 BufferCB
;为了解决这个我翻转Y轴当前帧加入 v.RotateFlip(RotateFlipType.RotateNoneFlipY)这种变化的鼠标指针是不是在桌面录制视频可见。
It's may be because in BufferCB
if we convert pBuffer
in Bitmap then that frame is flipped on Y axis. To solve this I flipped current frame on Y axis by adding v.RotateFlip(RotateFlipType.RotateNoneFlipY);
inside BufferCB
after this change mouse pointer is not visible in desktop recording video.
我怎么可以翻转鼠标指针?
How I can flip mouse pointer?
< STRONG>更新#1
我转换图标指向图标
然后到位图
使用 Icon.ToBitmap()
,然后翻转Y轴,这里是代码(感谢@Roman右):
I converted icon pointer to Icon
then into Bitmap
using Icon.ToBitmap()
and then flipped on Y axis, here is code (Thanks to @Roman R.):
...
iconX = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
iconY = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);
Icon ic = Icon.FromHandle(iconPointer);
Bitmap icon = ic.ToBitmap();
icon.RotateFlip(RotateFlipType.RotateNoneFlipY);
g.DrawImage(icon, iconX, iconY);
g.Dispose();
v.Dispose();
icon.Dispose();
ic.Dispose();
...
只有一个问题,我面对的上述修改,有时我得到的ArgumentException
在行位图图标= ic.ToBitmap();
System.ArgumentException occurred
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap.FromHicon(IntPtr hicon)
InnerException:
我处理使用的所有位图,还是我得到这个例外。
I disposed all the bitmaps used, still i get this exception.
推荐答案
您这里的问题是你的 BufferCB
的实施。您创建一个临时图形
/ 位图
对象,这样就可以使用光标覆盖更新。你有这样的位图倒过来,并将其应用于翻转图标正常战平。
The problem you have here is your BufferCB
implementation. You create a temporary Graphics
/Bitmap
object, so that it could be updated with the cursor overlay. You have this bitmap upside down and normal draw of the icon applies it flipped.
您需要考虑到,你会得到24位RGB缓冲的行正常秩序与 BufferCB
回调底部到顶部; 位图
构造一阶>预期是相反的。您需要线转移到位图
以相反的顺序,然后让他们回来分别。我不知道,如果负的步幅作品在那里很好,想必也不会。或仅仅叠加光标预翻转,以补偿翻转背景和叠加坐标太
You need to take into account that normal order of lines of 24-bit RGB buffer you get with BufferCB
callback is bottom-to-top; order of lines Bitmap
constructor expects is the opposite. You need to transfer lines into Bitmap
in reversed order, then get them back respectively. I am not sure if negative stride works out there well, presumably it will not. Or simply overlay the cursor pre-flipped, to compensate for flipped background and overlay coordinates too.
这篇关于在使用DirectShow桌面录制Fliped光标图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!