我可以将打印屏幕上传到FTP而不保存到驱动器上吗?
在当前状态下,我将打印屏幕保存到驱动器“ E:\”,然后上传到FTP。
保存图像:
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(@"E:\pic.jpg", ImageFormat.Jpeg);
上传到FTP:
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile("ftp://127.0.0.1/xy.jpg", WebRequestMethods.Ftp.UploadFile, @"E:\pic.jpg");
}
您可以将位图保存到MemoryStream,将结果加载到字节数组中,并将字节数组写入WebRequest流中>
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/xy.jpg");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
byte[] fileContents;
using (MemoryStream sourceStream = new MemoryStream)
{
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(sourceStream, ImageFormat.Jpeg);
fileContents = new byte[sourceStream.Length];
sourceStream.Read(fileContents, 0, (int)sourceStream.Length);
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
}