How to Take screenshot and upload to remote server using C#/.Net 4.5 and PHP.
.NET 4.5 includes an HTTP library that makes it incredibly easy to send HTTP GET/POST requests. There is no longer a need to use CURL.
Here is an example utility that uses System.Net.Http to send an image of the screen to a remote server.
private void grabScreen()
{
this.mtxUploadLock.WaitOne( /* 10000 */);
try
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
Bitmap screenShot = new Bitmap(
screenWidth, screenHeight,
System.Drawing.Imaging.PixelFormat.Format32bppArgb
);
Graphics gfx = Graphics.FromImage(screenShot);
gfx.CopyFromScreen(
Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy
);
string tempDirPath = System.IO.Path.GetTempPath();
string fileName = String.Format(tempDirPath + "/{0}.jpg",
DateTime.Now.ToString()
.Replace(":", "_")
.Replace(" ", "_")
.Replace("/", "_")
.Replace("\\", "_"));
screenShot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
/// send file to server.
String url = this.txtURL.Text;
this.uploadFile(url, fileName);
System.IO.File.Delete(fileName);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
}
finally
{
this.mtxUploadLock.ReleaseMutex();
}
}
private void uploadFile(string url, string file)
{
MultipartFormDataContent formData = new MultipartFormDataContent();
// Build POST variables as a key-value pair array
KeyValuePair<string, string>[] values =
{
new KeyValuePair<string, string>("app", "TigerScreenGrab"),
new KeyValuePair<string, string>("ver", "1"),
};
foreach (KeyValuePair<string, string> pair in values)
{
formData.Add(new StringContent(pair.Value), pair.Key);
}
ByteArrayContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(file));
fileContent.Headers.ContentDisposition
= new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = System.IO.Path.GetFileName(file)
};
formData.Add(fileContent);
HttpClient client = new HttpClient();
HttpResponseMessage result = client.PostAsync(url, formData).Result;
result.EnsureSuccessStatusCode();
}
Good Evening.