gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:34:37 31 4
gpt4 key购买 nike

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

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 事件

关于c# - 我想使用 LoadAsync 和 MemoryStream 将数据库中的图像加载到图片框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225610/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com