我怎样才能使这段代码在异步下面,我不知道异步如何与 FTP 上传一起工作。我尝试了很多方法,但如果您上传文件,我不知道将“等待”放在哪里。
public static async void SelectRectangle(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
{
using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(Properties.Settings.Default.ftphost + Properties.Settings.Default.ftppath + FilePath + "." + Properties.Settings.Default.extension_plain);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Properties.Settings.Default.ftpuser, Properties.Settings.Default.ftppassword);
request.UseBinary = true;
bitmap.Save(request.GetRequestStream(), ImageFormat.Png);
}
}
}
乔里。
最佳答案
您需要创建方法async
,然后使用await
进行任何异步操作。您似乎只有其中之一,因此以下应该有效:
public static async Task SelectRectangle(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
{
using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(Properties.Settings.Default.ftphost + Properties.Settings.Default.ftppath + FilePath + "." + Properties.Settings.Default.extension_plain);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Properties.Settings.Default.ftpuser, Properties.Settings.Default.ftppassword);
request.UseBinary = true;
Stream rs = await request.GetRequestStreamAsync();
bitmap.Save(rs, ImageFormat.Png);
}
}
}
关于c# - 异步 FTP 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209587/