UWP文件缩略图保存在SQL Server数据库中



我有3种存储文件,照片,视频和音频,我想将它们作为字节数组保存到数据库中。我尝试这样做并起作用,但是当我将它们从 byte[]转换为 StorageFile时,然后我尝试获取他们的缩略图是白色文件图标,而不是适当的缩略图图像。

StorageFile to 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;
}

所以我在DB中制作了一个新的列,以为我可以将缩略图分别存储在其上作为字节[]。

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();
}

我尝试使用BitmapImageWriteableBitmapImage进行,但是当我尝试从该字节[]

中获得缩略图时,我没有得到"没有发现的例外",没有什么可用

与位图:

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获取它们时我想使用它们(播放音频或视频并显示图像(,在所有三种类型中,我都需要在我的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[] { };
        }
}

最新更新