首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

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

2012-01-31 
作为图像爱好者,我以我的方式表示对震亡同胞们的默哀----彩色图像真正,即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



'******************************************** 用于内存处理的相关API ********************************************

Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
Private Declare Function VarPtrArray Lib "msvbvm50" Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal numBytes As Long)


'******************************************** 公共常用的API函数 ********************************************

Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long


Private mHdc As Long '保存了内存DC
Private mhDib As Long '当前DibSection的句柄
Private mhOldDib As Long '原始DibSection的句柄
Private mPtr As Long '当前DibSection的内存地址
Private mWidthBytes As Long '当前图像的扫描行字节数
Private mBmpInfo As BITMAPINFOHEADER '当前图像的文件信息



Private Sub Class_Terminate()
  Destroy
End Sub

Public Property Get Width() As Long
  Width = mBmpInfo.biWidth
End Property

Public Property Get Height() As Long
  Height = mBmpInfo.biHeight
End Property

Public Property Get hdc() As Long
  hdc = mHdc
End Property

Public Property Get DataPtr() As Long
  DataPtr = mPtr
End Property

Public Property Get WidthBytes() As Long
  WidthBytes = mWidthBytes
End Property


[解决办法]
类!可以重复使用的类!收藏了~
[解决办法]
再接在理!
[解决办法]

探讨
支持+收藏+接分

[解决办法]
不知楼主这个Cimage一共有多少个公共函数?是否还能放出一些?呵呵,我有点贪
[解决办法]
人气确实大不如从前了,而且一些新人都是 急功近利的,不好好地去研究技术,不是他们需要的,可能都不去看
[解决办法]
CSDN应该学http://www.experts-exchange.com。分二部分,一部分收费,会员交费,解答者收费;一部分free,开发给所有人.
[解决办法]
支持
探讨
CSDN应该学http://www.experts-exchange.com。分二部分,一部分收费,会员交费,解答者收费;一部分free,开发给所有人.

[解决办法]
探讨
CSDN应该学http://www.experts-exchange.com。分二部分,一部分收费,会员交费,解答者收费;一部分free,开发给所有人.

[解决办法]
哪也不一定,如果钱已给出去了,自己收不回来了,也不一定就不结贴,而现在这些分数,很多人根本没有概念,不在乎,所以,知道他想的以后,就再也不管了
[解决办法]
Laview,
如果上面的编码是你CImage的一部分,有些问题与你商榷:

Set StdPic = LoadPicture(Filename) 



1.LoadPicture仅支持.bmp,.jpg,.emf,.wmf,.ico,.gif等格式。
2.LoadPicture不支持Unicode.把你的XP/Vista的Locale ID 设为1033(English),在读中文的文件名或包含中文的路径,LoadPicture failed.
[解决办法]
顶下 好东西 就是还有点看不懂
[解决办法]
没人给以价值的评论。CSDN真地出了问题。
[解决办法]
有没有办法,直接对Pictuerbox里的图像进行转换,然后直接生成灰度图像到pictuerbox中、
[解决办法]
發現多,我也頂

[解决办法]

探讨
stdpicture是不具有保存位8位的功能的,对于已经加载到picturebox中的图像,是可以找到其内存地址的,但是如果要让其变为灰度,在视觉上还是可以的,但实际上内部他还是真彩色。

[解决办法]
PF
[解决办法]
唯有帮顶!
[解决办法]
支持
[解决办法]
楼主很厉害,佩服。
[解决办法]
探讨
没人给以价值的评论。CSDN真地出了问题。

[解决办法]
老马应该说是不懂算法~
转灰度的算法很多人都会,问题是速度!

热点排行