Direct2D 加载位图

说明:

通过WIC从文件加载位图.

可缩放后加载到内存.

源码:
 1 HRESULT LoadImageFormFile(
 2                           IWICImagingFactory *pWicFactory, 
 3                           ID2D1RenderTarget *pRT,
 4                           PCTSTR fname,
 5                           DWORD dstWidth,
 6                           DWORD dstHeight,
 7                           ID2D1Bitmap **ppBitmap
 8                           )
 9 {
10     IWICBitmapDecoder *pDecoder = NULL;  
11     IWICBitmapFrameDecode *pFrame = NULL;  
12     IWICBitmapScaler *pScaler = NULL;
13     IWICFormatConverter *pFormat =  NULL;  
14 
15     HRESULT hr = pWicFactory->CreateDecoderFromFilename(
16         fname,
17         NULL,
18         GENERIC_READ,
19         WICDecodeMetadataCacheOnLoad,
20         &pDecoder
21         );
22 
23     if ( SUCCEEDED(hr) )
24     {
25         hr = pDecoder->GetFrame(0, &pFrame);
26     }
27 
28     if ( SUCCEEDED(hr) )
29     {
30         hr = pWicFactory->CreateFormatConverter(&pFormat);
31     }
32 
33     UINT width, height;
34     if ( SUCCEEDED(hr) )
35     {
36         hr = pFrame->GetSize(&width, &height);
37     }
38 
39     if ( SUCCEEDED(hr) )
40     {
41         if (width)
42         {
43             FLOAT scalar = (FLOAT)dstWidth / (FLOAT)width;
44             width = (UINT)(width * scalar);
45         }
46         if (height)
47         {
48             FLOAT scalar = (FLOAT)dstHeight / (FLOAT)height;
49             height = (UINT)(height * scalar);
50         }
51         hr = pWicFactory->CreateBitmapScaler(&pScaler);
52     }
53 
54     if ( SUCCEEDED(hr) )
55     {
56         hr = pScaler->Initialize(pFrame, width, height, WICBitmapInterpolationModeCubic);
57     }
58 
59     if (SUCCEEDED(hr))
60     {
61         hr = pFormat->Initialize(pScaler, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeMedianCut);
62     }
63 
64     if ( SUCCEEDED(hr) )
65     {
66         hr = pRT->CreateBitmapFromWicBitmap(pFormat, ppBitmap);
67     }
68     SafeRelease(pDecoder);
69     SafeRelease(pFrame);
70     SafeRelease(pScaler);
71     SafeRelease(pFormat);
72     return hr;
73 }

 

你可能感兴趣的:(DI)