Local $hBitmap, $BitmapData, $i_width, $i_height, $Scan0, $pixelData, $s_BMPData, $i_Stride
_GDIPlus_Startup()
$hBitmap = _GDIPlus_BitmapCreateFromFile($s_ImageFile)
If @error Then
_GDIPlus_ShutDown ()
Return 0;无效的图形文件或未找到该文件
EndIf
$i_width = _GDIPlus_ImageGetWidth($hBitmap)
$i_height = _GDIPlus_ImageGetHeight($hBitmap)
$BitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $i_width, $i_height, $GDIP_ILMREAD, $GDIP_PXF24RGB)
;If @error Then ......;
$i_Stride = DllStructGetData($BitmapData, "Stride");Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
$Scan0 = DllStructGetData($BitmapData, "Scan0");Scan0 - Pointer to the first (index 0) scan line of the bitmap.
$pixelData = DllStructCreate("ubyte lData[" & (Abs($i_Stride) * $i_height) & "]", $Scan0)
;不要使用官方论坛上提供的Abs($i_Stride) * $i_height-1,否则无法正确处理gif冗余等情形
$s_BMPData = DllStructGetData($pixelData, "lData")
$s_BMPData = StringTrimLeft($s_BMPData,2);去掉头部"0x"
_GDIPlus_BitmapUnlockBits($hBitmap, $BitmapData)
_GDIPlus_ImageDispose($hBitmap)
_WinAPI_DeleteObject ($hBitmap)
_GDIPlus_Shutdown()
If $b_Array2d Then;要求返回二维数组
Local $a_return[$i_width][$i_height], $x, $y, $s
For $y=0 To $i_height-1
$s=StringMid($s_BMPData, $y*($i_Stride*2)+1, $i_width*6)
;不要使用一些例子中提出的$s=StringMid($s_BMPData, $y*($i_width*6)+1, $i_width*6),经实际测试,该方式无法正确处理:
;GIF冗余,带隐藏内容的图形文件(一种数据加密方式),或者多页图形文件,当然这些情形比较少见,下同
For $x= 0 To $i_width-1
$a_return[$x][$y]= Number("0x"&StringMid($s,$x*6+1 ,6))
Select
Case $func_sc_type=1;黑白
If $a_return[$x][$y]>=$func_sc Then
$a_return[$x][$y]=$func_bg
Else
$a_return[$x][$y]=0
EndIf
Case $func_sc_type=2;反相黑白
If $a_return[$x][$y]<$func_sc Then
$a_return[$x][$y]=$func_bg
Else
$a_return[$x][$y]=0
EndIf
Case $func_sc_type=3;类似=1,但黑色用原色代替
If $a_return[$x][$y]>=$func_sc Then
$a_return[$x][$y]=$func_bg
EndIf
EndSelect
Next
Next
Else;要求返回一维数组
Local $a_return[$i_height], $y
For $y=0 To $i_height-1
$a_return[$y]=StringMid($s_BMPData, $y*($i_Stride*2)+1, $i_width*6)
$a_return[$y] = myChangeRegExpSC($a_return[$y], $func_sc_type, $func_sc, $func_bg);颜色变化
Next
EndIf
Return $a_return