807
社区成员
发帖
与我相关
我的任务
分享
上面这个CopyScreenToBMP的涵数是拷屏的,不是转换的
你可以这样
pBuffer是指向你的图像数据的指针,那么你,在你己知图像长宽的情况下,就可以用CreateDIBSection创建一个与它一样的DIB,然后利用CopyMemory将pBuffer指向的数据拷到DIB中,OleCreatePictureIndirect这个API就可以将这个DIB在内存中转换成VB的StdPicture对像.调用SavePicture方法,就可以直接保存成JPG或BMP了
创建DIB的方法
假设开发包传过来的图像是24位的,320X280,那么可以这样
With BmpInfo
.biBitCount = 24
.biPlanes = 1
.biHeight = 280 '高
.biWidth = 320,宽
.biSize = 40 '本结构长度
End With
hDIB = CreateDIBSection(m_DC, BmpInfo, DIB_RGB_COLORS, pData, 0, 0)
'这时候就得到一个HDIB,但它里面没有内容,pData是指向它的像素数据的指针,
然后我们就可以用CopyMemory将图像写入DIB
CopyMemory pData, pBuffer, pBuffer的字节数
调用下面这个涵数转成VB的StdPicture
dim myPicture as StdPicture
myPicture = CreateBitmapPicture(hDIB, 0, iType)
dim szfile as string
szfile="C:\Test.jpg"
Call SavePicture(szfile)
下面是一段网友写的位图句柄转成StdPicture的代码
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long, ByVal iType As Integer) As Picture
Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
'Fill GUID info
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
'Fill picture info
With Pic
.Size = Len(Pic) ' Length of structure
.hBmp = hBmp ' Handle to bitmap
.hPal = hPal ' Handle to palette (may be null)
If iType = 0 Then
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
Else
.Type = vbPicTypeIcon
End If
End With
'Create the picture
R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
'Return the new picture
Set CreateBitmapPicture = IPic
End Function
使用方法:
Set youPicture = CreateBitmapPicture(hBmp, 0, iType)
其中: youPicture 为你要的StdPicture对象
hBmp 为你已得到的位图的句柄
iType 为vbPicTypeBitmap(位图)或 vbPicTypeIcon(图标)