PB从剪贴板取图并保存

结构体定义

type bitmapinfoheader from structure
	long bisize  
	long biwidth  
	long biheight  
	integer biplanes  
	integer bibitcount  
	long bicompression  
	long bisizeimage  
	long bixpelspermeter  
	long biypelspermeter  
	long biclrused  
	long biclrimportant  
end type

type bitmapfileheader from structure
	integer bftype  
	long bfsize  
	integer bfreserved1  
	integer bfreserved2  
	long bfoffbits  
end type

type bitmapinfo from structure
	bitmapinfoheader bmiheader  
	unsignedlong bmicolors[]  
end type


API定义

FUNCTION Boolean OpenClipboard(ULong hWndNewOwner) LIBRARY "user32.dll"  
FUNCTION Boolean CloseClipboard() LIBRARY "user32.dll"  
FUNCTION ULong GetClipboardData(ulong wFormat) LIBRARY "user32.dll"  
FUNCTION ulong IsClipboardFormatAvailable(ulong wFormat) LIBRARY "user32.dll"  
FUNCTION ULong GetDesktopWindow() LIBRARY "user32.dll"  
FUNCTION ULong GetDC(ULong hWnd) LIBRARY "user32.dll"  
FUNCTION ULong CreateCompatibleDC(ULong hdc) LIBRARY "gdi32.dll"  
FUNCTION ULong SelectObject(ULong hdc,ULong hgdiobj) LIBRARY "gdi32.dll"  
FUNCTION Int GetDIBits(ULong hdc,ULong hbmp,UInt uStartScan,UInt cScanLines,REF Blob lpvBits,REF bitmapinfo lpbi,UInt uUsage) LIBRARY "gdi32.dll"  
FUNCTION Int GetDIBits(ULong hdc,ULong hbmp,UInt uStartScan,UInt cScanLines, ULong lpvBits, REF bitmapinfo lpbi, UInt uUsage) LIBRARY "gdi32.dll"  
SUBROUTINE CopyBitmapFileHeader(REF Blob Destination, bitmapfileheader Source,Long Length) LIBRARY "kernel32.dll" ALIAS FOR "RtlMoveMemory"  
SUBROUTINE CopyBitmapInfo(REF Blob Destination, bitmapinfo Source,Long Length) LIBRARY "kernel32.dll" ALIAS FOR "RtlMoveMemory"  
FUNCTION Boolean DeleteDC(ULong hdc) LIBRARY "gdi32.dll"  
FUNCTION Int ReleaseDC(ULong hWnd,ULong hdc) LIBRARY "user32.dll"  
FUNCTION ULong CreateFile(String lpFileName, ULong dwDesiredAccess, ULong dwShareMode, ULong lpSecurityAttributes, ULong dwCreationDisposition, &  
ULong dwFlagsAndAttributes, ULong hTemplateFile ) LIBRARY "kernel32.dll" ALIAS FOR "CreateFileA"  
FUNCTION Boolean WriteFile(ULong hFile,Blob lpBuffer,ULong nNumberOfBytesToWrite,REF ULong lpNumberOfBytesWritten, &  
ULong lpOverlapped) LIBRARY "kernel32.dll"  
FUNCTION Boolean CloseHandle(ULong hObject) LIBRARY "kernel32.dll" 

函数调用

long lul_hBitmap  
ulong lul_hdcmem, hdc  
//判断剪贴板中的数据是否是BMP数据  
constant ulong CF_BITMAP = 2  
constant ulong DIB_RGB_COLORS = 0
constant ulong BITMAPTYPE = 19778
if IsClipboardFormatAvailable(CF_BITMAP) <> 1 then return      
//(1)开始从剪贴板取数据  
OpenClipboard(GetDesktopWindow())  
lul_hBitmap = GetClipBoardData(CF_BITMAP)  
CloseClipboard()  
blob lb  
hdc = GetDC(GetDesktopWindow())  
lul_hdcmem = CreateCompatibleDC(hdc)  
selectobject(lul_hdcmem, lul_hBitmap)  
//(2)将取得的内存图片转换位blob数据流  
ULong lul_hdc, lul_pixels  
Blob lblb_header, lblb_info, lblb_bitmap  
BitmapInfo lstr_Info  
BitmapFileHeader lstr_Header  
IF lul_hBitmap = 0 THEN return 
lstr_Info.bmiHeader.biSize = 40  
IF GetDIBits(lul_hdcMem, lul_hBitmap, 0, 0, 0, lstr_Info, DIB_RGB_COLORS) <= 0 THEN return  
lul_pixels = lstr_Info.bmiHeader.biBitCount  
lstr_Info.bmiColors[lul_pixels] = 0  
lblb_bitmap = Blob(Space(lstr_Info.bmiHeader.biSizeImage))  
// 获取文件信息  
GetDIBits(lul_hdcMem, lul_hBitmap, 0, lstr_Info.bmiHeader.biheight, &  
lblb_bitmap, lstr_Info, DIB_RGB_COLORS)  
// 创建BMP文件头  
lstr_Header.bfType = BITMAPTYPE  
lstr_Header.bfSize = lstr_Info.bmiHeader.biSizeImage  
lstr_Header.bfOffBits = 54 + (lul_pixels * 4)  
// 将文件头转换成blob  
lblb_header = Blob(Space(14))  
CopyBitmapFileHeader(lblb_header, lstr_Header, 14)  
// 将文件内容转换为blob  
lblb_info = Blob(Space(40 + lul_pixels * 4))  
CopyBitmapInfo(lblb_info, lstr_Info, 40 + lul_pixels * 4)  
// 整合文件信息  
lblb_bitmap = lblb_header + lblb_info + lblb_bitmap  
//释放通过GetDC或GetWindowDC所检索出来的公用上下文设备  
ReleaseDC(GetDesktopWindow(),hdc)  
//删除通过CreteDC或CreateCompatibleDC所创建的上下文设备  
DeleteDC(lul_hdcmem)  
  
//(3)取到图片文件后,则开始保存到文件中  
if len(lblb_bitmap) <= 0 then  return
CONSTANT ULong INVALID_HANDLE_VALUE = -1  
CONSTANT ULong GENERIC_WRITE = 1073741824  
CONSTANT ULong FILE_SHARE_WRITE = 2  
CONSTANT ULong CREATE_ALWAYS = 2  
ULong lul_file, lul_length, lul_written  
Boolean lb_rtn  
// (创建)打开文件  

string ls_path, ls_file
int li_rc
li_rc = GetFileSaveName ( "Select BMP File",  ls_path, ls_file, "bmp",  "bmp Files (*.bmp),*.bmp" , "D:\",  32770)

IF li_rc <> 1 Then RETURN

lul_file = CreateFile(ls_path, GENERIC_WRITE, &  
FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0)  
IF lul_file = INVALID_HANDLE_VALUE THEN  RETURN  
// 写文件  
lul_length = Len(lblb_bitmap)  
lb_rtn = WriteFile(lul_file, lblb_bitmap, &  
lul_length, lul_written, 0)  
// 关闭文件  
CloseHandle(lul_file)     

需要保持为jpg格式文件的,可以在将图片保存为bmp格式后,再调用外部dll将bmp转为紧jpg。相关dll可以到这里下载 pb最好用的bmp图片转jpg图片dll(ImageUtils.dll)

    

相关源码:

PB从剪贴板取图并保存   

 

   

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值