public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
VideoCapture capture = new VideoCapture();
var img = capture.QueryFrame();
imageBox1.Image = img;
}
}
}
This is my code and I can't seem to make it work. Everything seems fine, I am unable to Debug. My ImageBox is not streaming a video just showing an image.
I found the answer. Thank You!. Everyone :)
public partial class Form1 : Form
{
bool _streaming;
VideoCapture _capture;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_streaming = false;
_capture = new VideoCapture();
}
private void Button2_Click(object sender, EventArgs e)
{
if (_streaming == false)
{
//Start streaming
Application.Idle += streaming;
button2.Text = "Stop Streaming";
}
else
{
Application.Idle -= streaming;
button2.Text = "Start Streaming";
}
_streaming = !_streaming;
}
private void streaming(object sender, EventArgs e)
{
var img = _capture.QueryFrame();
imageBox2.Image = img;
}
private void Button1_Click(object sender, EventArgs e)
{
imageBox1.Image = imageBox2.Image;
}
Related
I'm trying to programmatically add a ChromiumWebBrowser control to an application I'm developing. These are the steps I'm following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ChromiumWebBrowser chrome;
private void Form1_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
//Initializa
Cef.Initialize(settings);
textBox1.Text = "https://www.Google.com";
chrome = new ChromiumWebBrowser(textBox1.Text);
this.panel1.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
}
private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
textBox1.Text = e.Address;
}));
}
private void button1_Click(object sender, EventArgs e)
{
chrome.Load(textBox1.Text);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
i try display in one form, few panels, is possible?
image: https://prnt.sc/ublzvc
I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}
Here is my code:
public Form1()
{
InitializeComponent();
pBox1.AllowDrop = true;
}
private void pBox1_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pBox1.Image = bmp;
pBox1.Size = new Size(100, 100);
}
private void pBox2_MouseDown(object sender, MouseEventArgs e)
{
if (DoDragDrop(pBox2.Image, DragDropEffects.Move) == DragDropEffects.Move)
{
pBox2.Image = null;
}
}
pBox1 is the pictureBox that I would like to drag into, and pBox2 is the pictureBox I would like to drag from. The error I get is an object reference not set to instance of object error, on the line "if(DoDragDrop...." within the MouseDown method.
If what you have listed is the entire code listing, you are never setting pBox2.Image to an Image, which would cause the exception. May want to add:
private void pBox2_MouseDown(object sender, MouseEventArgs e)
{
if(pBox2.Image != null)
{
if (DoDragDrop(pBox2.Image, DragDropEffects.Move) == DragDropEffects.Move)
{
pBox2.Image = null;
}
}
}
To initialize the pBox2 to an image of some sort...
public Form1()
{
InitializeComponent();
pBox1.AllowDrop = true;
pBox2.Image = Image.FromFile(#"YourFilePath");
}
Edit
Just a note, this gets rid of your exception, but still does not implement drag drop properly. I am playing with it and will get back with you if I find a proper solution.
Edit
Possible duplicate to the following link:
Stack Overflow Thread
I got it working using the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
pictureBox2.Image = Image.FromFile(#"C:\TitleBar.jpg");
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox2.Image != null)
{
pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Move);
pictureBox2.Image = null;
}
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
}
this is the source code for the audio player:
public partial class MainWindow : Window
{
private DispatcherTimer timer;
public MainWindow()
{
InitializeComponent();
mPlayer.LoadedBehavior = MediaState.Manual;
mPlayer.UnloadedBehavior = MediaState.Manual;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += timer_Tick;
mPlayer.MediaOpened += mPlayer_MediaOpened;
}
void mPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
TimeSpan ts = mPlayer.NaturalDuration.TimeSpan;
SeekSlider.Maximum = ts.TotalSeconds;
}
void timer_Tick(object sender, EventArgs e)
{
SeekSlider.Value = mPlayer.Position.TotalSeconds;
}
private MediaElement mPlayer = new MediaElement();
private void ButtonOpen_Click(object sender, RoutedEventArgs e)
{
try
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
timer.Start();
mPlayer.Source = new Uri(ofd.FileName);
mPlayer.Volume = VolumeSlider.Value;
timer.Start();
mPlayer.Play();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void ButtonPlay_Click(object sender, RoutedEventArgs e)
{
mPlayer.Play();
timer.Start();
}
private void ButtonPause_Click(object sender, RoutedEventArgs e)
{
mPlayer.Pause();
timer.Stop();
}
private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
{
timer.Stop();
mPlayer.Stop();
}
private void SeekSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mPlayer.Position = TimeSpan.FromSeconds(SeekSlider.Value);
}
private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mPlayer.Volume = VolumeSlider.Value;
}
}
I don't know why it has sudden lags. Please help me in finding the problem.
If you think that MediaElement is not appropriate for playing different kinds of audio files. Please suggest an alternative.
The SeekSlider_ValueChanged event was firing too often. so that was causing some lag.
So i solved the problem using the Thumb.DragCompleted event. After this the playback is pretty smooth.
I have stack of images in a grid and i want to implement a slide show for it.I am using Microsoft VS 2010 Express Edition for Windows phone for implemenitng this.Can someone help ? The code is :
using System;
using System.Collections.Generic;
using System.Windows.Threading;
namespace swipe
{
public partial class MainPage : PhoneApplicationPage
{
// private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private int imageIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// tmr.Interval = TimeSpan.FromSeconds(5);
// tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
ShowNextImage();
}
private void LoadImages()
{
images.Add("/images/Hydrangeas.jpg");
images.Add("/images/Jellyfish.jpg");
images.Add("/images/Koala.jpg");
images.Add("/images/Tulips.jpg");
}
private void ShowNextImage()
{
// String bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
imageIndex = (imageIndex + 1) % images.Count;
}
//void tmr_Tick(object sender, EventArgs e)
//{
// ShowNextImage();
//}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//if (!tmr.IsEnabled)
//{
// tmr.Start();
//}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
//tmr.Stop();
base.OnNavigatedFrom(e);
}
private void Play_Click(object sender, RoutedEventArgs e)
{
ShowNextImage();
}
}
}
Here's a quick example of one way of doing this. It doesn't use a grid of images but I'm sure you can adjust this as you need to.
Edit: reread the title. If you want this to advance on button click get rid of the timer and call ShowNextImage() in the click event.
The page includes the following XAML:
<Image x:Name="myImg" />
The code behind looks like:
private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private int imageIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
ShowNextImage();
}
private void LoadImages()
{
// list the files (includede in the XAP file) here
images.Add("/images/filename1.jpg");
images.Add("/images/filename2.jpg");
images.Add("/images/filename3.jpg");
images.Add("/images/filename4.jpg");
}
private void ShowNextImage()
{
var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = bi;
imageIndex = (imageIndex + 1) % images.Count;
}
void tmr_Tick(object sender, EventArgs e)
{
ShowNextImage();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (!tmr.IsEnabled)
{
tmr.Start();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
tmr.Stop();
base.OnNavigatedFrom(e);
}
Sample code which shows using a button to advance code can be downloaded from http://cid-cc22250598bf7f04.office.live.com/self.aspx/Public/SlideShowDemo.zip
The easy way is to create a collection of images, and and go to next image in the collection when you go to next page in a pivot page.
A other solution is to create a usercontrol.
Here is a guide how to do it in silverlight ( but not in WP7, but it is very similar ).