C
C#2y ago
bald

❔ Hello im making a recording software for fun and my code isnt really working

so im using visual studio 2022 and i use .net framework 4.7.2 and im trying to make it record for 5 seconds then open recording after that but litteraly nothing happens when i press the button and i have no idea why
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace custos3
{
public partial class Form1 : Form
{
private const int ScreenWidth = 1920; // Replace with your screen width
private const int ScreenHeight = 1080; // Replace with your screen height
private const int FrameRate = 30; // Frames per second
private const int Duration = 5; // Duration of recording in seconds

private string outputFileName;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hej");
// Disable the button during recording
button1.Enabled = false;

// Generate a unique file name for the recording
outputFileName = $"recording_{DateTime.Now:yyyyMMdd_HHmmss}.mp4";

// Start a new thread to perform the recording
Thread recordingThread = new Thread(RecordScreen);
recordingThread.Start();
}

private void RecordScreen()
{
// Create a bitmap with the screen dimensions
using (Bitmap screenshot = new Bitmap(ScreenWidth, ScreenHeight, PixelFormat.Format32bppArgb))
{
// Create a graphics object from the bitmap
using (Graphics graphics = Graphics.FromImage(screenshot))
{
// Create a VideoFileWriter with the output file name
Accord.Video.FFMPEG.VideoFileWriter videoWriter = new Accord.Video.FFMPEG.VideoFileWriter();
videoWriter.Open(outputFileName, ScreenWidth, ScreenHeight, FrameRate, Accord.Video.FFMPEG.VideoCodec.MPEG4);

// Calculate the total number of frames to record
int totalFrames = FrameRate * Duration;
int millisecondsPerFrame = 1000 / FrameRate;

// Start recording frames
for (int frame = 0; frame < totalFrames; frame++)
{
// Copy the screen contents to the bitmap
graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Write the frame to the video
videoWriter.WriteVideoFrame(screenshot);

// Wait for the next frame
Thread.Sleep(millisecondsPerFrame);
}

// Close the video writer
videoWriter.Close();
}
}

// Enable the button after recording
button1.Enabled = true;

// Open the saved recording
if (outputFileName != null)
System.Diagnostics.Process.Start("explorer.exe", $"/select, \"{outputFileName}\"");
}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace custos3
{
public partial class Form1 : Form
{
private const int ScreenWidth = 1920; // Replace with your screen width
private const int ScreenHeight = 1080; // Replace with your screen height
private const int FrameRate = 30; // Frames per second
private const int Duration = 5; // Duration of recording in seconds

private string outputFileName;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hej");
// Disable the button during recording
button1.Enabled = false;

// Generate a unique file name for the recording
outputFileName = $"recording_{DateTime.Now:yyyyMMdd_HHmmss}.mp4";

// Start a new thread to perform the recording
Thread recordingThread = new Thread(RecordScreen);
recordingThread.Start();
}

private void RecordScreen()
{
// Create a bitmap with the screen dimensions
using (Bitmap screenshot = new Bitmap(ScreenWidth, ScreenHeight, PixelFormat.Format32bppArgb))
{
// Create a graphics object from the bitmap
using (Graphics graphics = Graphics.FromImage(screenshot))
{
// Create a VideoFileWriter with the output file name
Accord.Video.FFMPEG.VideoFileWriter videoWriter = new Accord.Video.FFMPEG.VideoFileWriter();
videoWriter.Open(outputFileName, ScreenWidth, ScreenHeight, FrameRate, Accord.Video.FFMPEG.VideoCodec.MPEG4);

// Calculate the total number of frames to record
int totalFrames = FrameRate * Duration;
int millisecondsPerFrame = 1000 / FrameRate;

// Start recording frames
for (int frame = 0; frame < totalFrames; frame++)
{
// Copy the screen contents to the bitmap
graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Write the frame to the video
videoWriter.WriteVideoFrame(screenshot);

// Wait for the next frame
Thread.Sleep(millisecondsPerFrame);
}

// Close the video writer
videoWriter.Close();
}
}

// Enable the button after recording
button1.Enabled = true;

// Open the saved recording
if (outputFileName != null)
System.Diagnostics.Process.Start("explorer.exe", $"/select, \"{outputFileName}\"");
}
}
}
51 Replies
phaseshift
phaseshift2y ago
Debug it, go step by step
bald
baldOP2y ago
how? im a newbie even when i make new projects buttons do nothing i can do nothing nothing works
phaseshift
phaseshift2y ago
$debug
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
bald
baldOP2y ago
i dont understand much of it can you help?
phaseshift
phaseshift2y ago
Suggest you look up a tutorial. Debugging is a critical skill
bald
baldOP2y ago
whats debuggin used for?
phaseshift
phaseshift2y ago
To figure out problems in your code
bald
baldOP2y ago
but is it in my code or just my visual studio? cus the button should say "hej" right?
phaseshift
phaseshift2y ago
Why should it show that? You make a message box with that message. Not any button
bald
baldOP2y ago
but its in the button1_click
phaseshift
phaseshift2y ago
Yes
bald
baldOP2y ago
so it should say hej when pressed right?
phaseshift
phaseshift2y ago
The message box should show, yes
bald
baldOP2y ago
it doesnt
phaseshift
phaseshift2y ago
Then you didn't connect the event handler to the event, I guess
bald
baldOP2y ago
what... whats that supposed to mean?
phaseshift
phaseshift2y ago
Sorry don't have time to dig into the basics. If you're following a tutorial then I suggest to retrace your steps
bald
baldOP2y ago
im not really following a tutorial when i make a
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("hej");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("hej");
}
even that button dont work
phaseshift
phaseshift2y ago
The button exposes events. You '+=' on the event to add handlers, ie your button1_click Are you just writing these methods?
bald
baldOP2y ago
no i also add to design the form1.cs
phaseshift
phaseshift2y ago
Show more details about that, maybe someone can help with it I hate winforms and stay away from it
bald
baldOP2y ago
bald
baldOP2y ago
why? is there a easier way of making windows applications?
phaseshift
phaseshift2y ago
Yeah, but where is the button event handler connected to the button?
bald
baldOP2y ago
uuh how do i do that when i press on view code it puts me to form1.cs where the other code is
phaseshift
phaseshift2y ago
Tell me how the button click method were created? You wrote it? Or it was generated?
bald
baldOP2y ago
i dragged it in from toolbox and wrote code
phaseshift
phaseshift2y ago
Idk. Click the lightning bolt, what shows?
bald
baldOP2y ago
i havent changed anything in the propertys
phaseshift
phaseshift2y ago
cap5lut
cap5lut2y ago
if ya click on that u see the events
cap5lut
cap5lut2y ago
for that button the currently selected element
bald
baldOP2y ago
bald
baldOP2y ago
should i change something?
cap5lut
cap5lut2y ago
yeah, u would have to set it to ur button1_Click event handler method
phaseshift
phaseshift2y ago
There's no events handler set...
bald
baldOP2y ago
god damn.
cap5lut
cap5lut2y ago
?
bald
baldOP2y ago
this is so usefull ok new error after i run it it says exception unhandled at button.enabled = true;
cap5lut
cap5lut2y ago
would be good to know which exception
bald
baldOP2y ago
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'button1' accessed from a thread other than the thread it was created on.'
cap5lut
cap5lut2y ago
yeah u cant update GUIs from outside their own thread
bald
baldOP2y ago
wich means? i have to move the code? to the thread
cap5lut
cap5lut2y ago
well, if u dont do the recording inside its own thread, the whole window would hang for that time
bald
baldOP2y ago
oh. how do i fix
cap5lut
cap5lut2y ago
usually GUI frameworks provide some way to dispatch some callback to its GUI thread dunno how its called in winforms tho
cap5lut
cap5lut2y ago
Stack Overflow
update a control in UI with running background Thread in Winforms
I am using Background Worker Thread in my Winform, inside my Do_Work Event I am calculating something, what I need the thing is that simultaneously I would like to update a label which is in main/UI
bald
baldOP2y ago
cap5slut how do i make it so the playbackspeed isnt sped up when i change framerate?
cap5lut
cap5lut2y ago
dunno i never worked with videos/recording at all
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server