I'm creating a C# program that's capturing the screen with bitmap.
And than I want to save it to an .Avi/ .mpeg file.
But I don't know how to save it to a video.
Here is the code I already have.
public Form1()
{
InitializeComponent();
}
static Bitmap bm;
private void btnFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowNewFolderButton = true;
DialogResult result = folderDlg.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = folderDlg.SelectedPath;
Environment.SpecialFolder root = folderDlg.RootFolder;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
SaveCapture(textBox1.Text);
}
private void SaveCapture(string path)
{
// Here should be the code to save it to mpeg/avi
}
private void timer1_Tick(object sender, EventArgs e)
{
// Take screenshot
bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bm as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bm.Size);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// Show it in picturebox
pictureBox1.Image = bm;
}
Thank you very much!
Create a Video Stream (AVI) from a Series of Images
I think this might be your best solution. Store all the .jpg's and create an avi from the command line at intervals. I don't see how creating video on the fly would produce a "lightweight" solution.
Hello click this to download the aviwrapper liblary. And the code that you should write is this:
var pngFileList = Directory.EnumerateFiles(folderImages, "*.png");
//load the first image
Bitmap bitmap = (Bitmap)Image.FromFile(pngFileList.First());
//create a new AVI file
AviManager aviManager = new AviManager(fileName, false); // location and the name of video file
//add a new video stream and one frame to the new file
//set IsCompressed = false
VideoStream aviStream = aviManager.AddVideoStream(false, 3, bitmap);
pngFileList.Skip(1).ToList().ForEach(file =>
{
bitmap = (Bitmap)Bitmap.FromFile(file);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
});
aviManager.Close();
Related
I would like to clear a pictureBox after clicking on an 'Encode' button to save it as a new image file in my project. I would like to clear the fields on the form after the user saves the image file.
The code I use to display image on pictureBox:
private void btnOpenfile_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files (*.png)|*.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
tbFilepath.Text = open.FileName;
//pictureBox1.ImageLocation = tbFilepath.Text;
}
}
The code I used to clear the pictureBox:
private void clearForm()
{
pictureBox1.Image = null; //doesn't work
pictureBox1.Invalidate(); //doesn't work
tbFilepath.Text = "";
tbMessage.Text = "";
}
I have also tried the following but it did not work either:
private void clearForm()
{
Bitmap bm = new Bitmap(img);
bm.Save(tbFilepath.Text,System.Drawing.Imaging.ImageFormat.Png);
}
I tried using the Refresh() method as one of the commentors suggested, but it did not work either:
private void clearForm()
{
Refresh(); //first attempt
pictureBox1.Refresh();// second attempt
}
I expect the pictureBox field to clear out the existing image I have selected but the image did not clear away.
Before clicking on encode button
After clicking on encode button, the textBox fields are cleared but not the pictureBox field. I used the codes I have added in this question.
Try creating a new Bitmap Variable and use it for the picturebox:
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
pictureBox.Image = bitmap;
pictrueBox.Invalidate();
Also, try to declare a Global Variable for the Bitmap so it is the one to be set as the picture and also the one to be cleared before setting it as the image for the PictureBox.
On the other hand you could try using the OnPaint Method of the Picurebox:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
Bitmap bm = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
pictureBox1.Image = bm;
}
private void btn_Open_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
if (ofd.ShowDialog() == DialogResult.OK)
{
bm = new Bitmap(Image.FromFile(ofd.FileName), new Size(pictureBox1.Width, pictureBox1.Height));
textBox1.Text = ofd.FileName;
pictureBox1.Invalidate();
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
bm = null;
pictureBox1.Invalidate();
}
These code I made worked perfectly. So please try it.
I am trying to figure out how to save a snapshot of a video being shown from a picturebox control. I can already save an image file, however my problem is the whole image that is 'seen' by my camera is the one that is being saved. What I would like to save is the image that is only being shown from my picturebox control, which is just a portion of what the camera is capturing. By the way I am using Aforge framework set of video libraries.
My picturebox is set at Height = 400 and Width = 400.
Here is a sample of my code
private void Form1_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in videoDevices)
{
drvrlist.Items.Add(device.Name);
}
drvrlist.SelectedIndex = 1;
videosource = new VideoCaptureDevice();
if (videosource.IsRunning)
{
videosource.Stop();
}
else
{
videosource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videosource.NewFrame += new NewFrameEventHandler(videosource_NewFrame);
videosource.Start();
}
}
private void startbtn_Click(object sender, EventArgs e)
{
if (videosource.IsRunning)
{
videosource.Stop();
}
else
{
videosource = new VideoCaptureDevice(videoDevices[drvrlist.SelectedIndex].MonikerString);
videosource.NewFrame += new NewFrameEventHandler(videosource_NewFrame);
videosource.Start();
}
}
private void videosource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
//throw new NotImplementedException();
}
private void save_btn_Click(object sender, EventArgs e)
{
SaveFileDialog savefilediag = new SaveFileDialog();
savefilediag.Filter = "Portable Network Graphics|.png";
if(savefilediag.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
if (pictureBox1.Image != null)
{
//Save First
Bitmap varBmp = new Bitmap(pictureBox1.Image);
Bitmap newBitmap = new Bitmap(varBmp);
varBmp.Save(savefilediag.FileName, ImageFormat.Png);
//Now Dispose to free the memory
varBmp.Dispose();
varBmp = null;
pictureBox1.Image = null;
pictureBox1.Invalidate();
}
else
{ MessageBox.Show("null exception"); }
}
}
You can use the Clone method to overwrite an instance of your picturebox's image with a subspace of the image.
Bitmap varBmp = new Bitmap(pictureBox1.Image);
varBmp = varBmp.Clone(new RectangleF(0, 0, 400, 400), varBmp.PixelFormat);
From there, you can go ahead and save it to a file.
varBmp.Save(savefilediag.FileName, ImageFormat.Png);
I am trying to save webcam image in directory using AForge.NET.
Here is my Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FilterInfoCollection webcam;
VideoCaptureDevice cam;
Bitmap bitmap;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
cam = new VideoCaptureDevice(webcam[0].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
pictureBox1.Image.Save("c:\\image\\image1.jpg");
}
But i am getting this exception:
InvalidOperationException was unhandled
Object is currently in use elsewhere.
If you are using Graphic objects after the GetHdc method, call the RealseHdc method.
Thanks in advance.
Problem is this line:
pictureBox1.Image = bitmap;
pictureBox1.Image.Save("c:\\image\\image1.jpg");
You are trying to save image that is not yet properly loaded and also you are facing cross-threading.
The solution in this case is to not use multiple threads when drawing.
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
try
{
this.Invoke((MethodInvoker)delegate
{
//saves image on its thread
pictureBox1.Image.Save("c:\\image\\image1.jpg");
});
}
catch (Exception ex)
{
MessageBox.Show(""+ex);
}
}
I wanted to display an image to the windows forms, but i already did this and the image did not come out.
Where did I go wrong?
Here is the code:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("../SamuderaJayaMotor.png");
pb1.Location = new Point(100, 100);
pb1.Size = new Size(500, 500);
this.Controls.Add(pb1);
}
Here (http://www.dotnetperls.com/picturebox) there 3 ways to do this:
Like you are doing.
Using ImageLocation property of the PictureBox like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "../SamuderaJayaMotor.png";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
Using an image from the web like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "http://www.dotnetperls.com/favicon.ico";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
And please, be sure that "../SamuderaJayaMotor.png" is the correct path of the image that you are using.
There could be many reasons for this. A few that come up quickly to my mind:
Did you call this routine AFTER InitializeComponent()?
Is the path syntax you are using correct? Does it work if you try it in the debugger? Try using backslash (\) instead of Slash (/) and see.
This may be due to side-effects of some other code in your form. Try using the same code in a blank Form (with just the constructor and this function) and check.
I display images in windows forms when I put it in Load event like this:
private void Form1_Load( object sender , EventArgs e )
{
pictureBox1.ImageLocation = "./image.png"; //path to image
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(0, 0);
pb.Size = new Size(150, 150);
pb.Image = Image.FromFile("E:\\Wallpaper (204).jpg");
pb.Visible = true;
this.Controls.Add(pb);
}
If i have a program which draws rectangles, circles and lines and i want to save the picture which the user has drawn on the form using SaveFileDialog, how would this be done?
I know how to save a text file using SaveFileDialog, just not sure how to save the form.
you can try this....
It will save form content as bitmap with SaveFileDialog
public class Form1
{
private Bitmap objDrawingSurface;
private Rectangle rectBounds1;
private void Button1_Click(object sender, System.EventArgs e)
{
objDrawingSurface = new Bitmap(this.Width, this.Height, Imaging.PixelFormat.Format24bppRgb);
rectBounds1 = new Rectangle(0, 0, this.Width, this.Height);
this.DrawToBitmap(objDrawingSurface, rectBounds1);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPG Files (*.JPG) |*.JPG";
if ((sfd.ShowDialog == Windows.Forms.DialogResult.OK))
{
objDrawingSurface.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public void SaveFormToFile(string fileName)
{
System.Drawing.Bitmap b = new Bitmap(this.Bounds.Width, this.Bounds.Height);
this.DrawToBitmap(b, this.Bounds);
b.Save(fileName );
}