如何在windows 8.1应用程序中显示存储在MYSQL中的blob图像
本文关键字:MYSQL 存储 中的 blob 图像 显示 应用程序 windows | 更新日期: 2023-09-27 18:04:35
我将图像保存到mysql作为Blob通常从visual studio windows 8.1项目,但我需要检索此图像并将其放入图像工具。
下面的代码将图像插入Mysql:
public static void insertImage()
{
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
};
StorageFile imageFile = await imagePicker.PickSingleFileAsync();
string a = imageFile.Name;
string FileName = a;
var inputStream = await imageFile.OpenSequentialReadAsync();
var readStream = inputStream.AsStreamForRead();
var buffer = new byte[readStream.Length];
await readStream.ReadAsync(buffer, 0, buffer.Length);
size = buffer.Length;
using (MySqlConnection connection = new MySqlConnection(
"server=localhost;database=familytree;uid=root;"))
{
connection.Open();
MySqlCommand update = new MySqlCommand("UPDATE node SET avatar = '" +
buffer + "' WHERE email = '" + email + "'", connection);
update.ExecuteNonQuery();
}
}
存储图片:
private static async Task FileToByteArray(Uri sourceUri){
var file = await StorageFile.GetFileFromApplicationUriAsync(sourceUri);
using (var inputStream = await file.OpenSequentialReadAsync())
{
var readStream = inputStream.AsStreamForRead();
var buffer = new byte[readStream.Length];
await readStream.ReadAsync(buffer, 0, buffer.Length);
return buffer;
}
}
和检索它:
private static async Task ConvertToImage(byte[] imageBytes){
var image = new BitmapImage();
using (var randomAccessStream = new InMemoryRandomAccessStream())
{
var dw = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
dw.WriteBytes(imageBytes);
await dw.StoreAsync();
image.SetSourceAsync(randomAccessStream);
}
return image;
}