我想使用LoadAsync和MemoryStream将数据库中的图像加载到图片框中

我在数据库中有想要异步加载到图片框中的图像。 我该怎么办? 现在我有:

byte[] byteBLOBData = new byte[0]; byteBLOBData = (byte[])ds.Tables["magazine_images"].Rows[c - 1]["image"]; MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image = Image.FromStream(stmBLOBData); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; labelMsg.Text = "Picture loaded successfully."; 

我想这样做:

 pictureBox1.LoadAsync("What should I put here?"); 

我正在使用MySQL数据库和Visual Studio 2010 C#

不要将字节加载到图像中,这会破坏你想要实现的目的……(注意这是一个快速的肮脏,将图像放入临时文件……有一个这里有很多额外的考虑因素,当你完成时删除临时文件至少是其中之一)

 byte[] byteBLOBData = (byte[])ds.Tables["magazine_images"].Rows[c - 1]["image"]; string tempImageFileName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName() + ".jpg"); using( FileStream fileStream = new FileStream(tempImageFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite) ) { using( BinaryWriter writer = new BinaryWriter(fileStream) ) { writer.Write(byteBLOBData); } } pictureBox1.LoadCompleted += LoadCompleted; pictureBox1.WaitOnLoad = false; pictureBox1.LoadAsync(tempImageFileName); 

 private static void LoadCompleted( object sender, AsyncCompletedEventArgs e ) { if( e.Error != null ) { // will get this if there's an error loading the file } if( e.Cancelled ) { // would get this if you have code that calls pictureBox1.CancelAsync() } else { // picture was loaded successfully } } 

另请参阅LoadProgressChanged事件