Voila un exemple d utiliser Gdiplus pour comparer deux images et extraire la différence en image, peut-être idéal pour la création d un logiciel pour camera de surveillance
amusez vous bien
première image
img0.jpg:
deuxième image
img.jpg
résultat
code:
Code : Tout sélectionner
EnableExplicit
;////////////////////////////////////
; Author Celtic88(c)
;///////////////////////////////////
Prototype GdiplusStartup(a,b,c)
Prototype GdipDisposeImage(a)
Prototype GdiplusShutdown(a)
Prototype GdipSaveImageToFile(a,b.s,c,d)
Prototype GdipCreateBitmapFromHicon(a, b)
Prototype GdipGetImageWidth(a,b)
Prototype GdipGetImageHeight(a,b)
Prototype GdipCreateBitmapFromFile(a.s,b)
Prototype GdipSetInterpolationMode(a,b)
Prototype GdipDrawImageRect(a,b,c.f,d.f,e.f,f.f)
Prototype GdipCreateFromHDC(a,b)
Prototype GdipCreateBitmapFromHBITMAP(a, b, c)
Prototype GdipDeleteGraphics(a)
Global lib_gdiplus = OpenLibrary(#PB_Any, "GDIPlus.dll")
; Prototype GdiTransparentBlt(a,b,c,d,e,f,g,h,k,l,m)
; Global lib_gdi32 = OpenLibrary(#PB_Any, "gdi32.dll")
; Global GdiTransparentBlt_.GdiTransparentBlt=GetFunction(lib_gdi32,"GdiTransparentBlt")
Global GdiplusStartup_.GdiplusStartup=GetFunction(lib_gdiplus,"GdiplusStartup")
Global GdiplusShutdown_.GdiplusShutdown=GetFunction(lib_gdiplus,"GdiplusShutdown")
Global GdipCreateBitmapFromHBITMAP_.GdipCreateBitmapFromHBITMAP=GetFunction(lib_gdiplus,"GdipCreateBitmapFromHBITMAP")
Global GdipSaveImageToFile_.GdipSaveImageToFile=GetFunction(lib_gdiplus,"GdipSaveImageToFile")
Global GdipDisposeImage_.GdipDisposeImage=GetFunction(lib_gdiplus,"GdipDisposeImage")
Global GdipGetImageWidth_.GdipGetImageWidth=GetFunction(lib_gdiplus,"GdipGetImageWidth")
Global GdipGetImageHeight_.GdipGetImageHeight=GetFunction(lib_gdiplus,"GdipGetImageHeight")
Global GdipCreateBitmapFromFile_.GdipCreateBitmapFromFile=GetFunction(lib_gdiplus,"GdipCreateBitmapFromFile")
Global GdipSetInterpolationMode_.GdipSetInterpolationMode=GetFunction(lib_gdiplus,"GdipSetInterpolationMode")
Global GdipDrawImageRect_.GdipDrawImageRect=GetFunction(lib_gdiplus,"GdipDrawImageRect")
Global GdipCreateFromHDC_.GdipCreateFromHDC=GetFunction(lib_gdiplus,"GdipCreateFromHDC")
Global GdipDeleteGraphics_.GdipDeleteGraphics=GetFunction(lib_gdiplus,"GdipDeleteGraphics")
Structure GdiplusStartupInput
GdiPlusVersion.l
*EventCallback._EVENT
SuppressBackgroundThread.l
SuppressExternalCodecs.l
EndStructure
Structure EncoderParameter
Guid.GUID
NumberOfValues.l
Type.l
*Value
EndStructure
Structure EncoderParameters
Count.l
Parameter.EncoderParameter[1]
EndStructure
Structure ImageCodecInfo
clsid.CLSID
formatID.GUID
*codecName
*dllName
*formatDescription
*filenameExtension
*mimeType
flags.l
version.l
sigCount.l
sigSize.l
*sigPattern
*sigMask
EndStructure
Procedure.b _Gdiplus_Startup(*token)
Protected GdiplusStartupInput.GdiplusStartupInput\GdiPlusVersion=1
ProcedureReturn Bool(GdiplusStartup_(@*token, @GdiplusStartupInput,0)=0)
EndProcedure
Procedure _Gdiplus_GetImageWidth(hImage)
Protected xw:GdipGetImageWidth_(hImage,@xw):ProcedureReturn xw
EndProcedure
Procedure _Gdiplus_GetImageHeight(hImage)
Protected xy:GdipGetImageHeight_(hImage,@xy):ProcedureReturn xy
EndProcedure
Procedure CompareImage_CreateDIBSection(iWidth, iHeight)
Protected tBITMAPINFO.BITMAPINFOHEADER
tBITMAPINFO\biSize=SizeOf(BITMAPINFOHEADER)
tBITMAPINFO\biWidth=iWidth
tBITMAPINFO\biHeight=iHeight
tBITMAPINFO\biPlanes=1
tBITMAPINFO\biBitCount=32
ProcedureReturn CreateDIBSection_(0,@tBITMAPINFO,0,0,0,0)
EndProcedure
Procedure _Gdiplus_CompareImages(hbitmap1,hbitmap2)
Protected iw=_Gdiplus_GetImageWidth(hbitmap1)
Protected ih=_Gdiplus_GetImageHeight(hbitmap1)
Protected hHBitmap1= CompareImage_CreateDIBSection(iw, ih)
Protected hd1=GetWindowDC_(0)
Protected hDC_backbuffer1 = CreateCompatibleDC_(hd1)
Protected DC_obj1 = SelectObject_(hDC_backbuffer1, hHBitmap1)
Protected hGraphic1,hGraphic2,hBitmap3
GdipCreateFromHDC_(hDC_backbuffer1,@hGraphic1)
GdipSetInterpolationMode_(hGraphic1,5)
GdipDrawImageRect_(hGraphic1,hbitmap1,0,0,iw, ih)
Protected hHBitmap2= CompareImage_CreateDIBSection(iw, ih)
Protected hd2=GetWindowDC_(0)
Protected hDC_backbuffer2 = CreateCompatibleDC_(hd2)
Protected DC_obj2 = SelectObject_(hDC_backbuffer2, hHBitmap2)
GdipCreateFromHDC_(hDC_backbuffer2,@hGraphic2)
GdipSetInterpolationMode_(hGraphic2,5)
GdipDrawImageRect_(hGraphic2,hbitmap2,0,0,iw, ih)
Protected hHBitmap3= CompareImage_CreateDIBSection(iw, ih)
Protected hd3=GetWindowDC_(0)
Protected hDC_backbuffer3 = CreateCompatibleDC_(hd3)
Protected DC_obj3 = SelectObject_(hDC_backbuffer3, hHBitmap3)
BitBlt_(hDC_backbuffer3, 0, 0, iw, ih, hDC_backbuffer1, 0, 0, #SRCCOPY)
BitBlt_(hDC_backbuffer3, 0, 0, iw, ih, hDC_backbuffer2, 0, 0, #MERGEPAINT)
GdipDisposeImage_(hBitmap1)
GdipDisposeImage_(hBitmap2)
GdipCreateBitmapFromHBITMAP_(hHBitmap3,0,@hBitmap3)
DeleteObject_(hHBitmap3)
GdipDeleteGraphics_(hGraphic1)
SelectObject_(hD1, DC_obj1)
DeleteDC_(hDC_backbuffer1)
DeleteObject_(hHBitmap1)
ReleaseDC_(0, hD1)
GdipDeleteGraphics_(hGraphic2)
SelectObject_(hD2, DC_obj2)
DeleteDC_(hDC_backbuffer2)
DeleteObject_(hHBitmap2)
ReleaseDC_(0, hD2)
SelectObject_(hD3, DC_obj3)
DeleteDC_(hDC_backbuffer3)
ReleaseDC_(0, hD3)
ProcedureReturn hBitmap3
EndProcedure
Define token
_Gdiplus_Startup(@token)
Define hbitmap1,hbitmap2,hBitmap3
GdipCreateBitmapFromFile_("img0.jpg",@hbitmap1)
GdipCreateBitmapFromFile_("img.jpg",@hbitmap2)
hBitmap3 =_Gdiplus_CompareImages(hbitmap1,hbitmap2)
GdipSaveImageToFile_(hBitmap3,"def.png",?Png_Clsid,0)
GdipDisposeImage_(hBitmap3)
GdiplusShutdown_(token)
DataSection
Png_Clsid: ; clsid for png image format
Data.l $557CF406
Data.w $1A04
Data.w $11D3
Data.b $9A,$73,$00,$00,$F8,$1E,$F3,$2E
EndDataSection