【问题标题】:Windows 8 metro apps convert existing image to byte[]Windows 8 Metro 应用程序将现有图像转换为字节 []
【发布时间】:2013-03-17 16:29:34
【问题描述】:

我正在尝试将现有图像(在我的应用程序的按钮内)保存到 byte[],然后将其保存到 SQLite 数据库。我知道如何使用FileOpenPicker 加载并转换为IRandomAccessStream,我知道如何保存到数据库,但我不知道如何在 Metro 应用程序/Windows UI 中将现有图像转换为字节数组。

我进行了很多搜索,但主要是使用 FileOpenPicker 或使用似乎不适用于 Metro 应用程序的内存流的示例。

谢谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以使用Datareader

        var file = await new FileOpenPicker().PickSingleFileAsync();
        var fStream = await file.OpenAsync(FileAccessMode.Read);
    
        var reader = new DataReader(fStream.GetInputStreamAt(0));
        var bytes = new byte[fStream.Size];
        await reader.LoadAsync((uint)fStream.Size);
        reader.ReadBytes(bytes);
    

    终于可以将bytes保存到数据库了。

    这里还有一个例子Reading a StorageFile to a byte array in C#

    添加编辑

    WinRT 你需要使用http://writeablebitmapex.codeplex.com/

    WinRT Loading an Image into a Byte array

    引用..

    基本上你需要将图像加载到 WriteableBitmap 中,然后 通过调用 PixelBuffer.AsStream() 访问其像素缓冲区。

    【讨论】:

    • 图像在我的应用程序的按钮内。我不需要使用 FileOpenPicker。我需要将该图像转换为字节数组。
    • 可以分享一些代码/xaml 吗?顺便说一句,您可以检查按钮的 bacground 属性以获取可以转换为字节数组的位图图像
    • 'foreach (var btn in ButtonsCollection) { var bmp = btn.Image; //////////////////////////////////////////////////////// /////// var reader = new DataReader(stream.GetInputStreamAt(0));// var bytes = new byte[stream.Size];等待 reader.LoadAsync((uint)stream.Size); reader.ReadBytes(字节); }'
    • 感谢 Amitd,但我正在使用 c# windows 8 Metro 应用程序并且没有 bitmapencoder 帧。
    • 对不起我的错.. 编辑了我的答案
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    相关资源
    最近更新 更多