在 Visual Basic 6 中,我有以下代码,用于捕获屏幕并编码或转换为 JPG,但在文件中。(例如,lRes = GdipSaveImageToFile
保存 JPG 文件,但我不想另存为文件,而是应该将 JPG 保存在内存或字节数组中)
我想将 JPG 图像保存在内存或字节数组中。我应该怎么办。
我不想将 PNG 保存在内存中,而是将 JPG 编码在内存中,我已经搜索了很多关于它的内容,但直到没有找到任何解决方案。
Public Sub DesktopToJPG(ByVal filename As String, Optional ByVal Quality As Long = 80, Optional IncludeMouseCursor As Boolean = False)
On Error Resume Next
Dim tSI As GdiplusStartupInput
Dim lRes As Long, lGDIP As Long, lBitmap As Long
Dim X As Long, Y As Long, wide As Long, high As Long
Dim myDIB As Long, myDC As Long, desktopDC As Long
Dim bi24BitInfo As BITMAPINFO
Dim bitmapData() As Byte
Dim pcin As PCURSORINFO
Dim piinfo As ICONINFO
' Starting position/Size of capture (full screen)
X = 0: Y = 0
wide = Screen.Width / Screen.TwipsPerPixelX
high = Screen.Height / Screen.TwipsPerPixelY
'
With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = wide
.biHeight = high
.biDataSize = ((.biWidth * 3 + 3) And &HFFFFFFFC) * .biHeight
ReDim bitmapData(0 To .biDataSize - 1)
End With
frmscrcontrol.Caption = UBound(bitmapData)
myDC = CreateCompatibleDC(0)
myDIB = CreateDIBSection(myDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
SelectObject myDC, myDIB
desktopDC = GetDC(0)
BitBlt myDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, desktopDC, X, Y, vbSrcCopy Or CAPTUREBLT
' Include mouse cursor?
If IncludeMouseCursor = True Then
pcin.cbSize = Len(pcin)
GetCursorInfo pcin
GetIconInfo pcin.hCursor, piinfo
DrawIcon myDC, pcin.ptScreenPos.X - piinfo.xHotspot, pcin.ptScreenPos.Y - piinfo.yHotspot, pcin.hCursor
If piinfo.hbmMask Then DeleteObject piinfo.hbmMask
If piinfo.hbmColor Then DeleteObject piinfo.hbmColor
End If
Call GetDIBits(myDC, myDIB, 0, bi24BitInfo.bmiHeader.biHeight, bitmapData(0), bi24BitInfo, DIB_RGB_COLORS)
' save as JPG
'------------
'Initialize GDI+
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI)
If lRes = 0 Then
' Create the GDI+ bitmap from the image handle
lRes = GdipCreateBitmapFromHBITMAP(myDIB, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
' Initialize the encoder GUID
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
' Initialize the encoder parameters
tParams.Count = 1
With tParams.Parameter ' Quality
' Set the Quality GUID
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.Type = 4
.Value = VarPtr(Quality)
End With
' Save the image
lRes = GdipSaveImageToFile(lBitmap, StrPtr(filename), tJpgEncoder, tParams)
' Destroy the bitmap
GdipDisposeImage lBitmap
End If
' Shutdown GDI+
GdiplusShutdown lGDIP
End If
If lRes Then
Err.Raise 5, , "Cannot save the image. GDI+ Error:" & lRes
End If
' CLEAN UP
ReleaseDC 0, desktopDC
DeleteObject myDIB
DeleteDC myDC
End Sub