繁体   English   中英

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

[英]UWP file thumbnail save in SQL Server database

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

StorageFileByte[]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM