我在数据库中有想要异步加载到图片框中的图像。我该怎么做?现在我有:
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#
答案 0 :(得分:2)
不要将字节加载到图像中,这会破坏您尝试实现的目的......(请注意,将图像转换为临时文件是一种快速的方法。这里有很多额外的考虑因素,当你完成时删除临时文件至少是这样的。
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
事件