Switching between multiple Cameras using Emgu CV - winforms

I have a quick question: I need to switch between the two camera on a tablet. Front and Back. By default, the Front camera is always used by Emgu CV.
Thanks.

Ok. There is a different constructor. I was building upon the 7 line demo for Emgu CV.
Using the correct overloaded constructor, this is what did the trick for me:
private Capture _capture;
private void InitCapture(Int32 _camIndex) {
try {
if (_capture != null) {
Application.Idle -= ProcessFrame;
}
_capture = new Capture(_camIndex);
Application.Idle += ProcessFrame;
}
catch (NullReferenceException excpt) {
XtraMessageBox.Show(excpt.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg) {
Image<Bgr, Byte> frame = _capture.QueryFrame();
ImageBoxCapture.Image = frame;
}
private void CharmsBarBase_ButtonTop01Click(object sender, EventArgs e) {
InitCapture(0);
}
private void CharmsBarBase_ButtonTop02Click(object sender, EventArgs e) {
InitCapture(1);
}
Regards.

Related

Stop further link navigating inside web browser control [WPF]

Hello is there something similar to AllowNavigation like in WinForms?
My search didn't yield any satisfying result.
Basically I'm trying to open a webpage inside new wpf window and stop user from clicking random links on that webpage and navigating further.
Saw something with
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
Rest of the code is :
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
browser1.Navigating += browser1_Navigating;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
But it just makes my webpage not display ?
Thanks
Try this:
Once you have loaded your page, then you assign the browser_Navigating event handler.
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
browser1.Navigating += browser1_Navigating;
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}

Caching Cookies in Awesomium

The Setup:
I am building an MDI Application. One of the Child Forms I call is a basic web browser using the Awesomium API.
Reference: BrowserControl.cs
public partial class BrowserControl : UserControl
{
public BrowserControl()
{
InitializeComponent();
}
private void OnControlLoad(object sender, EventArgs e)
{
String w3DataPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\HttpAgent";
if (!Directory.Exists(w3DataPath))
{
Directory.CreateDirectory(w3DataPath);
}
var webSession = WebCore.CreateWebSession(w3DataPath, WebPreferences.Default);
webControl.WebSession = webSession;
}
private void OnBackButtonClick(object sender, EventArgs e)
{
this.webControl.GoBack();
}
private void OnFwdButtonClick(object sender, EventArgs e)
{
this.webControl.GoForward();
}
private void OnReloadButtonClick(object sender, EventArgs e)
{
this.webControl.Reload(true);
}
private void OnStopButtonClick(object sender, EventArgs e)
{
this.webControl.Stop();
}
private void OnHomeButtonClick(object sender, EventArgs e)
{
this.webControl.GoToHome();
}
private void OnSearchButtonClick(object sender, EventArgs e)
{
String urlString = String.Format("https://www.google.com/#q={0}", HttpUtility.UrlEncode(this.SearchBox.Text));
this.webControl.Source = new System.Uri(urlString);
}
private void OnLoadingFrame(object sender, Awesomium.Core.LoadingFrameEventArgs e)
{
this.BrowserStatus.Text = "Loading " + e.Url;
}
private void OnLoadingComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
this.BrowserStatus.Text = "Document Ready";
}
private void OnLoadingFailure(object sender, Awesomium.Core.LoadingFrameFailedEventArgs e)
{
this.BrowserStatus.Text = e.ErrorDescription;
}
}
Problem: Functionally the browser works just fine; however, it does not cache any persistent data locally. Note, I have established a valid DataPath using the Awesomium WebSession object as indicated in the above method OnControlLoad()
In terms of the validity of the path I define - I have confirmed it does exist on disk and it even appears the Awesomium WebSession adds a sub-directory called "cache". Unfortunately, none of the cookies I am getting in my web session are being saved locally. In fact - nothing is being cached. Both the HttpAgent directory I define and the cache directory the WebSession object defines are empty after deliberately logging into sites I know set cookies.
Any insight or help would be greatly appreciated. Thanks
I am not sure if it helps but try closing Awesomium "correctly": dispose WebControls, WebSession, call WebCore.Shutdown().

Mouse click and drag Event WPF

I am developing an analog clock picker control.
The user is able to click on the minute or hour hand and drag to turn the needle to select the specific time. I was wondering how to detect such a click and drag event.
I tried using MouseLeftButtonDown + MouseMove but I cannot get it to work as MouseMove is always trigger when the mousemove happen despite me using a flag. Is there any easier way?
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
//my code: moving the needle
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
I think this is the easiest and most straightforward way :
private void Window_MouseMove(object sender, MouseEventArgs e) {
if (e.LeftButton == MouseButtonState.Pressed) {
this.DragMove();
}
}
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
this.DragMove();
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
does the trick
You can make things easier and need not handle mouse down / up :
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
//my code: moving the needle
}
}

WinForms - WPF like painting

We know very well how easy is too create a WPF application where user can paint a rectangle using the mouse. To do this you just create a Rectangle control and set its coordinates, you don't worry about DoubleBuffering, repainting and such stuff. Well, I'd be very happy yo use WPF for the application where user can paint different shapes, but the clients insists to be a WinForms application. So the solution here is to use the XOR or ROP operation like in old good WinAPI years and I don't really like this. This doesn't give me a nice option to move a text while in XOR mode.
So I was thinking how can I achieve same smooth painting experience in a WinForms application like I'd have in WPF. Put together such a code, where I wanted to create a separate layer where I'd paint the current shape, while leaving intact the rest of the objects. I used same technique in an iPad application and worked pretty well.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace TestPainting
{
public partial class Form1 : Form
{
private bool _isMouseDown;
private Graphics _bufferGraphics;
private Point _startPos;
private TransparentPanel _paintBuffer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
_isMouseDown = true;
_paintBuffer = new TransparentPanel
{
Size = Size,
};
Controls.Add(_paintBuffer);
_paintBuffer.BringToFront();
_bufferGraphics = Graphics.FromHwnd(_paintBuffer.Handle);
_startPos = e.Location;
Capture = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!_isMouseDown)
return;
_bufferGraphics.Clear(Color.Transparent);
_bufferGraphics.DrawRectangle(Pens.Green, _startPos.X, _startPos.Y, e.X - _startPos.X, e.Y - _startPos.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_isMouseDown = false;
Capture = false;
_bufferGraphics.Dispose();
Controls.Remove(_paintBuffer);
_paintBuffer.Dispose();
}
}
public class TransparentPanel : Panel
{
public TransparentPanel()
{
DoubleBuffered = true;
}
[Browsable(false)]
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Do Nothing
}
}
}
which if course doesn't work as needed. I'm getting a black panel when pressing the mouse instead of a transparent one. Plus the rectangle while is painted flickers a lot, even though I did set the DoubleBuffering stuff.
Can someone provide some better ideas of such an implementation or maybe there some other open source project where I can see how other people are doing. I'd need to have same experience as in Paint.NET, just too bad is not open source anymore. (I know I can use Reflector, and I did, but man, there is tons of code over there :) )
Thx for any ideas.
Try this (see FIX #1 and FIX #2):
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
_isMouseDown = true;
_paintBuffer = new TransparentPanel
{
Size = Size,
};
Controls.Add( _paintBuffer );
_paintBuffer.BringToFront();
// FIX #1:
//
this.Refresh();
_bufferGraphics = Graphics.FromHwnd( _paintBuffer.Handle );
_startPos = e.Location;
Capture = true;
}
private void Form1_MouseMove( object sender, MouseEventArgs e )
{
if ( !_isMouseDown )
return;
//FIX #2:
// _bufferGraphics.Clear( Color.Transparent );
_bufferGraphics.Clear( this.BackColor );
_bufferGraphics.DrawRectangle( Pens.Green, _startPos.X, _startPos.Y, e.X - _startPos.X, e.Y - _startPos.Y );
}

WPF mediaelement

I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}

Resources