ホーム › Graphics.Gdi › GetPixel
GetPixel
関数指定座標のピクセルの色を取得する。
シグネチャ
// GDI32.dll
#include <windows.h>
COLORREF GetPixel(
HDC hdc,
INT x,
INT y
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdc | HDC | in | デバイス コンテキストへのハンドル。 |
| x | INT | in | 検査するピクセルの x 座標 (論理単位)。 |
| y | INT | in | 検査するピクセルの y 座標 (論理単位)。 |
戻り値の型: COLORREF
公式ドキュメント
GetPixel 関数は、指定された座標のピクセルの赤、緑、青 (RGB) のカラー値を取得します。
戻り値
戻り値は、ピクセルの RGB を指定する COLORREF 値です。ピクセルが現在のクリッピング領域の外にある場合、戻り値は CLR_INVALID (Wingdi.h で定義されている 0xFFFFFFFF) になります。
解説(Remarks)
ピクセルは現在のクリッピング領域の境界内になければなりません。
すべてのデバイスが GetPixel をサポートしているわけではありません。アプリケーションは、指定したデバイスがこの関数をサポートしているかどうかを判断するために GetDeviceCaps を呼び出す必要があります。
デバイス コンテキスト内にビットマップが選択されている必要があります。選択されていない場合、すべてのピクセルに対して CLR_INVALID が返されます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// GDI32.dll
#include <windows.h>
COLORREF GetPixel(
HDC hdc,
INT x,
INT y
);[DllImport("GDI32.dll", ExactSpelling = true)]
static extern uint GetPixel(
IntPtr hdc, // HDC
int x, // INT
int y // INT
);<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function GetPixel(
hdc As IntPtr, ' HDC
x As Integer, ' INT
y As Integer ' INT
) As UInteger
End Function' hdc : HDC
' x : INT
' y : INT
Declare PtrSafe Function GetPixel Lib "gdi32" ( _
ByVal hdc As LongPtr, _
ByVal x As Long, _
ByVal y As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetPixel = ctypes.windll.gdi32.GetPixel
GetPixel.restype = wintypes.DWORD
GetPixel.argtypes = [
wintypes.HANDLE, # hdc : HDC
ctypes.c_int, # x : INT
ctypes.c_int, # y : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('GDI32.dll')
GetPixel = Fiddle::Function.new(
lib['GetPixel'],
[
Fiddle::TYPE_VOIDP, # hdc : HDC
Fiddle::TYPE_INT, # x : INT
Fiddle::TYPE_INT, # y : INT
],
-Fiddle::TYPE_INT)#[link(name = "gdi32")]
extern "system" {
fn GetPixel(
hdc: *mut core::ffi::c_void, // HDC
x: i32, // INT
y: i32 // INT
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("GDI32.dll")]
public static extern uint GetPixel(IntPtr hdc, int x, int y);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetPixel' -Namespace Win32 -PassThru
# $api::GetPixel(hdc, x, y)#uselib "GDI32.dll"
#func global GetPixel "GetPixel" sptr, sptr, sptr
; GetPixel hdc, x, y ; 戻り値は stat
; hdc : HDC -> "sptr"
; x : INT -> "sptr"
; y : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "GDI32.dll"
#cfunc global GetPixel "GetPixel" sptr, int, int
; res = GetPixel(hdc, x, y)
; hdc : HDC -> "sptr"
; x : INT -> "int"
; y : INT -> "int"; COLORREF GetPixel(HDC hdc, INT x, INT y)
#uselib "GDI32.dll"
#cfunc global GetPixel "GetPixel" intptr, int, int
; res = GetPixel(hdc, x, y)
; hdc : HDC -> "intptr"
; x : INT -> "int"
; y : INT -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
gdi32 = windows.NewLazySystemDLL("GDI32.dll")
procGetPixel = gdi32.NewProc("GetPixel")
)
// hdc (HDC), x (INT), y (INT)
r1, _, err := procGetPixel.Call(
uintptr(hdc),
uintptr(x),
uintptr(y),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // COLORREFfunction GetPixel(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer // INT
): DWORD; stdcall;
external 'GDI32.dll' name 'GetPixel';result := DllCall("GDI32\GetPixel"
, "Ptr", hdc ; HDC
, "Int", x ; INT
, "Int", y ; INT
, "UInt") ; return: COLORREF●GetPixel(hdc, x, y) = DLL("GDI32.dll", "dword GetPixel(void*, int, int)")
# 呼び出し: GetPixel(hdc, x, y)
# hdc : HDC -> "void*"
# x : INT -> "int"
# y : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "gdi32" fn GetPixel(
hdc: ?*anyopaque, // HDC
x: i32, // INT
y: i32 // INT
) callconv(std.os.windows.WINAPI) u32;proc GetPixel(
hdc: pointer, # HDC
x: int32, # INT
y: int32 # INT
): uint32 {.importc: "GetPixel", stdcall, dynlib: "GDI32.dll".}pragma(lib, "gdi32");
extern(Windows)
uint GetPixel(
void* hdc, // HDC
int x, // INT
int y // INT
);ccall((:GetPixel, "GDI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Int32, Int32),
hdc, x, y)
# hdc : HDC -> Ptr{Cvoid}
# x : INT -> Int32
# y : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetPixel(
void* hdc,
int32_t x,
int32_t y);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.GetPixel(hdc, x, y)
-- hdc : HDC
-- x : INT
-- y : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const GetPixel = lib.func('__stdcall', 'GetPixel', 'uint32_t', ['void *', 'int32_t', 'int32_t']);
// GetPixel(hdc, x, y)
// hdc : HDC -> 'void *'
// x : INT -> 'int32_t'
// y : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("GDI32.dll", {
GetPixel: { parameters: ["pointer", "i32", "i32"], result: "u32" },
});
// lib.symbols.GetPixel(hdc, x, y)
// hdc : HDC -> "pointer"
// x : INT -> "i32"
// y : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetPixel(
void* hdc,
int32_t x,
int32_t y);
C, "GDI32.dll");
// $ffi->GetPixel(hdc, x, y);
// hdc : HDC
// x : INT
// y : INT
// 構造体/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);
int GetPixel(
Pointer hdc, // HDC
int x, // INT
int y // INT
);
}@[Link("gdi32")]
lib LibGDI32
fun GetPixel = GetPixel(
hdc : Void*, # HDC
x : Int32, # INT
y : Int32 # INT
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetPixelNative = Uint32 Function(Pointer<Void>, Int32, Int32);
typedef GetPixelDart = int Function(Pointer<Void>, int, int);
final GetPixel = DynamicLibrary.open('GDI32.dll')
.lookupFunction<GetPixelNative, GetPixelDart>('GetPixel');
// hdc : HDC -> Pointer<Void>
// x : INT -> Int32
// y : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetPixel(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer // INT
): DWORD; stdcall;
external 'GDI32.dll' name 'GetPixel';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetPixel"
c_GetPixel :: Ptr () -> Int32 -> Int32 -> IO Word32
-- hdc : HDC -> Ptr ()
-- x : INT -> Int32
-- y : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getpixel =
foreign "GetPixel"
((ptr void) @-> int32_t @-> int32_t @-> returning uint32_t)
(* hdc : HDC -> (ptr void) *)
(* x : INT -> int32_t *)
(* y : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)
(cffi:defcfun ("GetPixel" get-pixel :convention :stdcall) :uint32
(hdc :pointer) ; HDC
(x :int32) ; INT
(y :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetPixel = Win32::API::More->new('GDI32',
'DWORD GetPixel(HANDLE hdc, int x, int y)');
# my $ret = $GetPixel->Call($hdc, $x, $y);
# hdc : HDC -> HANDLE
# x : INT -> int
# y : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f GetDeviceCaps — デバイスの能力や属性情報を取得する。
- f SetPixel — 指定座標のピクセルを指定色で描画し、実際の色を返す。