注册 登录
编程论坛 C图形专区

Direct2D怎样读取加密jpg?

seahdiao 发布于 2021-05-30 16:15, 1143 次点击
比方说,我有一个test.jpg,我把test.jpg的每个字节+=1,然后生成test.dat,请问direct2d应该怎么读取那个test.dat然后用WIC来解码绘制bitmap
1 回复
#2
seahdiao2021-05-31 18:10
找到了,首先先创造一个IWICStream 然后再用InitializeFromMemory来初始化他,最后再用CreateDecoderFromStream来创造Decoder

程序代码:

void dx::loadFromMem() {
    using namespace std;
    fstream file;
    file.open("test.dat", ios::binary | ios::in);

    file.seekp(0, ios::end);
    int length = file.tellp();
    file.seekp(0, ios::beg);
    byte* x = NULL;
    x = new byte[length];
    memset(x, 0, length);
    for (int i = 0; i < length; i++) {
        x[i] = file.get()-1;

    }
    file.close();
    IWICStream* file2=NULL;
    IWICBitmapDecoder* bitmapdecoder = NULL;
    imageFactory->CreateStream(&file2);
    file2->InitializeFromMemory(x, length);
   
    if (S_OK != imageFactory->CreateDecoderFromStream(file2, NULL, WICDecodeMetadataCacheOnLoad, &bitmapdecoder)) {
        MessageBox(NULL, L"Create Decoder Fail", L"cant open 2.jpg", MB_OK);
        exit(-1);
    }


    IWICBitmapFrameDecode* pframe = NULL;
    bitmapdecoder->GetFrame(0, &pframe);

    IWICFormatConverter* fmtcovter = NULL;
    imageFactory->CreateFormatConverter(&fmtcovter);
    fmtcovter->Initialize(pframe, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom);
    renderTarget->CreateBitmapFromWicBitmap(fmtcovter, NULL, &bitmap);

    fmtcovter->Release();
    pframe->Release();
    bitmapdecoder->Release();
}


[此贴子已经被作者于2021-5-31 18:13编辑过]

1