Win32 API 日本語リファレンス
ホームGraphics.Gdi › CreateDIBSection

CreateDIBSection

関数
アプリが直接アクセス可能なメモリを持つDIBセクションを作成する。
DLLGDI32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// GDI32.dll
#include <windows.h>

HBITMAP CreateDIBSection(
    HDC hdc,   // optional
    const BITMAPINFO* pbmi,
    DIB_USAGE usage,
    void** ppvBits,
    HANDLE hSection,   // optional
    DWORD offset
);

パラメーター

名前方向説明
hdcHDCinoptionalデバイス コンテキストへのハンドル。iUsage の値が DIB_PAL_COLORS の場合、関数はこのデバイス コンテキストの論理パレットを使用して DIB の色を初期化します。
pbmiBITMAPINFO*inビットマップの寸法や色など、DIB のさまざまな属性を指定する BITMAPINFO 構造体へのポインター。
usageDIB_USAGEin

pbmi が指す BITMAPINFO 構造体の bmiColors 配列メンバーに格納されるデータの種類(論理パレット インデックスまたはリテラルな RGB 値のいずれか)。次の値が定義されています。

意味
DIB_PAL_COLORS
bmiColors メンバーは、hdc で指定されたデバイス コンテキストの論理パレットへの 16 ビット インデックスの配列です。
DIB_RGB_COLORS
BITMAPINFO 構造体は、リテラルな RGB 値の配列を含みます。
ppvBitsvoid**outDIB ビット値の位置へのポインターを受け取る変数へのポインター。
hSectionHANDLEinoptional

関数が DIB の作成に使用するファイル マッピング オブジェクトへのハンドル。このパラメーターは NULL にできます。

hSectionNULL でない場合は、PAGE_READWRITE または PAGE_WRITECOPY フラグを指定して CreateFileMapping 関数を呼び出して作成されたファイル マッピング オブジェクトへのハンドルでなければなりません。読み取り専用の DIB セクションはサポートされていません。他の方法で作成されたハンドルを指定すると、CreateDIBSection は失敗します。

hSectionNULL でない場合、CreateDIBSection 関数は、hSection が参照するファイル マッピング オブジェクト内のオフセット dwOffset の位置にビットマップのビット値を配置します。アプリケーションは後で、CreateDIBSection が返した HBITMAP を指定して GetObject 関数を呼び出すことで、hSection ハンドルを取得できます。

hSectionNULL の場合、システムが DIB 用のメモリを割り当てます。この場合、CreateDIBSection 関数は dwOffset パラメーターを無視します。アプリケーションは後でこのメモリへのハンドルを取得することはできません。GetObject 関数を呼び出して設定される DIBSECTION 構造体の dshSection メンバーは NULL になります。

offsetDWORDinhSection が参照するファイル マッピング オブジェクトの先頭から、ビットマップのビット値の格納を開始する位置までのオフセット。hSectionNULL の場合、この値は無視されます。ビットマップのビット値はダブルワード境界に整列されるため、dwOffsetDWORD のサイズの倍数でなければなりません。

戻り値の型: HBITMAP

公式ドキュメント

CreateDIBSection 関数は、アプリケーションが直接書き込める DIB を作成します。

戻り値

関数が成功した場合、戻り値は新しく作成された DIB へのハンドルであり、*ppvBits はビットマップのビット値を指します。

関数が失敗した場合、戻り値は NULL であり、*ppvBitsNULL になります。拡張エラー情報を取得するには、GetLastError を呼び出します。

GetLastError は次の値を返すことがあります。

エラー コード 説明
ERROR_INVALID_PARAMETER
1 つ以上の入力パラメーターが無効です。

解説(Remarks)

前述のとおり、hSectionNULL の場合、システムが DIB 用のメモリを割り当てます。後で DeleteObject 関数を呼び出して DIB を削除すると、システムがそのメモリへのハンドルを閉じます。hSectionNULL でない場合は、ビットマップを削除するために DeleteObject を呼び出した後、hSection のメモリ ハンドルを自分で閉じる必要があります。

DIB セクションをあるアプリケーションから別のアプリケーションへ貼り付けることはできません。

CreateDIBSectionBITMAPINFOHEADER のパラメーター biXPelsPerMeter および biYPelsPerMeter を使用せず、BITMAPINFO 構造体に解像度情報を提供しません。

CreateDIBSection で作成したビットマップに自分で描画する前に、GDI サブシステムがそのビットマップへの描画をすべて完了していることを保証する必要があります。ビットマップへのアクセスは同期しなければなりません。これは GdiFlush 関数を呼び出すことで行います。これは、ビットマップのビット値へのポインターを使用するあらゆる場合に適用され、SetDIBits などの関数の呼び出しでポインターを渡す場合も含まれます。

ICM: カラー マネジメントは行われません。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// GDI32.dll
#include <windows.h>

HBITMAP CreateDIBSection(
    HDC hdc,   // optional
    const BITMAPINFO* pbmi,
    DIB_USAGE usage,
    void** ppvBits,
    HANDLE hSection,   // optional
    DWORD offset
);
[DllImport("GDI32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateDIBSection(
    IntPtr hdc,   // HDC optional
    IntPtr pbmi,   // BITMAPINFO*
    uint usage,   // DIB_USAGE
    IntPtr ppvBits,   // void** out
    IntPtr hSection,   // HANDLE optional
    uint offset   // DWORD
);
<DllImport("GDI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateDIBSection(
    hdc As IntPtr,   ' HDC optional
    pbmi As IntPtr,   ' BITMAPINFO*
    usage As UInteger,   ' DIB_USAGE
    ppvBits As IntPtr,   ' void** out
    hSection As IntPtr,   ' HANDLE optional
    offset As UInteger   ' DWORD
) As IntPtr
End Function
' hdc : HDC optional
' pbmi : BITMAPINFO*
' usage : DIB_USAGE
' ppvBits : void** out
' hSection : HANDLE optional
' offset : DWORD
Declare PtrSafe Function CreateDIBSection Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal pbmi As LongPtr, _
    ByVal usage As Long, _
    ByVal ppvBits As LongPtr, _
    ByVal hSection As LongPtr, _
    ByVal offset As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateDIBSection = ctypes.windll.gdi32.CreateDIBSection
CreateDIBSection.restype = ctypes.c_void_p
CreateDIBSection.argtypes = [
    wintypes.HANDLE,  # hdc : HDC optional
    ctypes.c_void_p,  # pbmi : BITMAPINFO*
    wintypes.DWORD,  # usage : DIB_USAGE
    ctypes.c_void_p,  # ppvBits : void** out
    wintypes.HANDLE,  # hSection : HANDLE optional
    wintypes.DWORD,  # offset : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
CreateDIBSection = Fiddle::Function.new(
  lib['CreateDIBSection'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC optional
    Fiddle::TYPE_VOIDP,  # pbmi : BITMAPINFO*
    -Fiddle::TYPE_INT,  # usage : DIB_USAGE
    Fiddle::TYPE_VOIDP,  # ppvBits : void** out
    Fiddle::TYPE_VOIDP,  # hSection : HANDLE optional
    -Fiddle::TYPE_INT,  # offset : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "gdi32")]
extern "system" {
    fn CreateDIBSection(
        hdc: *mut core::ffi::c_void,  // HDC optional
        pbmi: *const BITMAPINFO,  // BITMAPINFO*
        usage: u32,  // DIB_USAGE
        ppvBits: *mut *mut (),  // void** out
        hSection: *mut core::ffi::c_void,  // HANDLE optional
        offset: u32  // DWORD
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll", SetLastError = true)]
public static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr pbmi, uint usage, IntPtr ppvBits, IntPtr hSection, uint offset);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_CreateDIBSection' -Namespace Win32 -PassThru
# $api::CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
#uselib "GDI32.dll"
#func global CreateDIBSection "CreateDIBSection" sptr, sptr, sptr, sptr, sptr, sptr
; CreateDIBSection hdc, varptr(pbmi), usage, ppvBits, hSection, offset   ; 戻り値は stat
; hdc : HDC optional -> "sptr"
; pbmi : BITMAPINFO* -> "sptr"
; usage : DIB_USAGE -> "sptr"
; ppvBits : void** out -> "sptr"
; hSection : HANDLE optional -> "sptr"
; offset : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global CreateDIBSection "CreateDIBSection" sptr, var, int, sptr, sptr, int
; res = CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
; hdc : HDC optional -> "sptr"
; pbmi : BITMAPINFO* -> "var"
; usage : DIB_USAGE -> "int"
; ppvBits : void** out -> "sptr"
; hSection : HANDLE optional -> "sptr"
; offset : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HBITMAP CreateDIBSection(HDC hdc, BITMAPINFO* pbmi, DIB_USAGE usage, void** ppvBits, HANDLE hSection, DWORD offset)
#uselib "GDI32.dll"
#cfunc global CreateDIBSection "CreateDIBSection" intptr, var, int, intptr, intptr, int
; res = CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
; hdc : HDC optional -> "intptr"
; pbmi : BITMAPINFO* -> "var"
; usage : DIB_USAGE -> "int"
; ppvBits : void** out -> "intptr"
; hSection : HANDLE optional -> "intptr"
; offset : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procCreateDIBSection = gdi32.NewProc("CreateDIBSection")
)

// hdc (HDC optional), pbmi (BITMAPINFO*), usage (DIB_USAGE), ppvBits (void** out), hSection (HANDLE optional), offset (DWORD)
r1, _, err := procCreateDIBSection.Call(
	uintptr(hdc),
	uintptr(pbmi),
	uintptr(usage),
	uintptr(ppvBits),
	uintptr(hSection),
	uintptr(offset),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HBITMAP
function CreateDIBSection(
  hdc: THandle;   // HDC optional
  pbmi: Pointer;   // BITMAPINFO*
  usage: DWORD;   // DIB_USAGE
  ppvBits: Pointer;   // void** out
  hSection: THandle;   // HANDLE optional
  offset: DWORD   // DWORD
): THandle; stdcall;
  external 'GDI32.dll' name 'CreateDIBSection';
result := DllCall("GDI32\CreateDIBSection"
    , "Ptr", hdc   ; HDC optional
    , "Ptr", pbmi   ; BITMAPINFO*
    , "UInt", usage   ; DIB_USAGE
    , "Ptr", ppvBits   ; void** out
    , "Ptr", hSection   ; HANDLE optional
    , "UInt", offset   ; DWORD
    , "Ptr")   ; return: HBITMAP
●CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset) = DLL("GDI32.dll", "void* CreateDIBSection(void*, void*, dword, void*, void*, dword)")
# 呼び出し: CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
# hdc : HDC optional -> "void*"
# pbmi : BITMAPINFO* -> "void*"
# usage : DIB_USAGE -> "dword"
# ppvBits : void** out -> "void*"
# hSection : HANDLE optional -> "void*"
# offset : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "gdi32" fn CreateDIBSection(
    hdc: ?*anyopaque, // HDC optional
    pbmi: [*c]BITMAPINFO, // BITMAPINFO*
    usage: u32, // DIB_USAGE
    ppvBits: ?*anyopaque, // void** out
    hSection: ?*anyopaque, // HANDLE optional
    offset: u32 // DWORD
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc CreateDIBSection(
    hdc: pointer,  # HDC optional
    pbmi: ptr BITMAPINFO,  # BITMAPINFO*
    usage: uint32,  # DIB_USAGE
    ppvBits: pointer,  # void** out
    hSection: pointer,  # HANDLE optional
    offset: uint32  # DWORD
): pointer {.importc: "CreateDIBSection", stdcall, dynlib: "GDI32.dll".}
pragma(lib, "gdi32");
extern(Windows)
void* CreateDIBSection(
    void* hdc,   // HDC optional
    BITMAPINFO* pbmi,   // BITMAPINFO*
    uint usage,   // DIB_USAGE
    void** ppvBits,   // void** out
    void* hSection,   // HANDLE optional
    uint offset   // DWORD
);
ccall((:CreateDIBSection, "GDI32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Ptr{BITMAPINFO}, UInt32, Ptr{Cvoid}, Ptr{Cvoid}, UInt32),
      hdc, pbmi, usage, ppvBits, hSection, offset)
# hdc : HDC optional -> Ptr{Cvoid}
# pbmi : BITMAPINFO* -> Ptr{BITMAPINFO}
# usage : DIB_USAGE -> UInt32
# ppvBits : void** out -> Ptr{Cvoid}
# hSection : HANDLE optional -> Ptr{Cvoid}
# offset : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* CreateDIBSection(
    void* hdc,
    void* pbmi,
    uint32_t usage,
    void** ppvBits,
    void* hSection,
    uint32_t offset);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
-- hdc : HDC optional
-- pbmi : BITMAPINFO*
-- usage : DIB_USAGE
-- ppvBits : void** out
-- hSection : HANDLE optional
-- offset : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const CreateDIBSection = lib.func('__stdcall', 'CreateDIBSection', 'void *', ['void *', 'void *', 'uint32_t', 'void *', 'void *', 'uint32_t']);
// CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
// hdc : HDC optional -> 'void *'
// pbmi : BITMAPINFO* -> 'void *'
// usage : DIB_USAGE -> 'uint32_t'
// ppvBits : void** out -> 'void *'
// hSection : HANDLE optional -> 'void *'
// offset : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  CreateDIBSection: { parameters: ["pointer", "pointer", "u32", "pointer", "pointer", "u32"], result: "pointer" },
});
// lib.symbols.CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset)
// hdc : HDC optional -> "pointer"
// pbmi : BITMAPINFO* -> "pointer"
// usage : DIB_USAGE -> "u32"
// ppvBits : void** out -> "pointer"
// hSection : HANDLE optional -> "pointer"
// offset : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* CreateDIBSection(
    void* hdc,
    void* pbmi,
    uint32_t usage,
    void** ppvBits,
    void* hSection,
    uint32_t offset);
C, "GDI32.dll");
// $ffi->CreateDIBSection(hdc, pbmi, usage, ppvBits, hSection, offset);
// hdc : HDC optional
// pbmi : BITMAPINFO*
// usage : DIB_USAGE
// ppvBits : void** out
// hSection : HANDLE optional
// offset : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Gdi32 extends StdCallLibrary {
    Gdi32 INSTANCE = Native.load("gdi32", Gdi32.class);
    Pointer CreateDIBSection(
        Pointer hdc,   // HDC optional
        Pointer pbmi,   // BITMAPINFO*
        int usage,   // DIB_USAGE
        Pointer ppvBits,   // void** out
        Pointer hSection,   // HANDLE optional
        int offset   // DWORD
    );
}
@[Link("gdi32")]
lib LibGDI32
  fun CreateDIBSection = CreateDIBSection(
    hdc : Void*,   # HDC optional
    pbmi : BITMAPINFO*,   # BITMAPINFO*
    usage : UInt32,   # DIB_USAGE
    ppvBits : Void**,   # void** out
    hSection : Void*,   # HANDLE optional
    offset : UInt32   # DWORD
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CreateDIBSectionNative = Pointer<Void> Function(Pointer<Void>, Pointer<Void>, Uint32, Pointer<Void>, Pointer<Void>, Uint32);
typedef CreateDIBSectionDart = Pointer<Void> Function(Pointer<Void>, Pointer<Void>, int, Pointer<Void>, Pointer<Void>, int);
final CreateDIBSection = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<CreateDIBSectionNative, CreateDIBSectionDart>('CreateDIBSection');
// hdc : HDC optional -> Pointer<Void>
// pbmi : BITMAPINFO* -> Pointer<Void>
// usage : DIB_USAGE -> Uint32
// ppvBits : void** out -> Pointer<Void>
// hSection : HANDLE optional -> Pointer<Void>
// offset : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateDIBSection(
  hdc: THandle;   // HDC optional
  pbmi: Pointer;   // BITMAPINFO*
  usage: DWORD;   // DIB_USAGE
  ppvBits: Pointer;   // void** out
  hSection: THandle;   // HANDLE optional
  offset: DWORD   // DWORD
): THandle; stdcall;
  external 'GDI32.dll' name 'CreateDIBSection';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateDIBSection"
  c_CreateDIBSection :: Ptr () -> Ptr () -> Word32 -> Ptr () -> Ptr () -> Word32 -> IO (Ptr ())
-- hdc : HDC optional -> Ptr ()
-- pbmi : BITMAPINFO* -> Ptr ()
-- usage : DIB_USAGE -> Word32
-- ppvBits : void** out -> Ptr ()
-- hSection : HANDLE optional -> Ptr ()
-- offset : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createdibsection =
  foreign "CreateDIBSection"
    ((ptr void) @-> (ptr void) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> uint32_t @-> returning (ptr void))
(* hdc : HDC optional -> (ptr void) *)
(* pbmi : BITMAPINFO* -> (ptr void) *)
(* usage : DIB_USAGE -> uint32_t *)
(* ppvBits : void** out -> (ptr void) *)
(* hSection : HANDLE optional -> (ptr void) *)
(* offset : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("CreateDIBSection" create-dibsection :convention :stdcall) :pointer
  (hdc :pointer)   ; HDC optional
  (pbmi :pointer)   ; BITMAPINFO*
  (usage :uint32)   ; DIB_USAGE
  (ppv-bits :pointer)   ; void** out
  (h-section :pointer)   ; HANDLE optional
  (offset :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateDIBSection = Win32::API::More->new('GDI32',
    'HANDLE CreateDIBSection(HANDLE hdc, LPVOID pbmi, DWORD usage, LPVOID ppvBits, HANDLE hSection, DWORD offset)');
# my $ret = $CreateDIBSection->Call($hdc, $pbmi, $usage, $ppvBits, $hSection, $offset);
# hdc : HDC optional -> HANDLE
# pbmi : BITMAPINFO* -> LPVOID
# usage : DIB_USAGE -> DWORD
# ppvBits : void** out -> LPVOID
# hSection : HANDLE optional -> HANDLE
# offset : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型