2013-05-26 123 views
1

我正在創建一個用位圖捕獲屏幕的C#程序。 而我想將它保存到.Avi/.mpeg文件。 但我不知道如何將它保存到視頻。C#保存位圖輸出

這是我已經有的代碼。

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; 
    } 

非常感謝!

+1

這可能幫助:http://stackoverflow.com/questions/5978632/bitmaps-to-avi-file-c-sharp-net –

+0

感謝你爲這個快速和有用的答案 –

回答

1

你好click this下載aviwrapper liblary。而且,你應該寫的代碼是這樣的:

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();