Capture mouse movement and export to image [code | app] - c#

To get more in depth information about the usage of WinForm or Webapplications I want to capture mouse-movement and click information. It would be perfect if this could be rendered to an image.
The result would be something like this:
The question is how does one start with creating an app like this?
How do I receive mousemovements and clicks when its a process from another application?
How convert mousemovements to an image?
(Anyone know if there is a opensource of free/cheap app which can do this)
IOGraphica is able to do this, but it is in Java and it is free, but not open source.

I would try to capture any mouse movement/click of your windows and store it in a collection. When you finished recording - try to draw your image. I would recommend looking for "c# capture mouse" or "mouse hook" in google. Some time ago I did this for getting a keyboard hook. You may have a look at Processing Global Mouse Events.
In terms of code this may help:
Dictionary<Point, MouseEventInfo> dict = new Dictionary<Point, MouseEventInfo>();
/// see given link to find the correct way to get this kind of event
public void mouse_event(....)
{
/// get mouse coordinates
/// create point struct
/// check if point exists in DICT
/// no - add new MouseEventInfo at Point
/// yes - access MouseEventInfo object and increase a value according to the event
}
public void onFinished()
{
/// create new bitmap/image - width/height according to your screen resultion
/// iterate through all MouseEventInfo objects and draw any information
}
/// stores mouse event info for a point
class MouseEventInfo
{
Point p;
int moved=0;
int clicked=0;
int doubleClicked=0;
}
I know this is just a piece of pseudoCode - anyway - I hope this may help you! Ah - keep in mind that any kind of hook (keyboard, mouse or anything else) may result in a virus alert of your antiVir.
hth

If you're looking for something which you could run externally and track mouse movement and clicks, you might find this mouse/keyboard hooking project over at codeproject.com useful:
http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx
There's a sample application and a hooking lib with full source. It'd be a starting point to use in your efforts.

Related

HOW to get LOW touch point argument for each touch point in UWP by C#

I need to filtrate touch point in an UWP app. I need to filtrate touch point according to timestamp of a touch point, and get coordinate or id of the touch point.
In a WPF application , I can get necessary data of touch point by using TouchFrameEventArgs class, and using the method GetTouchPoints().
But, In UWP app, what should I use? I want to get the first occurred touch point after a specific event occurs.
First, generally speaking, touhc, mouse and pen input in UWP is abstracted to a single event set dealing with pointers.
When you handle a pointer event like PointerDown, you get an instance of PointerRoutedEventArgs class. This class will help you identify a specific pointer using the Pointer.PointerId property. This a unique identification you can use to check for the pointer that raised this event. You can store the pointer IDs you have encountered in time which should provide you with the behavior you are requesting.

How to block bing map pan to coordinate bound?

I'm using bing maps components in C# for WP7 with my custom tiles.
I override Microsoft.Phone.Controls.Maps.TileSource, all ok, I'm able to use my tiles.
But, because don't have entire world, I want to limit pan only in one city bound?
It's correct to use MapDragEventArgs to check and block pan??
private void map_MapPan(object sender, MapDragEventArgs e)
{
Map m = (Map)sender;
// something...?
}
...or I need to override something else? In that case, what do I need to override?
thanks.
I have not worked with Bing Maps on Windows phone 7, so going solely by the documentation, I would do the following:
Always initialize the map such that its view bounding rectangle is within your defined tile area.
Bind to ViewChangeStart event. (or ViewChangeOnFrame, you will have to experiment and figure out which to use)
Inside handler for ViewChangeStart, check if TargetBoundingRectangle properly falls outside of your tile coverage area.
If TargetBoundingRectangle falls outside of your coverage area, figure out a way to cancel the view change or manually set the bounding rectangle back the previous valid state.
Assuming everything works like I expect it to, this should limit the users map navigation to your limited areas and avoid the behavior of the view "Snapping" back to some other place after the user has navigated.
What you can easily do and would be the best approach is to use the ViewChangeEnd event and set back the map to your coverage area.
See the MSDN reference to do this:
http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.maps.core.mapcore.viewchangeend(v=vs.92).aspx

Dynamic-Data-Display / Getting x-coordinate of user input

My program is basically about analyzing videos.
A major part is to plot a diagram showing (f.e.) brightness per frame on y-axis and every frame number on x-axis. Because the program is written in C# and uses WPF, D³ was the way to go for plotting.
Now the user might see a peak signal in the diagram and wants to look on that single frame to understand why it's so bright (it might be just natural, or an encoding-artifact).
There comes my question: The most intuitive way for the user to click on the diagram where the peak is, which jumps the video preview (other GUI element) right to that frame. So I need the x-coordinate (=frame number) of the user click on the diagram.
It is possible to manually analyze the mouse-input event, but that would take much work (because the x-axis is different for each video and the entire diagram can be resized, so absolute coordinates are a no go).
But maybe something similar is already implemented by D³. I searched the documentary, but didn't find anything useful. The only piece of information was using a "DraggablePoint", but that's where the trail goes cold.
Does someone of you know how to get the x-coordinate without much work?
It sure is possible! The way that I have done it in the past is to add a CursorCoordinateGraph object to my plotters children, and it automatically tracks the mouse position on the graph in relation to the data. You can turn off the visual features of the CursorCoordinateGraph and use it for tracking only. Here's what it would look like:
CursorCoordinateGraph mouseTrack;
plotter.Children.Add(mouseTrack);
mouseTrack.ShowHorizontalLine = false;
mouseTrack.ShowVerticalLine = false;
And your mouse click event would look like this:
private void plotter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point mousePos = mouseTrack.Position;
var transform = plotter.Viewport.Transform;
Point mousePosInData = mousePos.ScreenToData(transform);
double xValue = mousePosInData.X;
}
You can then use xValue and manipulate it however you would like to achieve your desired effect.

I want to force a render, but only draw as fast as possible (InvalidateVisual / CompositionTarget.Rendering)

I'm working on a real-time WPF/Silverlight (and soon WP7) visualization component and I'm looking for the best solution to force a redraw of the entire component in a Game-loop style. Redraw should be on-demand, but I don't want to back up the message pump with re-draw calls. Most of the drawing in my component is done using non-WPF primitives (e.g. Bitmap Interop, Direct2D) so my code does not use InvalidateVisual, and as a result, currently looks like this
// Pseudocode, doesnt compile, just to convey the meaning
public void InvalidateElement()
{
if (CurrentlyDrawing)
return;
Dispatcher.BeginInvoke(() =>
{
CurrentlyDrawing = true;
DoDrawInternal();
CurrentlyDrawing = false;
}
}
Ok so this is great. If I call InvalidateElement lots of times I get good responsiveness. However, what I want to do is ensure I can push data to my visualization component as fast as possible but only draw when the component is able to draw, and not keep drawing to catch up with the data once the input stream completes.
No I can't override OnRender, I'm using non-WPF drawing inside WPF ;-)
Basically what I want is something like the old Invalidate() / OnPaint in WindowsForms, or better yet, a game loop in DirectX.
At the moment I get the situation where if I have an external thread that pushes data to the visualization component at a high rate then if I Stop pushing data I get another 20 seconds worth of refreshes to get through before the component stops drawing. I want to stop drawing as soon as data has gone in.
Another idea I had was to handle CompositionTarget.Rendering in the visualization component then implement some sort of rudimentary Queue to push data to and the Rendering event consumes this data as fast as it can.
In Summary
Given a WPF visualization component, V, and a datasource which pushes it data every 1ms, D, how can I ensure that no matter the datarate of D, V draws data at 30FPS (or whatever it can do) and updates itself in chunks, sort of how a game render loop does in DirectX?
When the data stops, V should redraw everything it has up to now in one go. When the data is too fast, V draws larger chunks at a time to compensate.
If you need more information I'd be happy to share it. Right now I've just posted a synopsis to gauge if there are any quick fixes but a fuller Q with code examples can be provided on request.
Best regards,
You might want to consider rendering on the CompositionTarget.Rendering event and throttling on the invalidated state.
Silverlight game loop example (F#):
/// Run game
let runGame () =
let state = gameState.GetEnumerator()
let rate = TimeSpan.FromSeconds(1.0/50.0)
let lastUpdate = ref DateTime.Now
let residual = ref (TimeSpan())
CompositionTarget.Rendering.Add (fun x ->
let now = DateTime.Now
residual := !residual + (now - !lastUpdate)
while !residual > rate do
state.MoveNext() |> ignore
residual := !residual - rate
lastUpdate := now
)
Play the game: http://trelford.com/blog/post/LightCycles.aspx
Read the source: https://bitbucket.org/ptrelford/lightcycles
You can listen to the CompositionTarget.Rendering event, which is triggered right before WPF renders the UI, and do your drawing in there.
Another tidbit.. InvalidateVisuals() is nothing like Form.Invalidate(), as it also causes re-layout which is expensive. If you want something like Form.Invalidate(), then create a DrawingGroup (or bitmap image) "backingStore", place it in the DrawingContext during OnRender(), and then update it whenever you want. WPF will automatically update and repaint the UI.
Have you thought of using a dispatch timer running at 30FPS, then take a snapshot of the current data and rendering it at each timer tick? If you want to avoid redrawing if nothing has changed, you can simply keep timestamps for LastChanged and LastRendered, only performing an actual redraw if LastChanged > LastRendered. Basically updating the data and rendering the data are decoupled from one-another; the main trick is making sure you can somehow get a coherent snapshot of the data when the rendering thread wants to render it (i.e. you'll need some sort of locking.)
I was recently working with a project that required a game loop like style. Although my example is purely in F#, you can figure it out how you can do that way in C# too, may be use some interop code to initialize the timer and hooking up events as given in this below link,
http://dl.dropbox.com/u/23500975/Demos/loopstate.zip
The sample doesn't show how to redraw, it just updates the underlying stock data for every 500ms, It should pretty much work for any kind of drawing mechanisms with WPF. The core idea is to use composable events, in F# an event is a first-class citizen + an IObservable (reactive extensions for C#), so we can easily compose functions that in-turn return a set of events or a single event. There is a function Observable.await, which takes in an Observable and also has a state to return.
eventSource
|> Observable.await(fun (t:State.t) e ->
// return the modified state back on every check or in the end
match e with
// start button click
| Choice1Of3(_) ->
{t with start=true}
// stop button click
| Choice2Of3(_) ->
{t with start=false}
// timer tick event,
| Choice3Of3(_) ->
if t.start = true then
handleStockUpdate(t)
else t
) (state)
I just used some of FP terms here, but it should work just fine with normal C# (OO) way of doing things here.
Hope this helps!
-Fahad
I'm not sure why you would use WPF for your front-end if you're drawing using non-WPF elements and require the Invalidate() method that was provided by WinForms? Can't you just switch the UI to use WinForms?

How to draw on an unfilled box on a video stream using the mouse

I am using dshownet(first time) and C#. I have got a sample to take the web cam input and display it on a form. I now need to draw a rectangle on top of the video stream using the mouse. (the intent is to track what is inside the box from there onwards).
I heard that there is something called VMR. So I went to the dshownet samples and went through them. I didnt find any samples that use the mouse to overlay a shape on the video stream. Someone on here suggested to use colorkey. Another person said to use GDI+ and mouse handling. I tried to compile the DXLogo sample but got this error :
Error 1 Cannot create an instance of the abstract class or interface 'System.Drawing.Image' C:\Documents and Settings\TLNA\Desktop\Final Year Project\Libraries\DirectShow library 2\DirectShowSamples-2010-February\Samples\Capture\DxLogo\Capture.cs 128 32 DxLogo-2008
for the code section:
if (fileName.Length > 0)
{
m_Bitmap = new Image(fileName); // error happened here
Rectangle r = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
m_bmdLogo = m_Bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
}
I know that I must go through the Bitmap9 Interface. But I really dont know where to start. Should I read the Directshow API docs.
btw I also have the book Programming Microsoft Directshow for digital video and television. I started reading that book and stopped after a few pages since the code is mainly in C++. Should I continue to read this is book ? How can I accomplish the certain mentioned tasks in C# ?
Any suggestions as how to draw on the video. Some useful links(tutorials) would be helpful.
Many Thanks
Tlna
I'm not sure why the DirectShow sample doesn't compile, but you may be able to change the problem line:
m_Bitmap = new Image(fileName);
to this:
m_Bitmap = new Bitmap(fileName);
and get it to work.
You're actually facing a pretty difficult problem here. DirectShow renders a video by drawing a series of still frames to a device context (like a PictureBox or a Form, or even the screen) many times a second (depending on whatever the frame rate is). You (as a programmer) can also (easily) render graphics directly to this same device context.
However, in order to make your drawn box appear over top of a running video, your code needs to draw the rectangle immediately after DirectShow draws each frame of the video; otherwise, the next frame will obliterate your rectangle. DirectShow objects probably have some sort of frame rendering event that you can handle, and then inside the event handler you just re-draw your box (based on the initial and current mouse coordinates, which you can get from the MouseDown and MouseMove events of whatever control you're drawing on).
Update: I just took a look at my code from when I was playing around with DirectShow.NET, and it looks like there is an event (DsEvCode.Repaint) that you could hook into and use to draw your box.
I haven't looked at the code sample you're working with, but do a search and see if you can find an OnGraphNotify() method in your code, you should be able to add something like this:
if (code == DsEvCode.Repaint)
{
// draw the box here
}
Presumably, this event is fired after each frame of the video is rendered, so if you redraw your box here every time it will appear as if the box is persisting.

Categories

Resources