Преобразувайте BitmapImage в Base64String в WinRT

Искам да конвертирам BitmapImage в base64string в моите приложения за Windows 8.1. КОД:

protected void UpdateSignatureAsync(BitmapImage bitmapImage, string fileName, long vehicleInsRecID)
{


    WriteableBitmap writimage = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmapExtensions.FromStream(writimage, ms);
        Stream s1 = writimage.PixelBuffer.AsStream();
        s1.CopyTo(ms);
        writimage.PixelBuffer.AsStream();
        var ic = new ImageCapture
        {
            ImageBinary = Convert.ToBase64String(ms.ToArray()),/// this line
            CaseServiceRecId = vehicleInsRecID,
            FileName = fileName
        };
        await UpdateImageAsync(ic);
    }

person Kashif    schedule 06.08.2015    source източник
comment
и какъв точно е проблемът, с който се сблъсквате?   -  person DL Narasimhan    schedule 06.08.2015
comment
... така че искате да запазите данните за изображението като Bitmap? или се опитвате да запазите необработени пиксели?   -  person JuanK    schedule 06.08.2015
comment
Този код е грешен. BitmapImage не преобразува WriteableBitmap. Просто взема височина и ширина на BitmapImage Byte array(ms.ToArray()) е всеки байт е 0.   -  person Kashif    schedule 07.08.2015
comment
Използвахте await в метод, който не е async - нещо друго не е наред с вашия примерен код.   -  person Peter Torr - MSFT    schedule 10.08.2015


Отговори (1)


Нямам точен отговор на въпроса ви, но ако имате StorageFile, което е изображение, тогава можете да конвертирате това изображение в низ Base64, като използвате метода GetBase64FromImageFile в следния клас:

public static class ImageHash
{
    private static async Task<string> GetBase64FromImageFile(StorageFile file)
    {
        return ComputeHash(await ConvertToByteArray(file));
    }

    private static string ComputeHash(byte[] buffer)
    {
        IBuffer input = buffer.AsBuffer();
        IBuffer hashed = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1).HashData(input);

        return CryptographicBuffer.EncodeToBase64String(hashed);
    }

    public async static Task<byte[]> ConvertToByteArray(StorageFile imageFile)
    {
        byte[] srcPixels;
        RandomAccessStreamReference streamRef =
            RandomAccessStreamReference.CreateFromFile(imageFile);

        using (IRandomAccessStreamWithContentType fileStream =
            await streamRef.OpenReadAsync())
        {
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
            BitmapFrame frame = await decoder.GetFrameAsync(0);

            // I know the parameterless version of GetPixelDataAsync works for this image
            PixelDataProvider pixelProvider = await frame.GetPixelDataAsync();
            srcPixels = pixelProvider.DetachPixelData();
        }

        return srcPixels;
    }
}
person Kristian Vukusic    schedule 19.08.2015