Project1
标题:
Graphics.snap_to_bitmap 優化2版
[打印本页]
作者:
danny8376
时间:
2008-12-1 03:12
标题:
Graphics.snap_to_bitmap 優化2版
這個優化版
速度快了很多
而且把一些該釋放的釋放掉了
(作截圖存檔請自己找Bitmap的Marshal 找一下就有了)
#==============================================================================
# ■ Graphics.snap_to_bitmap 優化2版
# 原作者:神思
# 優化者:釣到一隻猴子@_@ ( AAM@_@ )
#==============================================================================
#==============================================================================
# ■ Graphics
#------------------------------------------------------------------------------
# 进行有关全体图像处理的模块。
# RMXP 版的 Graphics.snap_to_bitmap
#==============================================================================
class << Graphics
CreateDC = Win32API.new("gdi32", "CreateDC", "pppl", "l")
CreateCompatibleBitmap = Win32API.new("gdi32", "CreateCompatibleBitmap", "lll", "l")
CreateCompatibleDC = Win32API.new("gdi32", "CreateCompatibleDC", "l", "l")
SelectObject = Win32API.new("gdi32", "SelectObject", "ll", "l")
BitBlt = Win32API.new("gdi32", "BitBlt", "lllllllll", "l")
GetBitmapBits = Win32API.new("gdi32", "GetBitmapBits", "llp", "l")
ScreenToClient = Win32API.new("user32", "ScreenToClient", "ip", "i")
DeleteDC = Win32API.new("gdi32", "DeleteDC", "l", "l")
DeleteObject = Win32API.new("gdi32", "DeleteObject", "l", "l")
SRCCOPY = 0xCC0020
RtlMoveMemory = Win32API.new('kernel32', 'RtlMoveMemory', 'ipi', 'i')
HWnd = Win32API.new("user32", "GetActiveWindow", nil, 'l').call
DwCount = (640 * 480 * 4) * 2
@@lpBits = "\000" * DwCount
#--------------------------------------------------------------------------
# ● snap_to_bitmap
# 将当前画面转换为Bitmap对象
#--------------------------------------------------------------------------
def snap_to_bitmap
width, height = 640,480
hb = Bitmap.new(width, height)
rgbs = self.bitmap_data
len = width * 4
for y in 0...height
break if 2*y >= height - 1
nth1 = y * len
nth2 = (height - 1 - y) * len
tStr = rgbs[nth1,len]
rgbs[nth1, len] = rgbs[nth2, len]
rgbs[nth2, len] = tStr
end
RtlMoveMemory.call(hb.address, rgbs, 640 * 480 * 4)
return hb
end
#--------------------------------------------------------------------------
# ● bitmap_data
# 获取当前画面的数据部分```
# RGB依然是调转过来了`= =``
#--------------------------------------------------------------------------
def bitmap_data
hScrDC = CreateDC.call("DISPLAY", "", "", 0)
hMemDC = CreateCompatibleDC.call(hScrDC)
hBitmap = CreateCompatibleBitmap.call(hScrDC, 640, 480)
hOldBitmap = SelectObject.call(hMemDC, hBitmap)
BitBlt.call(hMemDC, xsrc, ysrc, 640*2, 480*2, hScrDC, 0, 0, SRCCOPY)
hBitmap2 = SelectObject.call(hMemDC, hOldBitmap)
GetBitmapBits.call(hBitmap2, DwCount, @@lpBits)
DeleteDC.call(hScrDC)
DeleteDC.call(hMemDC)
DeleteObject.call(hBitmap)
DeleteObject.call(hOldBitmap)
DeleteObject.call(hBitmap2)
return @@lpBits
end
#--------------------------------------------------------------------------
# ● 用户窗口的位置
#--------------------------------------------------------------------------
def xsrc
return self.point[0]
end
#--------------------------------------------------------------------------
# ● 用户窗口的位置
#--------------------------------------------------------------------------
def ysrc
return self.point[1]
end
#--------------------------------------------------------------------------
# ● 那个点`= =``
#--------------------------------------------------------------------------
def point
point = [0,0].pack("LL")
ScreenToClient.call(HWnd, point)
return point.unpack("LL")
end
end
#==============================================================================
# ■ Bitmap
#------------------------------------------------------------------------------
# 处理影像的类。
#==============================================================================
class Bitmap
RtlMoveMemory = Win32API.new('kernel32', 'RtlMoveMemory', 'pii', 'i')
#--------------------------------------------------------------------------
# ●
#--------------------------------------------------------------------------
def address
# [[[bitmap.object_id * 2 + 16] + 8] + 16] == 数据的开头
buffer, ad = "xxxx", object_id * 2 + 16
RtlMoveMemory.call(buffer, ad, 4)
ad = buffer.unpack("L")[0] + 8
RtlMoveMemory.call(buffer, ad, 4)
ad = buffer.unpack("L")[0] + 16
RtlMoveMemory.call(buffer, ad, 4)
return buffer.unpack("L")[0]
end
end
复制代码
作者:
神思
时间:
2008-12-1 05:15
呵呵~不错~
把DC释放后就完美了
作者:
danny8376
时间:
2008-12-4 08:01
以下引用
神思于2008-11-30 21:15:38
的发言:
呵呵~不错~ 把DC释放后就完美了
沒改到= =
不過速度慢(忽略set_pixel)是曼在圓腳本"\000" * dwCount
這處理時間超過0.1S
作者:
danny8376
时间:
2009-5-29 07:37
以下引用
神思于2008-11-30 21:15:38
的发言:
呵呵~不错~ 把DC释放后就完美了
改好了
托真久
都半年了= =
作者:
杨飞云
时间:
2009-5-29 07:46
提示:
作者被禁止或删除 内容自动屏蔽
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1