作为图像爱好者,小弟我以小弟我的方式表示对震亡同胞们的默哀-彩色图像<<真正,即32bpp->8bpp>>转为灰度图像(源代码)

作为图像爱好者,我以我的方式表示对震亡同胞们的默哀----彩色图像<<真正,即32bpp->8bpp>>转为灰度图像(源代码)。
今天很多网站也实现了真彩色转为灰度图像来作为主页,作为图像的研究者,特此放送严格意义上的32->8[b][/b]位图像的转变,并实现保存功能。
  此代码为我自己写的Cimage类中摘取的部分内容并作了大量简化和大量注释,如有疑问欢迎和群30417248内的其他高手交流。

   
一下代码请贴如一个新建的Cimage类中:

Option Explicit


Private Type BITMAPFILEHEADER
  bfType As Integer
  bfSize As Long
  bfReserved1 As Integer
  bfReserved2 As Integer
  bfOffBits As Long
End Type

Private Type BITMAPINFOHEADER
  biSize As Long
  biWidth As Long
  biHeight As Long
  biPlanes As Integer
  biBitCount As Integer
  biCompression As Long
  biSizeImage As Long
  biXPelsPerMeter As Long
  biYPelsPerMeter As Long
  biClrUsed As Long
  biClrImportant As Long
End Type

Private Type bitmap
  bmType As Long
  bmWidth As Long
  bmHeight As Long
  bmWidthBytes As Long
  bmPlanes As Integer
  bmBitsPixel As Integer
  BmBits As Long
End Type

Private Type RGBQUAD
  Blue As Byte
  Green As Byte
  Red As Byte
  Reserved As Byte
End Type

Private Type BITMAPINFO
  bmiHeader As BITMAPINFOHEADER
  bmiColors As RGBQUAD
End Type



Private Const BI_bitfields = 3& '带掩码的
Private Const BI_RGB = 0 '正常
Private Const DIB_RGB_COLORS = 0 '真彩色
Private Const OBJ_BITMAP = 7 '位图对象
Private Const SRCCOPY = &HCC0020 '直接拷贝
Private Const IMAGE_BITMAP = 0 'LoadImage函数的载入类型,位图
Private Const LR_LOADFROMFILE = &H10 '从文件载入
Private Const LR_CREATEDIBSECTION = &H2000 '如果指定了IMAGE_BITMAP,就返回DIBSection的句柄,而不是位图的句柄
Private Const STRETCH_ANDSCANS = 1 '默认设置。剔除的线段与剩下的线段进行AND运算。这个模式通常应用于采用了白色背景的单色位图
Private Const STRETCH_ORSCANS = 2 '剔除的线段被简单的清除。这个模式通常用于彩色位图
Private Const STRETCH_DELETESCANS = 3 '剔除的线段与剩下的线段进行OR运算。这个模式通常应用于采用了白色背景的单色位图
Private Const STRETCH_HALFTONE = 4 '目标位图上的像素块被设为源位图上大致近似的块。这个模式要明显慢于其他模式

'******************************************** 用于图像方面的相关API ********************************************

Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, lpBitsInfo As BITMAPINFOHEADER, ByVal wUsage As Long, lpBits As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, lpRGBQuad As Any) As Long
Private Declare Function SetDIBColorTable Lib "gdi32" (ByVal hdc As Long, ByVal un1 As Long, ByVal un2 As Long, pcRGBQuad As RGBQUAD) As Long

'******************************************** 用于系统输出的相关API ********************************************

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function GetStretchBltMode Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long