//以下代碼,仿自胖無極, 但生成的檔案,無法預覽,請各位大俠賜教!謝謝!!!
ulong hbmp, dwBPP, dwNumColors
long ll_hdc, ll_mem, mybitmap
bitmap bmp
blob{14} lbl_header
blob{40} lbl_info
blob b_imagedata
ulong dwImageSize
n_api.OpenClipBoard(0)
hbmp = n_api.GetClipBoardData(2)
ll_hdc = getdc( handle(aw_win)) //aw_win: 為傳入的父窗體參數
ll_mem = CreateCompatibleDC(ll_hdc)
SelectObject(ll_mem, hbmp)
SetMapMode(ll_mem, GetMapMode(ll_hdc))
GetObject(hbmp, 24, ref bmp)
BitBlt(ll_hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, ll_mem, 0, 0, SRCCOPY)
mybitmap = CreateCompatibleBitmap (ll_hdc,bmp.bmWidth,bmp.bmHeight)
BitmapInfo lstr_Info
BitmapFileHeader lstr_Header
dwBPP = n_api.GetDeviceCaps(ll_hdc, n_api.BITSPIXEL)
if dwBPP <= 8 then
dwNumColors = n_api.GetDeviceCaps(ll_hdc, n_api.NUMCOLORS)
else
dwNumColors = 0
end if
dwImageSize = bmp.bmWidth * bmp.bmHeight * dwBPP / 8
//填充BitmapFileHeader
lstr_Info.bmiheader.biSize = 40
lstr_Info.bmiheader.biWidth = bmp.bmWidth
lstr_Info.bmiheader.biHeight = bmp.bmHeight
lstr_Info.bmiheader.biPlanes = 1
lstr_Info.bmiheader.biBitCount = dwBPP
lstr_Info.bmiheader.biCompression = 0
lstr_Info.bmiheader.biSizeImage = dwImageSize
lstr_Info.bmiheader.biXPelsPerMeter = 0
lstr_Info.bmiheader.biYPelsPerMeter = 0
lstr_Info.bmiheader.biClrUsed = dwNumColors
lstr_Info.bmiheader.biClrImportant = dwNumColors
b_imagedata = blob(space(dwImageSize))
GetDIBits(ll_hdc, mybitmap, 0, bmp.bmHeight,ref b_imagedata, lstr_Info, 0)
lstr_Header.bfType[1] = 'B'
lstr_Header.bfType[2] = 'M'
lstr_Header.bfSize = dwImageSize + &
14/*sizeof(bitmapfileheader)*/ + &
40/*sizeof(bitmapinfoheader)*/+&
dwNumColors * 4
lstr_Header.bfReserved1 = 0
lstr_Header.bfReserved2 = 0
lstr_Header.bfOffBits = 14/*sizeof(bitmapfileheader)*/ + &
40/*sizeof(bitmapinfoheader)*/+&
dwNumColors * 4
CopyBitmapFileHeader(lbl_header, lstr_Header, 14/*sizeof(bitmapfileheader)*/ )
CopyBitmapInfo(lbl_Info, lstr_Info, 40/*sizeof(bitmapinfoheader)*/)
b_imagedata = lbl_header + lbl_info + b_imagedata
save2file('d:\zls.jpg', b_imagedata) //此處將Blob數據寫入到檔案中
DeleteObject(mybitmap)
DeleteDC(ll_mem)
n_api.CloseClipBoard()
return true
uj5u.com熱心網友回復:
//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"
type bitmapfileheader from structure
integer bftype
long bfsize
integer bfreserved1
integer bfreserved2
long bfoffbits
end type
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 bitmapinfo from structure
bitmapinfoheader bmiheader
unsignedlong bmicolors[]
end type
//前面的是函式定義與結構體
long lul_hBitmap
ulong lul_hdcmem, hdc
//判斷剪貼板中的資料是否是BMP資料
constant ulong CF_BITMAP = 2
if IsClipboardFormatAvailable(CF_BITMAP) = 1 then
//(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
lstr_Info.bmiHeader.biSize = 40
IF GetDIBits(lul_hdcMem, lul_hBitmap, 0, 0, 0, lstr_Info, DIB_RGB_COLORS) > 0 THEN
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
END IF
END IF
//釋放通過GetDC或GetWindowDC所檢索出來的公用背景關系設備
ReleaseDC(GetDesktopWindow(),hdc)
//洗掉通過CreteDC或CreateCompatibleDC所創建的背景關系設備
DeleteDC(lul_hdcmem)
//lul_hBitmap由于已經通過selectobject函式選入背景關系設備lul_hdcmem中,因此不需要再呼叫DeleteObject函式來洗掉
//(3)取到圖片檔案后,則開始保存到檔案中
if len(lblb_bitmap) > 0 then
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
// (創建)打開檔案
lul_file = CreateFile("d:\abc.bmp", GENERIC_WRITE, &
FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0)
IF lul_file = INVALID_HANDLE_VALUE THEN
RETURN
END IF
// 寫檔案
lul_length = Len(lblb_bitmap)
lb_rtn = WriteFile(lul_file, lblb_bitmap, &
lul_length, lul_written, 0)
// 關閉檔案
CloseHandle(lul_file)
end if
End If
uj5u.com熱心網友回復:
//lul_hBitmap由于已經通過selectobject函式選入背景關系設備lul_hdcmem中,因此不需要再呼叫DeleteObject函式來洗掉應該改為
DeleteObject(lul_hbitmap)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/81589.html
標籤:API 調用
上一篇:請問怎樣才能從文本檔案中讀出資料
下一篇:系統運行不了 ,請大家幫忙,
