Created
March 25, 2014 05:57
-
-
Save krisives/9755958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Diagnostics; | |
namespace Remote | |
{ | |
public partial class Form1 : Form | |
{ | |
BoundsForm boundsForm = new BoundsForm(); | |
Bitmap captureBuffer = null; | |
Graphics captureGraphics; | |
Graphics controlGraphics; | |
int screenX = 0, screenY = 0; | |
Size screenSize = new Size(); | |
bool recording = false; | |
BufferedGraphicsContext bufferContext; | |
BufferedGraphics bg; | |
Graphics g; | |
EncoderParameters p = new EncoderParameters(1); | |
ImageCodecInfo jgpEncoder;// = GetEncoder(ImageFormat.Jpeg); | |
EncoderParameter qualityParam;// = new EncoderParameter(myEncoder, 50L); | |
Process encoderProcess; | |
public Form1() | |
{ | |
jgpEncoder = GetEncoder(ImageFormat.Jpeg); | |
qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10L); | |
p.Param[0] = qualityParam; | |
bufferContext = BufferedGraphicsManager.Current; | |
InitializeComponent(); | |
//SetStyle(ControlStyles.OptimizedDoubleBuffer, true); | |
//encoderProcess = createEncoderProcess(); | |
} | |
public Process getEncoderProcess() | |
{ | |
if (encoderProcess == null) | |
{ | |
encoderProcess = createEncoderProcess(); | |
} | |
return encoderProcess; | |
} | |
private Process createEncoderProcess() | |
{ | |
String ffmpegPath = "X:\\GRemote\\ffmpeg.exe"; | |
String args = String.Format( | |
"-f rawvideo -vcodec rawvideo -s {0}x{1} -framerate 10 -pix_fmt rgb24 -i - X:\\xxxx.avi", | |
captureBuffer.Width, | |
captureBuffer.Height | |
); | |
// Process p = Process.Start(ffmpegPath, args); | |
Process p = new Process(); | |
p.StartInfo.FileName = ffmpegPath; | |
p.StartInfo.Arguments = args; | |
p.StartInfo.UseShellExecute = false; | |
p.StartInfo.RedirectStandardInput = true; | |
p.StartInfo.RedirectStandardError = true; | |
//p.StartInfo.RedirectStandardOutput = true; | |
p.Start(); | |
return p; | |
} | |
private ImageCodecInfo GetEncoder(ImageFormat format) | |
{ | |
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); | |
foreach (ImageCodecInfo codec in codecs) | |
{ | |
if (codec.FormatID == format.Guid) | |
{ | |
return codec; | |
} | |
} | |
return null; | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
boundsForm.Show(); | |
} | |
private void snapshotTimer_Tick(object sender, EventArgs e) | |
{ | |
updateBounds(); | |
captureGraphics.CopyFromScreen(screenX, screenY, 0, 0, screenSize); | |
// Stream stream = new MemoryStream(); | |
// captureBuffer.Save(stream, ImageFormat.Jpeg); | |
// captureBuffer.Save(stream, jgpEncoder, p); | |
// bytesLabel.Text = stream.Position.ToString(); | |
StreamWriter sw = getEncoderProcess().StandardInput; | |
Stream stream = sw.BaseStream; | |
captureBuffer.Save(stream, ImageFormat.Bmp); | |
// encoderProcess.StandardError.BaseStream.ReadTimeout = 1; | |
// String s = encoderProcess.StandardError.ReadLine(); | |
//if (s != null && s.Length > 0) | |
// { | |
// Console.WriteLine(s); | |
//} | |
g.DrawImage(captureBuffer, 0, 0);//, 0, 0, captureBuffer.Width, captureBuffer.Height); | |
bg.Render(); | |
} | |
public void updateBounds() | |
{ | |
int w; | |
int h; | |
screenSize.Width = w = boundsForm.Width; | |
screenSize.Height = h = boundsForm.Height; | |
screenX = boundsForm.Left; | |
screenY = boundsForm.Top; | |
if (captureBuffer != null && (captureBuffer.Width != w || captureBuffer.Height != h)) | |
{ | |
captureBuffer.Dispose(); | |
captureBuffer = null; | |
} | |
captureBuffer = new Bitmap(w, h); | |
captureGraphics = Graphics.FromImage(captureBuffer); | |
controlGraphics = previewPanel.CreateGraphics(); | |
bg = bufferContext.Allocate(controlGraphics, new Rectangle(0, 0, w, h)); | |
g = bg.Graphics; | |
} | |
private void recordButton_Click(object sender, EventArgs e) | |
{ | |
toggleRecording(); | |
} | |
public void toggleRecording() | |
{ | |
recording = !recording; | |
if (recording) | |
{ | |
snapshotTimer.Start(); | |
} | |
else | |
{ | |
snapshotTimer.Stop(); | |
} | |
recordButton.Text = recording ? "Stop" : "Start"; | |
statusLabel.Text = recording ? "Recording..." : "Not Recording"; | |
} | |
private void previewPanel_Paint(object sender, PaintEventArgs e) | |
{ | |
// if (buffer == null) | |
// { | |
// return; | |
// } | |
// e.Graphics.DrawImage(buffer, 0, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment