{"id":7500,"date":"2025-12-22T15:12:40","date_gmt":"2025-12-22T07:12:40","guid":{"rendered":"https:\/\/support.fuxinsoft.cn\/?p=7500"},"modified":"2025-12-26T17:19:16","modified_gmt":"2025-12-26T09:19:16","slug":"bitmap%e7%b1%bb%e5%9e%8b-%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e4%b8%bawindwos%e7%9a%84hbitmapc","status":"publish","type":"post","link":"https:\/\/support.fuxinsoft.cn\/?p=7500","title":{"rendered":"Bitmap\u7c7b\u578b \u600e\u4e48\u8f6c\u6362\u4e3awindwos\u7684HBITMAP(C++)"},"content":{"rendered":"\n<p>HBITMAP\u662f Windows GDI\uff08\u56fe\u5f62\u8bbe\u5907\u63a5\u53e3\uff09\u4e2d\u7684\u4f4d\u56fe\u53e5\u67c4\uff08Handle to a Bitmap\uff09\uff0c\u5728\u4f7f\u7528C++\u7248\u672cFoxit PDF SDK\u5f00\u53d1PDF\u663e\u793a\u9884\u89c8\u7684\u56fe\u5f62\u754c\u9762\u65f6\uff0c\u7ecf\u5e38\u4f1a\u7528\u5230\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u798f\u6615SDK\u7684 C++ \u7248\u672c\u7684Bitmap\u7c7b\u578b\uff0c\u8f6c\u6210HBITMAP\u7684\u793a\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;windows.h&gt;\n#include &lt;stdexcept&gt;\n#include &lt;cstdint&gt;\n#include &lt;algorithm&gt;  \n\nHBITMAP BitmapToHBITMAP( Bitmap&amp; bitmap) {\n\n    if (bitmap.GetWidth() &lt;= 0 || bitmap.GetHeight() &lt;= 0) {\n        throw std::invalid_argument(\"Invalid bitmap width or height\");\n    }\n    int bpp = bitmap.GetBpp();\n    int bmp_w = bitmap.GetWidth();\n    int bmp_h = bitmap.GetHeight();\n    if (bpp != 8 &amp;&amp; bpp != 24 &amp;&amp; bpp != 32) {\n        throw std::invalid_argument(\"Unsupported BPP: only 8, 24, 32 are allowed\");\n    }\n\n    const uint8_t* bmpBuffer = bitmap.GetBuffer();\n    if (!bmpBuffer) {\n        throw std::runtime_error(\"Bitmap buffer is null\");\n    }\n\n    BITMAPINFO bmi;\n    FillBitmapInfo(bmi, bmp_w, bmp_h, bpp);\n\n    \/\/ \u83b7\u53d6\u6bcf\u4e2a\u50cf\u7d20\u7684\u901a\u9053\u6570\n    int channels = 0;\n    if (bpp == 8) {\n        channels = 1;\n    }\n    else if (bpp == 24) {\n        channels = 3;\n    }\n    else if (bpp == 32) {\n        channels = 4;\n    }\n    int rowValidBytes = bmp_w * channels; \n\n    BYTE* pDibBits = nullptr;\n    HBITMAP hBitmap = CreateDIBSection(\n        nullptr,           \n        &amp;bmi,                 \n        DIB_RGB_COLORS,        \n        reinterpret_cast&lt;void**&gt;(&amp;pDibBits), \n        nullptr,               \n        0                      \n    );\n\n    if (!hBitmap || !pDibBits) {\n        throw std::runtime_error(\"Failed to create DIBSection\");\n    }\n\n    const BYTE* pSrc = reinterpret_cast&lt;const BYTE*&gt;(bmpBuffer);\n    for (int row = 0; row &lt; bmp_h; row++) {\n        pSrc = reinterpret_cast&lt;const BYTE*&gt;(bmpBuffer) + row * bitmap.GetPitch();\n        BYTE* pDst = &amp;pDibBits&#091;row * rowValidBytes];\n\n        \/\/ \u590d\u5236\u6709\u6548\u50cf\u7d20\u6570\u636e\n        memcpy(pDst, pSrc, rowValidBytes);\n        if (bpp == 24) {\n            for (int col = 0; col &lt; bmp_w; col++) {\n                std::swap(pDst&#091;col * 3 + 0], pDst&#091;col * 3 + 2]);\n            }\n        }\n        else if (bpp == 32) {\n            for (int col = 0; col &lt; bmp_w; col++) {\n                std::swap(pDst&#091;col * 4 + 0], pDst&#091;col * 4 + 2]);\n            }\n        }\n    }\nreturn hBitmap;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HBITMAP\u662f Windows GDI\uff08\u56fe\u5f62\u8bbe\u5907\u63a5\u53e3\uff09\u4e2d\u7684\u4f4d\u56fe\u53e5\u67c4\uff08Handle to a Bitmap\uff09\uff0c [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[425,1241],"class_list":["post-7500","post","type-post","status-publish","format-standard","hentry","category-gsdk","tag-bitmap","tag-hbitmap"],"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","_links":{"self":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7500"}],"version-history":[{"count":1,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7500\/revisions"}],"predecessor-version":[{"id":7501,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7500\/revisions\/7501"}],"wp:attachment":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}