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

GetPixel

関数
指定座標のピクセルの色を取得する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

COLORREF GetPixel(
    HDC hdc,
    INT x,
    INT y
);

パラメーター

名前方向
hdcHDCin
xINTin
yINTin

戻り値の型: COLORREF

各言語での呼び出し定義

// 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   // COLORREF
function 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)。