[英]UWP file thumbnail save in SQL Server database
我有3種存儲文件,照片,視頻和音頻,我想將它們作為字節數組保存到數據庫中。 我嘗試這樣做,但是它起作用了,但是當我將它們從byte[]
轉換回StorageFile
,我嘗試獲取的縮略圖只是一個白色文件圖標,而不是適當的縮略圖。
StorageFile
到Byte[]
:
public static async Task<byte[]> GetBytesAsync(StorageFile file)
{
byte[] fileBytes = null;
if (file is null)
{
return null;
}
using (var stream = await file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (var reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
return fileBytes;
}
byte[]
到StoragFile
:
public static async Task<StorageFile> GetStorageFileAsync(byte[] byteArray, string fileName)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(sampleFile, byteArray);
return sampleFile;
}
因此,我在數據庫中創建了一個新列,並認為我可以將縮略圖作為字節[]單獨存儲在其中。
public static async Task<byte[]> GetBytesForImageAsync(StorageFile mediafile)
{
WriteableBitmap bb = null;
using (var imgSource = await mediafile.GetScaledImageAsThumbnailAsync(ThumbnailMode.VideosView, Constants._thumbnailReqestedSize, ThumbnailOptions.UseCurrentScale))
{
if (!(imgSource is null))
{
bb = new WriteableBitmap(Convert.ToInt32(imgSource.OriginalWidth), Convert.ToInt32(imgSource.OriginalHeight));
await bb.SetSourceAsync(imgSource);
}
}
return bb is null ? new byte[] { } : bb.PixelBuffer.ToArray();
}
我試圖用BitmapImage
以及WriteableBitmapImage
做到這一點,但是當我嘗試從該字節取回縮略圖時,由於“未找到組件異常”,所以沒有任何效果。
使用位圖:
public async static Task<BitmapImage> GetImageFromBytesAsync(byte[] bytes)
{
var image = new BitmapImage();
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await image.SetSourceAsync(stream);
}
return image;
}
使用writeableBitmap
:
public static async Task<WriteableBitmap> GetImageFromBytesAsync(byte[] bytes)
{
using (var image = bytes.AsBuffer().AsStream().AsRandomAccessStream())
{
// decode image
var decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0);
// create bitmap
var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
await output.SetSourceAsync(image);
return output;
}
}
最終,我的目標是使用FileOpenPicker從本地設備中選擇音頻,視頻或照片文件(這也是我目前正在做的事情),然后將這些文件保存到SQL Server,並在以后從Web api獲取這些文件時我想要使用它們(播放音頻或視頻並顯示圖像),在所有3種類型中,我都需要縮略圖以顯示在gridview中。
您的問題出在GetBytesForImageAsync
方法中。 您獲得的byte[]
數據不正確。 請嘗試以下代碼:
public static async Task<byte[]> GetBytesForImageAsync(StorageFile mediafile)
{
byte[] bts;
using (var imgSource = await mediafile.GetScaledImageAsThumbnailAsync(ThumbnailMode.VideosView, Constants._thumbnailReqestedSize, ThumbnailOptions.UseCurrentScale))
{
if (!(imgSource is null))
{
using (MemoryStream stream = new MemoryStream())
{
await imgSource.AsStream().CopyToAsync(stream);
bts = stream.ToArray();
return bts;
}
}
else
return new byte[] { };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.