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

CreateBitmap

関数
指定した幅・高さ・色深度のビットマップを作成する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HBITMAP CreateBitmap(
    INT nWidth,
    INT nHeight,
    DWORD nPlanes,
    DWORD nBitCount,
    const void* lpBits   // optional
);

パラメーター

名前方向説明
nWidthINTinビットマップの幅(ピクセル単位)。
nHeightINTinビットマップの高さ(ピクセル単位)。
nPlanesDWORDinデバイスが使用するカラープレーンの数。
nBitCountDWORDin1 つのピクセルの色を識別するために必要なビット数。
lpBitsvoid*inoptional

ピクセルの矩形領域に色を設定するために使用する色データの配列へのポインター。矩形内の各走査線はワード境界に整列している必要があります(ワード境界に整列していない走査線はゼロでパディングする必要があります)。必要なバッファーサイズ cj は、次の式で計算できます。

cj = (((nWidth * nPlanes * nBitCount + 15) >> 4) << 1) * nHeight;

このパラメーターが NULL の場合、新しいビットマップの内容は未定義です。

戻り値の型: HBITMAP

公式ドキュメント

CreateBitmap 関数は、指定された幅、高さ、および色フォーマット(カラープレーン数とピクセルあたりのビット数)を持つビットマップを作成します。

戻り値

関数が成功した場合、戻り値はビットマップのハンドルです。

関数が失敗した場合、戻り値は NULL です。

この関数は、次の値を返すことがあります。

Return code Description
ERROR_INVALID_BITMAP
計算されたビットマップのサイズが 0 未満です。

解説(Remarks)

CreateBitmap 関数は、デバイス依存ビットマップを作成します。

ビットマップを作成した後は、SelectObject 関数を呼び出すことでデバイスコンテキストに選択できます。ただし、ビットマップをデバイスコンテキストに選択できるのは、ビットマップと DC が同じフォーマットを持つ場合に限られます。

CreateBitmap 関数はカラービットマップの作成に使用できます。ただし、パフォーマンス上の理由から、アプリケーションはモノクロビットマップの作成に CreateBitmap を使用し、カラービットマップの作成には CreateCompatibleBitmap を使用するべきです。CreateBitmap から返されたカラービットマップがデバイスコンテキストに選択されるたびに、システムはそのビットマップが選択先のデバイスコンテキストのフォーマットと一致するかどうかを確認します。CreateCompatibleBitmap はデバイスコンテキストを引数に取るため、指定されたデバイスコンテキストと同じフォーマットを持つビットマップを返します。したがって、その後の SelectObject の呼び出しは、CreateBitmap から返されたカラービットマップを使用する場合よりも、CreateCompatibleBitmap から得たカラービットマップを使用する場合の方が高速です。

ビットマップがモノクロの場合、ゼロは描画先デバイスコンテキストの前景色を、1 は背景色を表します。

アプリケーションが nWidth または nHeight パラメーターを 0 に設定した場合、CreateBitmap は 1 × 1 ピクセルのモノクロビットマップのハンドルを返します。

ビットマップが不要になったら、DeleteObject 関数を呼び出して削除してください。

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

各言語での呼び出し定義

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

HBITMAP CreateBitmap(
    INT nWidth,
    INT nHeight,
    DWORD nPlanes,
    DWORD nBitCount,
    const void* lpBits   // optional
);
[DllImport("GDI32.dll", ExactSpelling = true)]
static extern IntPtr CreateBitmap(
    int nWidth,   // INT
    int nHeight,   // INT
    uint nPlanes,   // DWORD
    uint nBitCount,   // DWORD
    IntPtr lpBits   // void* optional
);
<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function CreateBitmap(
    nWidth As Integer,   ' INT
    nHeight As Integer,   ' INT
    nPlanes As UInteger,   ' DWORD
    nBitCount As UInteger,   ' DWORD
    lpBits As IntPtr   ' void* optional
) As IntPtr
End Function
' nWidth : INT
' nHeight : INT
' nPlanes : DWORD
' nBitCount : DWORD
' lpBits : void* optional
Declare PtrSafe Function CreateBitmap Lib "gdi32" ( _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal nPlanes As Long, _
    ByVal nBitCount As Long, _
    ByVal lpBits As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateBitmap = ctypes.windll.gdi32.CreateBitmap
CreateBitmap.restype = ctypes.c_void_p
CreateBitmap.argtypes = [
    ctypes.c_int,  # nWidth : INT
    ctypes.c_int,  # nHeight : INT
    wintypes.DWORD,  # nPlanes : DWORD
    wintypes.DWORD,  # nBitCount : DWORD
    ctypes.POINTER(None),  # lpBits : void* optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
CreateBitmap = Fiddle::Function.new(
  lib['CreateBitmap'],
  [
    Fiddle::TYPE_INT,  # nWidth : INT
    Fiddle::TYPE_INT,  # nHeight : INT
    -Fiddle::TYPE_INT,  # nPlanes : DWORD
    -Fiddle::TYPE_INT,  # nBitCount : DWORD
    Fiddle::TYPE_VOIDP,  # lpBits : void* optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "gdi32")]
extern "system" {
    fn CreateBitmap(
        nWidth: i32,  // INT
        nHeight: i32,  // INT
        nPlanes: u32,  // DWORD
        nBitCount: u32,  // DWORD
        lpBits: *const ()  // void* optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll")]
public static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint nPlanes, uint nBitCount, IntPtr lpBits);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_CreateBitmap' -Namespace Win32 -PassThru
# $api::CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
#uselib "GDI32.dll"
#func global CreateBitmap "CreateBitmap" sptr, sptr, sptr, sptr, sptr
; CreateBitmap nWidth, nHeight, nPlanes, nBitCount, lpBits   ; 戻り値は stat
; nWidth : INT -> "sptr"
; nHeight : INT -> "sptr"
; nPlanes : DWORD -> "sptr"
; nBitCount : DWORD -> "sptr"
; lpBits : void* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "GDI32.dll"
#cfunc global CreateBitmap "CreateBitmap" int, int, int, int, sptr
; res = CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
; nWidth : INT -> "int"
; nHeight : INT -> "int"
; nPlanes : DWORD -> "int"
; nBitCount : DWORD -> "int"
; lpBits : void* optional -> "sptr"
; HBITMAP CreateBitmap(INT nWidth, INT nHeight, DWORD nPlanes, DWORD nBitCount, void* lpBits)
#uselib "GDI32.dll"
#cfunc global CreateBitmap "CreateBitmap" int, int, int, int, intptr
; res = CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
; nWidth : INT -> "int"
; nHeight : INT -> "int"
; nPlanes : DWORD -> "int"
; nBitCount : DWORD -> "int"
; lpBits : void* optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procCreateBitmap = gdi32.NewProc("CreateBitmap")
)

// nWidth (INT), nHeight (INT), nPlanes (DWORD), nBitCount (DWORD), lpBits (void* optional)
r1, _, err := procCreateBitmap.Call(
	uintptr(nWidth),
	uintptr(nHeight),
	uintptr(nPlanes),
	uintptr(nBitCount),
	uintptr(lpBits),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HBITMAP
function CreateBitmap(
  nWidth: Integer;   // INT
  nHeight: Integer;   // INT
  nPlanes: DWORD;   // DWORD
  nBitCount: DWORD;   // DWORD
  lpBits: Pointer   // void* optional
): THandle; stdcall;
  external 'GDI32.dll' name 'CreateBitmap';
result := DllCall("GDI32\CreateBitmap"
    , "Int", nWidth   ; INT
    , "Int", nHeight   ; INT
    , "UInt", nPlanes   ; DWORD
    , "UInt", nBitCount   ; DWORD
    , "Ptr", lpBits   ; void* optional
    , "Ptr")   ; return: HBITMAP
●CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits) = DLL("GDI32.dll", "void* CreateBitmap(int, int, dword, dword, void*)")
# 呼び出し: CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
# nWidth : INT -> "int"
# nHeight : INT -> "int"
# nPlanes : DWORD -> "dword"
# nBitCount : DWORD -> "dword"
# lpBits : void* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "gdi32" fn CreateBitmap(
    nWidth: i32, // INT
    nHeight: i32, // INT
    nPlanes: u32, // DWORD
    nBitCount: u32, // DWORD
    lpBits: ?*anyopaque // void* optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc CreateBitmap(
    nWidth: int32,  # INT
    nHeight: int32,  # INT
    nPlanes: uint32,  # DWORD
    nBitCount: uint32,  # DWORD
    lpBits: pointer  # void* optional
): pointer {.importc: "CreateBitmap", stdcall, dynlib: "GDI32.dll".}
pragma(lib, "gdi32");
extern(Windows)
void* CreateBitmap(
    int nWidth,   // INT
    int nHeight,   // INT
    uint nPlanes,   // DWORD
    uint nBitCount,   // DWORD
    void* lpBits   // void* optional
);
ccall((:CreateBitmap, "GDI32.dll"), stdcall, Ptr{Cvoid},
      (Int32, Int32, UInt32, UInt32, Ptr{Cvoid}),
      nWidth, nHeight, nPlanes, nBitCount, lpBits)
# nWidth : INT -> Int32
# nHeight : INT -> Int32
# nPlanes : DWORD -> UInt32
# nBitCount : DWORD -> UInt32
# lpBits : void* optional -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* CreateBitmap(
    int32_t nWidth,
    int32_t nHeight,
    uint32_t nPlanes,
    uint32_t nBitCount,
    void* lpBits);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
-- nWidth : INT
-- nHeight : INT
-- nPlanes : DWORD
-- nBitCount : DWORD
-- lpBits : void* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const CreateBitmap = lib.func('__stdcall', 'CreateBitmap', 'void *', ['int32_t', 'int32_t', 'uint32_t', 'uint32_t', 'void *']);
// CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
// nWidth : INT -> 'int32_t'
// nHeight : INT -> 'int32_t'
// nPlanes : DWORD -> 'uint32_t'
// nBitCount : DWORD -> 'uint32_t'
// lpBits : void* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  CreateBitmap: { parameters: ["i32", "i32", "u32", "u32", "pointer"], result: "pointer" },
});
// lib.symbols.CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits)
// nWidth : INT -> "i32"
// nHeight : INT -> "i32"
// nPlanes : DWORD -> "u32"
// nBitCount : DWORD -> "u32"
// lpBits : void* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* CreateBitmap(
    int32_t nWidth,
    int32_t nHeight,
    uint32_t nPlanes,
    uint32_t nBitCount,
    void* lpBits);
C, "GDI32.dll");
// $ffi->CreateBitmap(nWidth, nHeight, nPlanes, nBitCount, lpBits);
// nWidth : INT
// nHeight : INT
// nPlanes : DWORD
// nBitCount : DWORD
// lpBits : void* optional
// 構造体/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 CreateBitmap(
        int nWidth,   // INT
        int nHeight,   // INT
        int nPlanes,   // DWORD
        int nBitCount,   // DWORD
        Pointer lpBits   // void* optional
    );
}
@[Link("gdi32")]
lib LibGDI32
  fun CreateBitmap = CreateBitmap(
    nWidth : Int32,   # INT
    nHeight : Int32,   # INT
    nPlanes : UInt32,   # DWORD
    nBitCount : UInt32,   # DWORD
    lpBits : Void*   # void* optional
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CreateBitmapNative = Pointer<Void> Function(Int32, Int32, Uint32, Uint32, Pointer<Void>);
typedef CreateBitmapDart = Pointer<Void> Function(int, int, int, int, Pointer<Void>);
final CreateBitmap = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<CreateBitmapNative, CreateBitmapDart>('CreateBitmap');
// nWidth : INT -> Int32
// nHeight : INT -> Int32
// nPlanes : DWORD -> Uint32
// nBitCount : DWORD -> Uint32
// lpBits : void* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateBitmap(
  nWidth: Integer;   // INT
  nHeight: Integer;   // INT
  nPlanes: DWORD;   // DWORD
  nBitCount: DWORD;   // DWORD
  lpBits: Pointer   // void* optional
): THandle; stdcall;
  external 'GDI32.dll' name 'CreateBitmap';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateBitmap"
  c_CreateBitmap :: Int32 -> Int32 -> Word32 -> Word32 -> Ptr () -> IO (Ptr ())
-- nWidth : INT -> Int32
-- nHeight : INT -> Int32
-- nPlanes : DWORD -> Word32
-- nBitCount : DWORD -> Word32
-- lpBits : void* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createbitmap =
  foreign "CreateBitmap"
    (int32_t @-> int32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning (ptr void))
(* nWidth : INT -> int32_t *)
(* nHeight : INT -> int32_t *)
(* nPlanes : DWORD -> uint32_t *)
(* nBitCount : DWORD -> uint32_t *)
(* lpBits : void* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("CreateBitmap" create-bitmap :convention :stdcall) :pointer
  (n-width :int32)   ; INT
  (n-height :int32)   ; INT
  (n-planes :uint32)   ; DWORD
  (n-bit-count :uint32)   ; DWORD
  (lp-bits :pointer))   ; void* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateBitmap = Win32::API::More->new('GDI32',
    'HANDLE CreateBitmap(int nWidth, int nHeight, DWORD nPlanes, DWORD nBitCount, LPVOID lpBits)');
# my $ret = $CreateBitmap->Call($nWidth, $nHeight, $nPlanes, $nBitCount, $lpBits);
# nWidth : INT -> int
# nHeight : INT -> int
# nPlanes : DWORD -> DWORD
# nBitCount : DWORD -> DWORD
# lpBits : void* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目