[COLOR=blue]Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type
Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type
Private Type PictDesc
cbSizeofStruct As Long
PicType As Long
hImage As Long
xExt As Long
yExt As Long
End Type
Private Declare Function GdiplusStartup Lib "GDIPlus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal Token As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hpal As Long, BITMAP As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicArray As Any, RefIID As Any, ByVal OwnsHandle As Long, IPic As Any) As Long
Const PICTYPE_BITMAP = 1
Const CF_BITMAP = 2
Public Sub example()
Dim ClipboardPic As Picture
If OpenClipboard(0&) Then
Set ClipboardPic = HandleToPicture(GetClipboardData(CF_BITMAP), PICTYPE_BITMAP) [COLOR=green]' assumes we have a bitmap on the clipboard[/color]
CloseClipboard
If Not ClipboardPic Is Nothing Then
SaveAsJPG ClipboardPic, "d:\downloads\deleteme\test.jpg"
Else
MsgBox "Could not find bitmap on clipboard"
End If
Else
MsgBox "Could not open Clipboard"
End If
End Sub
Private Function HandleToPicture(hHandle As Long, PicType As Long) As Picture
Dim pd As PictDesc
Dim IPic As GUID
If hHandle = 0 Then Exit Function [COLOR=green]' no handle to any type of image[/color]
pd.cbSizeofStruct = Len(pd)
pd.PicType = PicType
pd.hImage = hHandle
CLSIDFromString ByVal StrPtr("{00020400-0000-0000-C000-000000000046}"), IPic
OleCreatePictureIndirect pd, IPic, -1, HandleToPicture
End Function
Private Sub SaveAsJPG(ByVal SrcPicture As StdPicture, ByVal filename As String, Optional ByVal quality As Byte = 80)
Dim gdiInput As GdiplusStartupInput
Dim gdiStatus As Long
Dim Token As Long
Dim gdiImage As Long
Dim gdiJPGEncoder As GUID
Dim gdiEncoderParams As EncoderParameters
[COLOR=green]' Initialize GDI+[/color]
gdiInput.GdiplusVersion = 1
gdiStatus = GdiplusStartup(Token, gdiInput)
If gdiStatus = 0 Then
[COLOR=green]' Create the GDI+ bitmap from the image handle[/color]
gdiStatus = GdipCreateBitmapFromHBITMAP(SrcPicture.Handle, SrcPicture.hpal, gdiImage)
If gdiStatus = 0 Then ' Successful?
[COLOR=green]' Initialize the encoder GUID
' assumes platform has a JPG codec; in real world we might want to enumerate codecs to see if this assumption is true. See my gdi+ code in thread222-1686785 for one way of doing this[/color]
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), gdiJPGEncoder
[COLOR=green]' Initialize the encoder parameters - we are setting up Quality encoder[/color]
gdiEncoderParams.Count = 1
With gdiEncoderParams.Parameter
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.type = 4
.Value = VarPtr(quality) '0-100%
End With
[COLOR=green]' Save the image[/color]
gdiStatus = GdipSaveImageToFile(gdiImage, StrPtr(filename), gdiJPGEncoder, gdiEncoderParams)
[COLOR=green]' Cleanup[/color]
GdipDisposeImage gdiImage
End If
[COLOR=green]' Shutdown GDI+[/color]
If Token Then GdiplusShutdown Token
End If
If gdiStatus Then msgbox "Could not save - GDI+ error"
End Sub[/color]