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}\"");
}
}
}
67 replies