Private Const CF_BITMAP As Long = 2 ' // GDI+関係 Private Declare Function GdiplusStartup Lib "gdiplus" ( _ ByRef token As Long, _ ByRef inputBuf As GdiplusStartupInput, _ ByVal outputBuf As Long) As Long Private Declare Sub GdiplusShutdown Lib "gdiplus" ( _ ByVal token As Long) Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus" ( _ ByVal hbm As Long, _ ByVal hpal As Long, _ ByRef bitmap As Long) As Long Private Declare Function GdipDisposeImage Lib "gdiplus" ( _ ByVal image As Long) As Long 'Imageの寸法取得 Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal image As Long, Width As Long) As Long Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal image As Long, Height As Long) As Long Private Declare Function GdipBitmapGetPixel Lib "gdiplus" (ByVal bitmap As LongPtr, ByVal x As Long, ByVal y As Long, ByRef color As Long) As Long Private Type GdiplusStartupInput GdiplusVersion As Long ' UINT32 GdiplusVersion DebugEventCallback As Long ' DebugEventProc DebugEventCallback SuppressBackgroundThread As Long ' BOOL SuppressBackgroundThread SuppressExternalCodecs As Long ' BOOL SuppressExternalCodecs End Type Private Type GUID Data1 As Long ' unsigned long Data1 Data2 As Integer ' unsigned short Data2 Data3 As Integer ' unsigned short Data3 Data4(7) As Byte ' unsigned char Data4[8] End Type Sub Sample() Dim shp As Shape Dim hBmp As OLE_HANDLE ' GDI+ を初期化する If GDIplus_Initialize() = False Then MsgBox "GDI+ を初期化できません", vbCritical Exit Sub End If ActiveSheet.Pictures.Insert( _ "C:\hogehoge\TEST.png").Select Selection.ShapeRange.Left = 0 Selection.ShapeRange.Top = 0 Range(Cells(1, 1), Cells(12, 5)).Select Call Selection.CopyPicture(xlScreen, xlBitmap) hBmp = pvGetHBitmapFromClipboard() If hBmp = 0 Then MsgBox "a3" End If Ret = GdipCreateBitmapFromHBITMAP(hBmp, 0&, GdipBmpHdl) GdipGetImageWidth GdipBmpHdl, lngWidth GdipGetImageHeight GdipBmpHdl, lngHeight Call GdipBitmapGetPixel(GdipBmpHdl, 0, 0, color) R = Int(color / 256 / 256) G = Int(color / 256) Mod 256 B = color Mod 256 h = R * 256 * 256 + G * 256 + B MsgBox R & ";" & G & ";" & B & ";" & lngWidth & ";" & lngHeight & ";" & h Call Gdiplus_Shutdown End Sub ' // GDI+ 初期化 Private Function GDIplus_Initialize() As Boolean Dim uGdiStartupInput As GdiplusStartupInput Dim nStatus As Long If m_GDIplusToken Then Call Gdiplus_Shutdown With uGdiStartupInput .GdiplusVersion = 1 .DebugEventCallback = 0 .SuppressBackgroundThread = 0 .SuppressExternalCodecs = 0 End With nStatus = GdiplusStartup(m_GDIplusToken, uGdiStartupInput, 0&) GDIplus_Initialize = CBool(nStatus = 0) End Function ' // GDI+ 終了 Private Function Gdiplus_Shutdown() As Long If m_GDIplusToken Then Call GdiplusShutdown(m_GDIplusToken) m_GDIplusToken = 0 End If End Function ' // クリップボード hBitmap を取得する Private Function pvGetHBitmapFromClipboard() As OLE_HANDLE If OpenClipboard(0&) <> 0 Then pvGetHBitmapFromClipboard = GetClipboardData(CF_BITMAP) Call CloseClipboard Else pvGetHBitmapFromClipboard = 0 End If End Function