【问题标题】:Generate Image from generated byte array in UWP vc++从 UWP vc++ 中生成的字节数组生成图像
【发布时间】:2018-06-25 21:04:57
【问题描述】:

参考Question & 回复@Decade Moon
我如何使用该方法从字节数组而不是图像文件生成图像。
我尝试如下,但没有任何效果。没有图片显示

std::vector<char> data= std::vector<char>(imgx->Height * imgx->Width * 4);
    int offset;
    for (int row = 0; row < imgx->Height; row++)
    {
        for (int col = 0; col < imgx->Width; col++)
        {
            offset = (row * (int)(imgx->Width * 4)) + (col * 4);
            data[offset] = 0x58;      // Red
            data[offset + 1] = 0x58;  // Green
            data[offset + 2] = 0x58;  // Blue
            data[offset + 3] = 0x58;  // Alpha
        }
    };

【问题讨论】:

    标签: c++ windows visual-c++ uwp windows-store-apps


    【解决方案1】:

    我的方法和你回复的有点不同,但效果很好。

    #include <wrl.h>  
    #include <robuffer.h>
    
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Windows::Storage::Streams;  
    using namespace Microsoft::WRL;  
    
    typedef uint8 byte;
    byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)  
    {  
        if (length != nullptr)  
        {  
            *length = pixelBuffer ->Length;  
        }  
        // Query the IBufferByteAccess interface.  
        ComPtr<IBufferByteAccess> bufferByteAccess;  
        reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));  
    
        // Retrieve the buffer data.  
        byte* pixels = nullptr;  
        bufferByteAccess->Buffer(&pixels);  
        return pixels;  
    }
    
    MainPage::MainPage()
    {
        InitializeComponent();
    
        auto bitmap = ref new WriteableBitmap(50, 50);
        image->Source = bitmap;
    
        unsigned int length;
        byte* sourcePixels = GetPointerToPixelData(bitmap->PixelBuffer, &length);  
        const unsigned int width = bitmap->PixelWidth;  
        const unsigned int height = bitmap->PixelHeight; 
    
        create_async([this, width, height, sourcePixels] {
            byte* temp = sourcePixels;  
    
            // generate RED - BLUE gradient
            for(unsigned int k = 0; k < height; k++) {
                for (unsigned int i = 0; i < (width * 4); i += 4) {
                    int pos = k * (width * 4) + (i);  
                    temp[pos] = (byte)(0xFF * k / (float)height);               // B
                    temp[pos + 1] = 0x0;                                        // G
                    temp[pos + 2] = 0xFF - (byte)(0xFF * k / (float)height);    // R
                    temp[pos + 3] = 0xFF;                                       // A
                }  
            }
        });
    }
    

    【讨论】:

    • 期待酷作品。
    • 在持续生成中使用时是否有任何开销,比如说每秒 30 次(如每秒帧数)
    • 我认为这不是创建动画内容的好方法。使用 Win2D library 肯定会更好。它是一个类似于 GDI+ 的 GPU 加速 2D 图形库。
    猜你喜欢
    • 2020-02-08
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多