/// <summary> /// 视频信息浏览 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonVideoInfoScan_Click(object sender, EventArgs e) { try { Microsoft.DirectX.AudioVideoPlayback.Video video = new Video(videoFileName); double frameRate = 1 / video.AverageTimePerFrame; //////////////////////////////////////////////////////////////////////////在控件中进行视频浏览 WinStructs.VIDEOBASICINFO videoBasicInfo; videoBasicInfo = FrameGrabber.GetVideoBasicInfo(videoFileName); if (videoBasicInfo.totalFrames == 0) { throw new ArgumentException("Total Frames is 0!"); } int intervalFrameNum = Convert.ToInt32(textBox_IntervalNum.Text.Trim()); for (int i = 1; i <= videoBasicInfo.totalFrames; i += intervalFrameNum) { this.pictureBox1.Refresh(); this.pictureBox1.Image = FrameGrabber.GetFrameFromVideo(videoBasicInfo.fileName, i, Size.Empty, string.Empty); this.pictureBox1.Image.Save("D:\\frames\\" + i.ToString() + ".bmp"); } } catch (VideoAnalysisProcessSystem.ClassFile.InvalidVideoFileException ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (StackOverflowException) { MessageBox.Show("The target image size is too big", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void showVideoFrame(double part) { try { System.Drawing.Bitmap frame = FrameGrabber.GetFrameFromVideo(PathLoad, part); if (_intrinsicCamera != null && _distCoefs != null) { Image <Rgb, Byte> FrameBuffer = new Image <Rgb, byte>(frame); Image <Rgb, Byte> FrameResultBuffer = new Image <Rgb, byte>(frame.Size); CvInvoke.Undistort(FrameBuffer, FrameResultBuffer, _intrinsicCamera, _distCoefs); frame = FrameResultBuffer.ToBitmap(); } _imgPath = _tmpFolder + _imgName + _imgCounter++ + ".png"; frame.Save(@_imgPath, System.Drawing.Imaging.ImageFormat.Png); ImageBrush brush = new ImageBrush(); brush.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new System.Uri(@_imgPath, System.UriKind.Absolute)); CanvasIMG = brush; } catch (InvalidVideoFileException) { InfoText = "An error occured. Video file has the wrong format."; } catch (System.ArgumentOutOfRangeException) { InfoText = "An error occured. Video part doesn't exist."; } }
/// <summary> /// 单帧播放。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_FrameStep_Click(object sender, EventArgs e) { try { for (int i = 0; i < TotalFrameNum; i++) { pictureBox_GrabFrame.Refresh(); Application.DoEvents(); pictureBox_GrabFrame.Image = FrameGrabber.GetFrameFromVideo(openFileDialog1.FileName, i, Size.Empty, VideoFormat); Thread.Sleep(500); } } catch (Exception ex) { } }
/// <summary> /// 显示下一帧。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_NextFrame_Click(object sender, EventArgs e) { try { if (FrameNum + 1 <= TotalFrameNum) { pictureBox_GrabFrame.Image = FrameGrabber.GetFrameFromVideo(openFileDialog1.FileName, FrameNum + 1, Size.Empty, VideoFormat); nextFrame = FrameGrabber.GetFrameFromVideo(openFileDialog1.FileName, FrameNum + 1, Size.Empty, VideoFormat); } else { MessageBox.Show("当前帧没有前一帧", "提示"); } } catch (Exception ex) { } }
/// <summary> /// 抓图。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_CaptureImage_Click(object sender, EventArgs e) { try { if (openFileDialog1.FileName != "") { if (MyVideo.CurrentPosition >= 0 && MyVideo.CurrentPosition <= MyVideo.Duration) { double currentTime = MyVideo.CurrentPosition; //分析视频的格式 if (VideoFormat == "asf") //如果是asf格式的视频,则默认帧率为30 { FrameNum = (int)(currentTime * 30); } else { FrameNum = (int)(MyVideo.CurrentPosition / MyVideo.AverageTimePerFrame); } //设置文件名 string ImageName = ""; if (folderBrowserDialog_ImagePath.SelectedPath != "") { ImageName = folderBrowserDialog_ImagePath.SelectedPath + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + FrameNum.ToString() + ".bmp"; } else { ImageName = textBox_ImagePath.Text + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + FrameNum.ToString() + ".bmp"; } Image tempImage = FrameGrabber.GetFrameFromVideo(openFileDialog1.FileName, FrameNum, Size.Empty, VideoFormat); tempImage.Save(ImageName); pictureBox_GrabFrame.Image = tempImage; currentFrame = tempImage; } } } catch (Exception ex) { } }
/// <summary> /// اضافه کردن کلیپ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripMenuItem5_Click(object sender, EventArgs e) { DialogResult dr = ofdFilms.ShowDialog(); if (dr == DialogResult.Cancel) { return; } int i = FilmBrowser.NextNumber(); bool isinsert = false; //بر خلاف عکس کلیپ ها را یکی یکی اضافه می کنیم FileInfo fi = new FileInfo(ofdFilms.FileName); string fn = i.ToString("0000000000") + fi.Extension; try { string fa = @"Files\" + folderBrowser.GetCurrentFolderName() + @"\FILM\" + fn; File.Copy(ofdFilms.FileName, fa); isinsert = true; //برای گرفتن عکس از کلیپ از پروژه دانلود شده استفاده می کنیم try { Image img = FrameGrabber.GetFrameFromVideo(fa, 0.2d); img = (Image)(new Bitmap(img, new Size(100, 100))); img.Save(@"Files\" + folderBrowser.GetCurrentFolderName() + @"\FILM\\thum\" + fn + ".jpg"); } catch { Image img = new Bitmap(1, 1); Graphics drawing = Graphics.FromImage(img); img.Dispose(); drawing.Dispose(); img = new Bitmap(100, 100); drawing = Graphics.FromImage(img); drawing.Clear(Color.Silver); Brush textBrush = new SolidBrush(Color.Black); drawing.DrawString(fi.Name, this.Font, textBrush, 10, 40); drawing.Save(); textBrush.Dispose(); drawing.Dispose(); img.Save(@"Files\" + folderBrowser.GetCurrentFolderName() + @"\FILM\\thum\" + fn + ".jpg"); } } catch (Exception ex) { //اگر فایل اضافه شده و فقط در گرفتن عکس مشکلی وجود دارد یک عکس خالی برای آن اضافه کن if (isinsert) { Image img = (Image)(new Bitmap(100, 100)); img.Save(@"Files\" + folderBrowser.GetCurrentFolderName() + @"\FILM\\thum\" + fn + ".jpg"); } //continue; } //یک لحظه هیج پوشه ای را انتخاب نمی کنیم و دوباره پوشه قبلی را انتخاب می کنیم تا فایل های اضافه شده نمایش داده شود int t = folderBrowser.CurrentIndex; folderBrowser.CurrentIndex = -3; folderBrowser.CurrentIndex = t; }
public void SaveFrames() { if (!_videoLoaded) { InfoText = "Error: You have to load a video first"; return; } if (!System.IO.Directory.Exists(PathSave)) { InfoText = "Error: You have to select a folder to save the images"; return; } if (_canvasHeight == 0 || _canvasWidth == 0) { InfoText = "Error: Set the region to cut out in the video"; return; } int videoLength = (int)(_mediaDet.StreamLength * 1000); double offset; if (!double.TryParse(_frameOffset, out offset) || (offset <= 0.0 || offset > videoLength)) { InfoText = "Frame offset must be set in milliseconds between 0 and " + videoLength + "."; return; } if (SizeNxN < 0 || SizeNxN >= _canvasHeight) { InfoText = "Error: Max bounds for the size are from 0 to " + _canvasHeight; return; } double percentage = 0.0; int posX, posY, width, height; Progress = (int)(percentage * 100); for (double iFrame = 0.0; iFrame <= videoLength; iFrame += offset) { percentage = iFrame / videoLength; if (percentage > 1.0) { percentage = 1.0; } System.Drawing.Bitmap frame = FrameGrabber.GetFrameFromVideo(PathLoad, percentage); posX = (int)(frame.Width * (RectLeft / _canvasWidth)); posY = (int)(frame.Height * (RectTop / _canvasHeight)); width = (int)(frame.Width * (RectWidth / _canvasWidth)); height = (int)(frame.Height * (RectHeight / _canvasHeight)); frame = frame.Clone(new System.Drawing.Rectangle(new System.Drawing.Point(posX, posY), new System.Drawing.Size(width, height)), frame.PixelFormat); if (SizeNxN != 0) { frame = new System.Drawing.Bitmap(frame, new System.Drawing.Size(SizeNxN, SizeNxN)); } if (_intrinsicCamera != null && _distCoefs != null) { Image <Rgb, Byte> FrameBuffer = new Image <Rgb, byte>(frame); Image <Rgb, Byte> FrameResultBuffer = new Image <Rgb, byte>(frame.Size); CvInvoke.Undistort(FrameBuffer, FrameResultBuffer, _intrinsicCamera, _distCoefs); frame = FrameResultBuffer.ToBitmap(); } frame.Save(@PathSave + "\\" + "frameExtractor" + iFrame + ".png", System.Drawing.Imaging.ImageFormat.Png); Progress = (int)(percentage * 100); } Progress = 100; }