我想从数据库中加载图像到使用LoadAsync和一个MemoryStream一个图片框 [英] I want to load image from the database into a picture box using LoadAsync and a MemoryStream
问题描述
我在数据库中的图像,我想要异步加载到图片框。我会怎么做呢?现在我有:
I have images in the database that I want to load asynchronously into a picture box. How would I do this? Right now I have:
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#
I'm using MySQL database and visual studio 2010 C#
推荐答案
不要字节加载到图像时,即会打败你想达到什么样的目的?(注意,这是一个快速-N -dirty获取图像到一个临时文件......在这里,而不是其中最重要的是删除,当你完成了临时文件有很多额外的考虑)
Don't load the bytes into the image, that'll defeat the purpose of what you're trying to achieve... (note this is a quick-n-dirty to get the image into a temporary file... there's a lot of additional considerations here, not the least of which would be to delete the temporary file when you're done)
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
事件
这篇关于我想从数据库中加载图像到使用LoadAsync和一个MemoryStream一个图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!