I trying to load my video from the resources folder but its not playing automatically once my form load. Can I know what mistake i have done. Thank you.
This is my c# code:
private void Form2_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"Resources/abc.mp4";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
Well I have solved it my ownself. Actually, I accidentally added the # symbol into my url. That causes the problem. This is the updated code.
private void Form2_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "Resources\\abc.mp4";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
To do that king of thing, you must get the stream of your resource.
So that code should work for you because works for me :)
// temporary file path - your temp file = video.avi
var strTempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Video.avi");
try
{
// ResourceName = the resource you want to play
File.WriteAllBytes(strTempFile, Properties.Resources.ResourceName);
axWMP.URL = strTempFile;
axWMP.Ctlcontrols.play();
}
catch (Exception ex)
{
// Manage me
}
You can implement a axWMP_PlayStateChange method to remove video.Avi at the end.
Hope it could help you
Related
I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)
In my C# application I want to delete file in below scenario.
OpenFileDialog and select any .jpg file.
Display that file in PictureBox.
Delete that file if needed.
I already try while doing step 3 I set default Image to PictureBox just before delete but that is not work.
How can I delete file? Please suggest me.
// Code for select file.
private void btnSelet_Click(object sender, EventArgs e)
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
txtFileName.Text = openFileDialog1.FileName;
myPictureBox.Image = Image.FromFile(openFileDialog1.FileName);
}
}
// Code for Delete file
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
//myPictureBox.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + #"\Images\defaultImage.jpg");
System.IO.File.Delete(txtFileName.Text);
MessageBox.Show("File Delete Sucessfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Replacing the image sounds like a good idea - but don't forget to dispose of the old Image that's still holding the file open (and will, by default, until that Image is garbage collected - at some unknown time in the future):
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
var old = myPictureBox.Image;
myPictureBox.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + #"\Images\defaultImage.jpg");
old.Dispose();
System.IO.File.Delete(txtFileName.Text);
MessageBox.Show("File Delete Sucessfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
(It may also be possible to Dispose of the Image directly without replacing the image for the PictureBox - it depends on what else you're going to do after deletion - e.g. if the form on which the PictureBox appears is closing that you may want to let that happen first and then just directly dispose of the image).
Okay so i wanted to know how i would take something that is in a textBox, then i press a button, the contents of the textBox will be saved to a file location, then when i load the .exe back up, the contents will reappear in the textBox.
This is what i have so far
private void button3_Click(object sender, EventArgs e)
{
File.WriteAllText(#"C:\Application.txt", textBox1.Text);
}
^To Write it to a file location, I have tried this multiple times but it doesnt seem to want to make the file on my C:.
private void Form1_Load(object sender, EventArgs e)
{
try
{
textBox1.Text = File.ReadAllText(#"C:\Application.txt", Encoding.ASCII);
}
catch
{
}
^To Load the file then inject it back into the textbox it came from
Any and all help is appreciated,
Thanks.
You likely get an exception when trying to write to your C drive because it requires administrative access. Try running Visual Studio as Administrator (therefor the app will run as admin when kicked off from VS) or try writing to another location. Your code is all fine. The Encoding.ASCII bit is unnecessary though and I recommend removing it (more than likely that's not the encoding you will write the file in).
Trying to write directly to the C: drive can cause problems.
Try writing to a location that you definitely have write access to. You could use the ApplicationData directory (for application files unique to the current user), or use SpecialFolder.MyDocuments if you prefer.
private string applicationFilePath = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "Application.txt");
private void button3_Click(object sender, EventArgs e)
{
File.WriteAllText(applicationFilePath, textBox1.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = File.ReadAllText(applicationFilePath, Encoding.ASCII);
}
I would do something like this:
using System.IO;
private void button3_Click(object sender, EventArgs e)
{
using (var stream = new FileStream(#"C:\Application.txt", FileMode.Create))
using (var writer = new StreamWriter(stream, Encoding.ASCII))
writer.Write(textBox1.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
using (var stream = new FileStream(#"C:\Application.txt", FileMode.Open))
using (var reader = new StreamReader(stream, Encoding.ASCII))
textBox1.Text = reader.ReadToEnd();
}
I think this method gives you more control on your content. Try to explore the contents of FileMode enum, and make sure to add your using System.IO; directive.
Be careful not to confuse the using statement with the using directive.
Also, always remember to dispose/close your stream when done to ensure that the data has been flushed and that the file is no longer in use by your application. Here, the using statement does the job of disposing when the stream is no longer in use.
EDIT: As mentioned by the other posts, writing to the C: directory causes problems on newer operating systems due to Admininstrative Access restrictions. Make sure to write to different drives/folders that you definitely have access to.
// Current User
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Application.txt");
// All users
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application.txt");
I have this project wherein I need to load pictures from a specific directory.
This code is working:
protected void Button1_Click(object sender, EventArgs e)
{
ShoImages(Server.MapPath(#"~/Images/"), "Images/");
}
Loads perfectly if I'm loading images of "Images" Folder inside my Visual Studio Project, but I want to load images inside a directory I'm getting error using this code below:
protected void Button1_Click(object sender, EventArgs e)
{
string strpath = #"D:\New Project\Uploads\12345\";
ShoImages(Server.MapPath(strpath), strpath);
}
The Error is - "'D:\New Project\Uploads\12345\' is a physical path, but a virtual path was expected."
Can you Please help me and give me an idea what to do. New with mappath. Thank you
MapPath takes a relative URL (~/Images/) and changes it into a local path on your machine (D:\Images). If you have a local path already, you don't need to call MapPath:
protected void Button1_Click(object sender, EventArgs e)
{
string strpath = #"D:\New Project\Uploads\12345\";
ShoImages(strpath, strpath);
}
Remove server.map path from ur code
ShoImages(strpath, strpath);
I used this :
ShoImages(HttpContext.Current.Server.MapPath(#"~/Uploads/12345/"), "Uploads/12345/");
instead HttpContext.Current.Server.MapPath pointing to my directory.
Thanks for sharing your ideas. Appreciated it. :D
My earlier post was unreadable so.
I am trying to read the last line of a text file every time it changes.
The code I have is,
private void fileSystemWatcherMCH1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string machState = File.ReadAllLines(#"C:\Users\sgarner\Documents\PROTOMET SHOP FLOOR\Machines\MACHINE_1.txt").Last();
btnMCH1.Text = machState;
btnMCH1.BackColor = Color.Blue;
}
If I only run the btnMCH1.BackColor = Color.Blue; it works. But I cannot read in the variable from the text file.
I am certain I am missing something simple.
Thanks,
It seems that your code is raising an exception but for any reason you're not seeing it. Maybe the file is being used by other process ... Try to catch it and then show it, so, you can see the problem:
private void fileSystemWatcherMCH1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
try
{
string machState = File.ReadAllLines(#"C:\Users\sgarner\Documents\PROTOMET SHOP FLOOR\Machines\MACHINE_1.txt").Last();
btnMCH1.Text = machState;
btnMCH1.BackColor = Color.Blue;
}
catch (Exception ex)
{
MessageBox.Show(ex.Messasge);
}
}