|
楼主 |
发表于 2021-10-18 17:17
|
显示全部楼层
这个先输出再导入本来就是个简单的辅助过程,也算是较快捷的方法,包括行入工作表后复制再删除等等,
但我就是不想“中间商赚差价”,另外就是想要确认直接内存中生成是可行的,
例如:论坛中就有 "纯代码生在二维码“ 的示例就是直接生成二维码图片后粘贴到剪切板(再粘贴到工作表)的......
正在研究中,看能否整合成一个通用的过程。
GlobalAlloc CopyMemory 方法么,也有一个很好的例子:
图片控件里怎样调用网络图片 (3 楼)
https://club.excelhome.net/thread-1257381-1-1.html
小fisher 大师 的代码:
- Option Explicit
- '内存函数
- Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
- Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
- 'OLE函数
- Public Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As Any) As Long
- Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
- Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As Any, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
- Private Type PicBmp
- Size As Long
- Type As Long
- hBmp As Long
- hPal As Long
- Reserved As Long
- End Type
- 'GDIplus函数
- Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
- Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
- Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
- Private Declare Function GdipLoadImageFromStream Lib "GDIPlus" (ByVal stream As IUnknown, Image As Long) As Long
- Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal bitmap As Long, hbmReturn As Long, ByVal background As Long) As Long
- Private Type GdiplusStartupInput
- GdiplusVersion As Long
- DebugEventCallback As Long
- SuppressBackgroundThread As Long
- SuppressExternalCodecs As Long
- End Type
- Private Const GMEM_MOVEABLE = &H2
- Public Function LoadWebImage(url As String) As StdPicture
-
- Dim hMem As Long
- Dim nSize As Long
- Dim lpData As Long
- Dim bufferBytes() As Byte
- Dim istm As stdole.IUnknown
- Dim lToken As Long
- Dim lGSI As GdiplusStartupInput
- Dim IID_IDispatch(15) As Byte
- Dim pic As PicBmp
- Dim lImage As Long, hBmp As Long
-
- Dim httpRequest
- Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1") '创建WinHttpRequest对象
- With httpRequest
- .Open "get", url, False '获取URL内容
- .Send
- If Left(.GetResponseHeader("Content-Type"), 6) = "image/" Then '如果URL为图片
- '############nSize = .GetResponseHeader("Content-Length") '获取网络图片的字节长度
- '##########ReDim bufferBytes(nSize - 1)
- bufferBytes = .ResponseBody '将图片文件存储到字节数组中
- nSize = UBound(bufferBytes) + 1 '获取网络图片的字节长度###########
- hMem = GlobalAlloc(GMEM_MOVEABLE, nSize) '分配一块全局内存
- lpData = GlobalLock(hMem) '获取内存句柄
- CopyMemory lpData, VarPtr(bufferBytes(0)), nSize '将图片文件的字节复制到全局内存中
-
- lGSI.GdiplusVersion = 1
- If GdiplusStartup(lToken, lGSI, 0) = 0 Then '初始化GDI+
- If CreateStreamOnHGlobal(hMem, 1, istm) = 0 Then '从全局内存创建流
- GdipLoadImageFromStream istm, lImage '将流中内容加载为GDI+ Image图形对象
- GdipCreateHBITMAPFromBitmap lImage, hBmp, &HFFFFFF '从Image获取Bitmap句柄
- GdipDisposeImage lImage '释放Image对象
-
- '以下代码从Bitmap句柄生成一个StdPicture对象
- CLSIDFromString StrPtr("{00020400-0000-0000-C000-000000000046}"), IID_IDispatch(0)
- With pic
- .Size = Len(pic)
- .Type = 1
- .hBmp = hBmp
- .hPal = 0
- End With
- OleCreatePictureIndirect pic, IID_IDispatch(0), 1, LoadWebImage
- End If
- GdiplusShutdown lToken '关闭GDI+
- End If
- GlobalUnlock hMem
- GlobalFree hMem '释放全局内存
- End If
- End With
- End Function
复制代码
无奈水平有限,未做出个成功的通用模板。 |
|