C# 如何在不保存到驱动器的情况下将打印屏幕上传到 FTP
C# How To upload print screen to FTP without saving to drive
我可以将打印屏幕上传到 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");
}
可以将Bitmap保存到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 = sourceStream.ToArray();
}
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}");
}
我可以将打印屏幕上传到 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");
}
可以将Bitmap保存到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 = sourceStream.ToArray();
}
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}");
}