我在数据库中有图像,我想异步加载到图片框中。我该怎么做呢?现在我有:
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
事件