IWICStream::InitializeFromFilename 方法 (wincodec.h)

從特定檔案初始化數據流。

語法

HRESULT InitializeFromFilename(
  [in] LPCWSTR wzFileName,
  [in] DWORD   dwDesiredAccess
);

參數

[in] wzFileName

類型: LPCWSTR

用來初始化數據流的檔案。

[in] dwDesiredAccess

類型: DWORD

所需的檔案存取模式。

意義
GENERIC_READ
讀取存取權。
GENERIC_WRITE
寫入存取權。

傳回值

類型: HRESULT

如果此方法成功,則會傳回 S_OK。 否則,它會傳回 HRESULT 錯誤碼。

備註

IWICStream 介面方法無法讓您提供檔案共享選項。 若要建立映像的共用檔案數據流,請使用 SHCreateStreamOnFileEx函 式。 然後,您可以使用 CreateDecoderFromStream 方法來建立 IWICBitmapDecoder

範例

此範例示範如何使用 InitializeFromFilename 來建立影像譯碼器。

    IWICImagingFactory *pFactory = NULL;
    IWICStream *pStream = NULL;
    IWICBitmapDecoder *pDecoder = NULL;

    HRESULT hr = CoCreateInstance(
                    CLSID_WICImagingFactory,
                    NULL,
                    CLSCTX_INPROC_SERVER,
                    IID_PPV_ARGS(&pFactory));

    // Create the stream.
    if (SUCCEEDED(hr))
    {
        hr = pFactory->CreateStream(&pStream);
    }

    // Initialize the stream from a specific file.
    if (SUCCEEDED(hr))
    {
        hr = pStream->InitializeFromFilename(L"test.jpg", GENERIC_READ);
    }    

    // Create a JPEG decoder.
    // Since the stream is created from the JPEG file, an explicit JPEG decoder
    // can be created using the generic IWICImagingFactory::CreateDecoder method.
    // However, use IWICImagingFactory::CreateDecoderFromStream method to auto
    // detect the stream and instantiate the appropriate codec.  
    if (SUCCEEDED(hr))
    {        
        hr = pFactory->CreateDecoder(
                GUID_ContainerFormatJpeg,   // Explicitly create a JPEG decoder.
                NULL,                       // No preferred vendor.
                &pDecoder);                 // Pointer to the decoder.
    } 

    // Initialize the decoder
    if (SUCCEEDED(hr))
    {
        hr = pDecoder->Initialize(
                pStream,                            // The stream to use.
                WICDecodeMetadataCacheOnDemand);    // Decode metadata when needed.
    } 

    // Process image frame.
    if (SUCCEEDED(hr))
    {
        UINT cFrames = 0;

        hr = pDecoder->GetFrameCount(&cFrames);

        if (SUCCEEDED(hr) && (cFrames > 0))
        {
            UINT width = 0, height = 0;        
            IWICBitmapFrameDecode *pBitmapFrame = NULL;

            hr = pDecoder->GetFrame(0, &pBitmapFrame);

            if (SUCCEEDED(hr))
            {
                // Do something with the frame here.
            }

            if (pBitmapFrame)
            {
                pBitmapFrame->Release();
            }
        }
    } 

    if (pStream)
    {
        pStream->Release();
    }

    if (pFactory)
    {
        pFactory->Release();
    }

    if (pDecoder)
    {
        pDecoder->Release();
    }

規格需求

需求
最低支援的用戶端 Windows XP 搭配 SP2、Windows Vista [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2008 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 wincodec.h
程式庫 Windowscodecs.lib
Dll Windowscodecs.dll