justinstenning / Direct3DHook

DirectX Capture and Overlays by using Direct3D API hooks
http://spazzarama.com/2011/03/14/c-screen-capture-and-overlays-for-direct3d-9-10-and-11-using-api-hooks
MIT License
582 stars 176 forks source link

When creating an ImageElement with the "Bitmap bitmap" override, DXHookD3D11 throws an exception #83

Open MarcellVokk opened 1 year ago

MarcellVokk commented 1 year ago

Hi!

So I'm trying to create an ImageElement from a Bitmap... until now, I was using the ImageElement's "Image" property, and converting my bitmap into a byte[] and using that to construct a new ImageElement, and this was and is working fine...

My problem however, is I'm trying to make the update loop as fast as possible, and converting an 1920x1080 bitmap to a byte[] takes more than 16ms, which is way to long for my purposes... I'm trying to use the "correct" method, and constructing the ImageElement with the Bitmap override to save some time, but for some reason, it always throws an error, and I can't seem to figure out what the issue is... Below I attached some additional context :)

Exception message:

Debug: DXHookD3D11: PresentHook: Exeception: System.ArgumentException: System.ArgumentException: Parameter is not valid.

Server stack trace: 
   at System.Drawing.Image.get_Width()
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.Drawing.Image.get_Width()
   at RenderHookAPI.Hook.DX11.DXImage.Initialise(Bitmap bitmap) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXImage.cs:line 55
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.GetImageForImageElement(ImageElement element) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 199
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.InitialiseElementResources() in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 108
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.Initialise(Device device, Texture2D renderTarget) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 82
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.Initialise(SwapChain swapChain) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 44
   at RenderHookAPI.Hook.DXHookD3D11.PresentHook(IntPtr swapChainPtr, Int32 syncInterval, PresentFlags flags) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DXHookD3D11.cs:line 556

The code that handles updating the overlay (called every frame):

        public void Draw(Bitmap frame, Point topLeft)
        {
            CaptureProcess.CaptureInterface.DrawOverlayInGame(new RenderHookAPI.Hook.Common.Overlay
            {
                Elements = new List<RenderHookAPI.Hook.Common.IOverlayElement>
                {
                    new RenderHookAPI.Hook.Common.ImageElement(frame) { Location = topLeft }
                }
            });
        }

Thanks in advance for anyones help :)! M

MarcellVokk commented 1 year ago

Update:

I also came across this exception:

Debug: DXHookD3D11: PresentHook: Exeception: System.Runtime.Serialization.SerializationException: System.Runtime.Serialization.SerializationException: Type 'System.Drawing.Imaging.BitmapData' in Assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

Server stack trace: 
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.SerializeResponse(IServerResponseChannelSinkStack sinkStack, IMessage msg, ITransportHeaders& headers, Stream& stream)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format)
   at RenderHookAPI.Hook.DX11.DXImage.Initialise(Bitmap bitmap) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXImage.cs:line 58
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.GetImageForImageElement(ImageElement element) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 199
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.InitialiseElementResources() in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 108
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.Initialise(Device device, Texture2D renderTarget) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 82
   at RenderHookAPI.Hook.DX11.DXOverlayEngine.Initialise(SwapChain swapChain) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DX11\DXOverlayEngine.cs:line 44
   at RenderHookAPI.Hook.DXHookD3D11.PresentHook(IntPtr swapChainPtr, Int32 syncInterval, PresentFlags flags) in D:\source\repos\DamnOverSharp\RenderHookAPI\Hook\DXHookD3D11.cs:line 556