SKY外语计算机学习

标题: vb图片变暗 读取像素改变值 [打印本页]

作者: SKY定格    时间: 2012-5-24 13:16
标题: vb图片变暗 读取像素改变值
本帖最后由 sky_yx 于 2015-12-30 14:21 编辑

以游戏超级男人暂停效果为例 代码如下:
我这里有三个picture  stopp1 stopp2 stopp3(里面还有一个image  装载stop!图像)
  1. Private Sub stop1_Click() ' 暂停过程
  2.     stopN = True       '暂停为真
  3.     '------------------------------截当前活动屏
  4.     stopp2.Visible = False
  5.     Clipboard.Clear
  6.     keybd_event 18, 0, 0, 0
  7.     keybd_event vbKeySnapshot, 0&, 0&, 0&
  8.     DoEvents
  9.    
  10.     t = Timer
  11.     Do While Timer - t < 0.1
  12.         DoEvents
  13.     Loop
  14.      
  15.     keybd_event 18, 0, KEYEVENTF_KEYUP, 0
  16.     stopp1.Picture = Clipboard.GetData
  17.     stopp1.Move 0, 0, form1.Width, form1.Height
  18.     stopp3.Move 0, 0, form1.Width, form1.Height
  19.     '------------------------------图像变暗
  20.    
  21.     Dim BITMAP As BITMAPINFO
  22.     Dim ImgData() As Byte
  23.     Dim lIndex As Long
  24.    
  25.     With BITMAP
  26.         .bmiHeader.biSize = 40
  27.     End With
  28.    
  29.     If GetDIBits(stopp1.hdc, stopp1.Picture.Handle, 0, 0, 0, BITMAP, DIB_RGB_COLORS) <> 0 Then      '获取图像位图数据大小
  30.         ReDim ImgData(BITMAP.bmiHeader.biSizeImage)     '重定义数组准备接收位图数据
  31.         If GetDIBits(stopp1.hdc, stopp1.Picture.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS) <> 0 Then     '正式获取位图数据并将数据存入定义的数组中
  32.             '注意数组中颜色分量的存储顺序为:B、G、R(并不是R、G、B),如果你需要对图像进行处理只需改变数组中的数据即可完成
  33.             
  34.             '**************** 下面这段程序是将图像亮度变暗(只作为演示,其它效果你可以自己动手做) ***********************
  35.             '注意:第一条扫描线的位置是从左下角开始
  36.             
  37.             For lIndex = 0 To BITMAP.bmiHeader.biSizeImage
  38.                 If ImgData(lIndex) - 50 < 0 Then
  39.                     ImgData(lIndex) = 0
  40.                 Else
  41.                     ImgData(lIndex) = ImgData(lIndex) - 50
  42.                 End If
  43.             Next
  44.             '*********************************************************************
  45.             
  46.             SetDIBits stopp3.hdc, stopp3.Image.Handle, 0, BITMAP.bmiHeader.biHeight - 1, ImgData(0), BITMAP, DIB_RGB_COLORS '将位图数据写入stopp3中
  47.         End If
  48.     End If
  49.     stopp3.Picture = stopp3.Image
  50.     '====================================剪切stopp3为stopp1
  51.     stopp2.Visible = True
  52.     stopp2.Move 0, 0, form1.ScaleWidth, form1.ScaleHeight
  53.     stopp2.PaintPicture stopp3, (form1.ScaleWidth - form1.Width) / 2, form1.ScaleHeight - form1.Height - (form1.ScaleWidth - form1.Width) / 2, form1.Width, form1.Height, 0, 0, form1.Width, form1.Height
  54.     Image1.Move 200, 200
复制代码
模块:
  1. '-----------------------------------------------------模拟按键
  2. Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  3. Public Const KEYEVENTF_KEYUP = &H2 '模拟按键截屏
  4. '-----------------------------------------------------------------------------像素读取 图像转暗
  5. Public Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  6. Public Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  7. Public Type BITMAPINFOHEADER '40 bytes
  8.         biSize As Long
  9.         biWidth As Long
  10.         biHeight As Long
  11.         biPlanes As Integer
  12.         biBitCount As Integer
  13.         biCompression As Long
  14.         biSizeImage As Long
  15.         biXPelsPerMeter As Long
  16.         biYPelsPerMeter As Long
  17.         biClrUsed As Long
  18.         biClrImportant As Long
  19. End Type
  20. Public Type RGBQUAD
  21.         rgbBlue As Byte
  22.         rgbGreen As Byte
  23.         rgbRed As Byte
  24.         rgbReserved As Byte
  25. End Type
  26.         bmiHeader As BITMAPINFOHEADER
  27.         bmiColors As RGBQUAD
  28. End Type
  29. Public Const DIB_RGB_COLORS = 0 '  color table in RGBs
复制代码
效果图:


作者: 夏师太~·    时间: 2012-5-25 10:40
本帖最后由 sky_yx 于 2015-12-30 14:21 编辑








欢迎光临 SKY外语计算机学习 (http://join.skywj.com/) Powered by Discuz! X2.5