Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save licturebox as jpeg 2

Status
Not open for further replies.

x508

Programmer
Jun 26, 2003
396
ZA
Hi,
Is it possible to save the picturebox as a jpeg

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Which won't save it as a jpeg, just as a bitmap (albeit with a jpg file extension)

The OLE Picture object (which is what VB's Picture object really is) only know's how to load jpegs (and a variety of other graphics formats), but only knows save bitmaps.

So, you either need to get hold of a third party jpeg handler (and I've often recommended in this forum), or consider using the GDI+ API, which supports a lot more graphics formats. GDI+ is really written to be used by .NET languages, but is useable from VB6 if you get hold of the GDI+ type library (available here: I'd point you to my fully worked and explained GDI+ JPG example in this forum (Thread222-651652) if it hadn't somehow been deleted. I still have the original code as I wrote it (although not all the explanations about how it works), but it is at home, so i won't be able to repost it for about 12 hours
 
Thanks, I checked out the first link for the 3rd party dll, is it free?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Be a tad careful with the MS link. It fails to point out that GDI+ is already installed on XP
 
Here's the text within the redist.txt...

===========
Gdiplus.dll
===========

For Windows XP use the system-supplied gdiplus.dll. Do not install a new gdiplus.dll over the system-supplied version (it will fail due to Windows File Protection).

For Windows 2000, Windows Millennium Edition, Windows NT 4.0 and Windows 98, install gdiplus.dll into the private directory of the application not into the system directory.

In addition to the rights granted in Section 1 of the Agreement ("Agreement"), with respect to gdiplus.dll for Windows 2000, Windows Millennium Edition, Windows NT 4.0 and Windows 98, you have the following non-exclusive, royalty free rights subject to the Distribution Requirements detailed in Section 1 of the Agreement:

(1) You may distribute gdiplus.dll solely for use with Windows 2000, Windows Millennium Edition, Windows NT 4.0 and Windows 98.
 
Yes - once you've downloaded it you can find that out. I'm just saying that the download page doesn't point it out...
 
It's worth pointing out that in my original, but now missing presumed deleted, GDI+ thread I included links to Microsoft's GDI+ dll download page, the GDI+ type library and to info about Dana Seaman and the GDI+ Wrapper library. So thanks for reminding me about them all.
 
So then, the GDIPLUSS dll comes with WinXP and I did not actually have to download it?

Something Else:

What happens when I call on a (non system)DLL from VB, and I create my installion package...will it "see" this dll to include in dependencies?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
>the GDIPLUSS dll comes with WinXP and I did not actually have to download it?

Exactly so
 
hmmm, so that means that it is actually quite easy to convert a bmp to JPG from within VB^ and XP

Sorry for being a pain.....

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Easy? Well, it depends on your definition of easy

Firstly, you can't use most of the GDI+ library from VB. So you have to use a type library such as the one I mention earlier.

Secondly, none of the MS documentation discusses using GDI+ from VB (no examples, etc.) because it is not designed to be used from VB - so you'll have to figure much of it out yourself. When I get around to reposting my GDI+ code later today, you'll see how 'easy' using GDI+ and this type library is

Thirdly, you can make this easier on yourself by downloading the GDI+ Wrapper library - but now you've got to deal with a 3rd party library that requires redistributon with your application (type libraries do not need to be redistributed). And if you are going to be redistributing a 3rd party library, you might actually find that there are easier ones to use for JPG conversion (such as DIjpg.dll as mentioned earlier)
 
OIC, allright..you can have one more star *

While you are listening, and I know i'm not supposed to cross my threads, but I'm getting no reply on my other...

Where do I go to learn more about the sendmessageA thingy?
Especially for using the AVICAP32.DLL?


Any help will be greatly,greatly,greatly appreciated

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
I've given an answer in the other thread to head off the thread hijacking...
 
I saw it, thanks

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
And now I've found it again...OK, for this example you'll need a form with a picture box and a couple of a command buttons. The code assumes that the picturebox has a picture in it.
[tt]
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (lpString As Any) As Long
Private Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrW" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long

' Lists the codecs we have available
Private Sub Command1_Click()
Dim GpInput As GdiplusStartupInput
Dim Token As Long
Dim Encoders As EncoderParameters
Dim lEncoders As Long
Dim lEncodersBuffer As Long
Dim arrEncoders() As ImageCodecInfo
Dim lp As Long

' Load the GDI+ Dll
GpInput.GdiplusVersion = 1
If GdiplusStartup(Token, GpInput) <> Ok Then
MsgBox &quot;Error loading GDI+!&quot;, vbCritical
Else
GdipGetImageEncodersSize lEncoders, lEncodersBuffer
ReDim arrEncoders(lEncoders - 1) As ImageCodecInfo
GdipGetImageEncoders lEncoders, lEncodersBuffer, arrEncoders(0)

GdiplusShutdown Token

For lp = LBound(arrEncoders) To UBound(arrEncoders)
Debug.Print StringFromPointer(arrEncoders(lp).MimeTypePtr)
Next
End If
End Sub

Private Sub Command2_Click()
Dim GpInput As GdiplusStartupInput
Dim Token As Long
Dim myCLSID As CLSID
Dim myEncoderParameters As EncoderParameters
Dim myEncoder(0) As EncoderParameter
Dim gdiImage As Long
Dim Quality As Long

' Get and check we have a CLSID for a jpeg codec
If GetEncoderCLSID(&quot;image/jpeg&quot;, myCLSID) Then
GpInput.GdiplusVersion = 1
If GdiplusStartup(Token, GpInput) <> Ok Then
MsgBox &quot;Error loading GDI+!&quot;, vbCritical
Else
myEncoderParameters.Count = 1
myEncoder(0).Guid = CLSIDFromString(EncoderQuality)
myEncoder(0).Type = EncoderParameterValueTypeLong
myEncoder(0).NumberOfValues = 1
Quality = 25 ' 0 to 100%
myEncoder(0).ValuePtr = VarPtr(Quality)
myEncoderParameters.Parameter = myEncoder(0)
GdipCreateBitmapFromHBITMAP Picture1.Picture.Handle, Picture1.Picture.hPal, gdiImage
Debug.Print GdipSaveImageToFile(gdiImage, &quot;c:\test.jpg&quot;, myCLSID, myEncoderParameters)
GdiplusShutdown Token
End If
End If
End Sub

' helper function to get CLSID of output codec
Private Function GetEncoderCLSID(ByVal strMimeType As String, outCLSID As CLSID) As Long
Dim GpInput As GdiplusStartupInput
Dim Token As Long
Dim lEncoders As Long
Dim lEncodersBuffer As Long
Dim lp As Long
Dim arrEncoders() As ImageCodecInfo

GpInput.GdiplusVersion = 1
If GdiplusStartup(Token, GpInput) <> Ok Then
MsgBox &quot;Error loading GDI+!&quot;, vbCritical
Else
GdipGetImageEncodersSize lEncoders, lEncodersBuffer
ReDim arrEncoders(lEncoders - 1) As ImageCodecInfo
GdipGetImageEncoders lEncoders, lEncodersBuffer, arrEncoders(0)

GdiplusShutdown Token

GetEncoderCLSID = False
For lp = LBound(arrEncoders) To UBound(arrEncoders)
If StringFromPointer(arrEncoders(lp).MimeTypePtr) = strMimeType Then
outCLSID = arrEncoders(lp).CLSID
GetEncoderCLSID = True
Exit For
End If
Next

End If

End Function


' Works for strings that are nullchar terminated
Public Function StringFromPointer(lpString As Long) As String
Dim sRet As String
Dim lret As Long
Dim lMaxLength As Long
Dim lCodec


If lpString = 0 Then
StringFromPointer = &quot;&quot;
Exit Function
End If

lMaxLength = lstrlen(ByVal lpString) * 2

If IsBadStringPtrByLong(lpString, lMaxLength) Then
StringFromPointer = &quot;&quot;
Exit Function
End If

sRet = Space$(lMaxLength)
CopyMemory ByVal sRet, ByVal lpString, Len(sRet)

StringFromPointer = StrConv(sRet, vbFromUnicode) 'sRet
End Function

 
Thanks strongm...

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top