async
我在编写检索Image
表单的方法时遇到问题WebCam
。
我这样称呼这个方法:
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Source = Helper.LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);
}
和:
public static BitmapSource LoadBitmap(System.Drawing.Bitmap source)
{
ip = source.GetHbitmap();
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ip);
return bs;
}
上面的代码有效(它来自库),我试着写这个:
public async static Task<BitmapSource> LoadBitmap(System.Drawing.Bitmap source)
{
return await Task.Run(() =>
{
ip = source.GetHbitmap();
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ip);
return bs;
});
}
我得到错误:
错误 3 无法将类型“System.Threading.Tasks.Task”隐式转换为“System.Windows.Media.ImageSource”
而且不知道为什么,因为我返回了同样的东西。