Please help; I can't figure out how to convert a bmp to gif. This is for a personal use program that only I will ever run.
Printable View
Please help; I can't figure out how to convert a bmp to gif. This is for a personal use program that only I will ever run.
open the file in photoshop then click save as, then below the filename, u select the file type. select gif.
ROFL :DQuote:
Originally Posted by jirohjoy
I have a module that will save it as a jpg, will that work for you?
I'm ready for bed so I'll post the jpg info now.
You'll need to find/ download the DILib.dll
Or I could email it to you.
Here's the module that goes with it:
modJpgDISave
Code:Option Explicit
'the mod requires DILIB.DLL, either in the folder with the app
'or in the proper windows dir and registered
'It takes a .bmp (the only image type VB can save) from a picture control
'then converts/saves it as a .jpg
Public Declare Function DIWriteJpg Lib "DILib.dll" Alias "#1" _
(DestPath As String, _
SrcPath As String, _
ByVal Quality As Long, _
Optional ByVal progressive As Long) As Long
Public Const DI_SUCCESS = 0
Public Const DI_ERR_CALL = 1
Public Const DI_ERR_INFILE = 2
Public Const DI_ERR_OUTFILE = 3
Public g_sPicFile As String
Public Function SaveAsJPG(ByRef objPicCntrl As Object, _
ByRef DestFilePath As String, _
Optional ByRef Quality As Long = 100) As String
'Takes the image from any VB picture control with a Picture Property
'and saves it to file
'Returns and empty string if Successful
'otherwise returns the error msg
Dim sTempBmp As String
Dim rtnVal As Long
Dim sMsg As String
If Quality > 100 Then
Quality = 100
ElseIf Quality < 1 Then
Quality = 1
End If
sTempBmp = AddSep(App.Path) & "temp.bmp"
SavePicture objPicCntrl.Picture, sTempBmp
DoEvents
rtnVal = DIWriteJpg(ByVal DestFilePath, ByVal sTempBmp, Quality) ', Abs(chkProg.Value))
DoEvents
SaveAsJPG = ErrMsg(rtnVal, DestFilePath, sTempBmp)
Kill sTempBmp
End Function
Private Function ErrMsg(ByRef ErrNumber As Long, ByRef JpgPath As String, ByRef SourcePath As String) As String
'converts the error number from the dll to a string
Dim sMsg As String
If ErrNumber <> DI_SUCCESS Then
sMsg = "DIWriteJpg did not succeed" & vbCrLf
Select Case ErrNumber
' Case DI_SUCCESS 'Success
' picJpg.Picture = LoadPicture(loadStr)
Case DI_ERR_CALL 'An error occured
sMsg = sMsg & "Error Call (0x00000001)"
Case DI_ERR_INFILE
sMsg = sMsg & "Error InFile (0x00000002): " & SourcePath
Case DI_ERR_OUTFILE
sMsg = sMsg & "Error OutFile (0x00000003): " & JpgPath
End Select
ErrMsg = sMsg
End If
End Function
Public Function AddSep(ByVal Path As String) As String
If RIGHT(Path, 1) = "\" Then
AddSep = Path
Else
AddSep = Path & "\"
End If
End Function
Jpegs are both too big and too lossy. I'm really looking for GIF.
I found many solutions, all of which require vic32.dll. After downloading several different versions of vic32.dll, I can't get past the following error message:
I don't need this to be a clean or professional solution, since as I said I'm the only one who will ever run it. If there were some way to open the file in paint and automate a Save As, that would be fine. Can you use Sendkeys to manipulate the file menu of paint?
Or a shareware conversion utility with command line processing would be fine as well. Mostly I need the compression I'm getting from paint, since I'm attaching these images to a messageboard with a small attachment limit.
Paint compresses the 9.4mb bitmaps down to 179k gifs.
Ever try this one?
EDIT: Re-Uploaded zip file (this one should be the original code).
GIFs can be worse, depending on how many colors are in the BMP. GIFs have a max of 256 colors.Quote:
Originally Posted by Ellis Dee
MS Paint for example, uses a default 216 color palette. Using some advanced adaptive palette algorithms +/- dithering can make the GIF appear better. Converting BMP to GIF can be done manually since the compression algorithm is pretty much open source -- but the the palette will determine the overall quality.
imagemagick might be a bit bulky for what you want but it will do the job, possibly better than many others, in theory it can be used in VB. As LaVolpe is saying the conversion from (presumably) 24 or 32 bit down to 8 bit is the key to the quality.
Here's a couple of links that you could use to code your own converter (not including the conversion to 8bit)
LZW Compression/Decompression Algorithm in VB
Gif spec
Alternatively you might want to consider converting to Ping, depending on the nature of the image Pings can compress to a similar size to Gifs without any data loss. (Photos convert badly)
Attached is a zip of one of the original bitmap images plus its gif counterpart after converting with paint so you guys can get a feel for what might work best.
I run a crawler each day and post the results to the group forum, but it is tedious to have to open paint and convert the image manually each day. Edgemeal's suggestion looks promising. It's slow -- which I don't care about at all -- but it creates a gif of 185k compared to the 177k in the archive. (Converting the same bitmap.) That's probably close enough.
Is there any way for a PNG or JPG to compress smaller while still leaving the text clearly legible? I'm open to any ideas; I'm not married to GIFs. Anything I can do to reduce the final image size would be highly desirable.
Using GDI+ I created a 177K GIF from that BMP, quality looks OK, guess you'll just have to try it.Quote:
Originally Posted by Ellis Dee
Did some playing with it.
The bmp only uses 7 colors so a gif is the way to go.
Using PSP and reducing the colorDepth to 8, took the gif size down to 129 kb.
The lowest setting I tried with the jpg code I posted was 20% quality.
It was almost 4 times the size of the gif, and artifacting badly.
The nature or your image is not suited to jpeg compression which is best suited for photographs. Ping works well, down to 40k without any data loss (66k if not indexed).Quote:
Originally Posted by Ellis Dee
Edit: Hmm, it seems photobucket has messed with the file by reducing the dimensions making the file size larger(??) attached is the 40KB version.
That is phenomenal; an ideal format. I have no clue how to automate the conversion, though. Little help?Quote:
Originally Posted by Milk
EDIT: Actually, looks like Edgemeal's posted solution can convert to png. I'll see if I can get it to work.
Hmm, Edgemeal's solution -- which is fantastic, thanks! -- creates a 195k png. How did you make the 40k version?
Edge, I'd rate you but it says I have to spread it around.
EDIT: Milk, did you create that 40k png using ImageMagick? I was quickly overwhelmed when looking at that site. Are there instructions on how to download a simple exe version anywhere?
Ellis Dee, will your app always run on XP or higher? Or will it always run on Win2K with GDI+ installed (not installed by default but is a free download for 2K). If not, converting to PNG requires a bunch more work.
Yes, only XP or higher. (It will only ever be run by me on my personal machine, which is XP.)Quote:
Originally Posted by LaVolpe
PNG uses several different filtering mechanisms to try and compress images more and more. The app is probably testing all 5 filters and picking the best one.Quote:
Originally Posted by Ellis Dee
I can beat the 195k -- how about 66kb? But I didn't use an application, I used old fashioned parsing, bit shifting & compression via the official zLIB.dll.
Edited: Found a miscalc in my routines, got it down to 51kb.
I do not know how to do that. Could you post the code you used?
You can also do this simply using WIA if you have it installed:
It does not do any extensive optimization of compression (yielding the same 176K to 194K files as already described).Code:Option Explicit
'
'Requires a reference to:
' Microsoft Windows Image Acquisition Library v2.0
'
Public Sub ImgConvert( _
ByVal InFileName As String, _
ByVal OutFileName As String, _
ByVal OutFormat As String)
Dim Img As WIA.ImageFile
Dim ImgProc As WIA.ImageProcess
Set Img = New WIA.ImageFile
Img.LoadFile InFileName
Set ImgProc = New WIA.ImageProcess
With ImgProc.Filters
.Add ImgProc.FilterInfos("Convert").FilterID
.Item(1).Properties("FormatID").Value = OutFormat
End With
Set Img = ImgProc.Apply(Img)
On Error Resume Next
Kill OutFileName
On Error GoTo 0
Img.SaveFile OutFileName
End Sub
Private Sub Main()
ImgConvert "Western City.bmp", "Western City.gif", wiaFormatGIF
ImgConvert "Western City.bmp", "Western City.png", wiaFormatPNG
MsgBox "Complete"
End Sub
The PNGOUT plugin for IrfanView (relying on KZIP deflation) can produce a PNG output at about 34.4KB. The stand alone command line PNGOUT utility might be useful from a VB6 program. PNGOUT Tutorial.
Attached is probably more than what you are expecting. Just unzip it to a new folder and run the project. I created a special form, for you, when it starts up. Let me know what you think; the best part is that because it is raw source code, you can customize/automate to your heart's desire.Quote:
Originally Posted by Ellis Dee
Edited: attachments removed
Edgemeal, this is driving me crazy. Why doesn't my picturebox have a numeric .Picture property like yours does? What's the trick?Quote:
Originally Posted by Edgemeal
I'd really like to use your technique because it's the only one that uses pure VB6 code; no references required. But no matter what I do, the following line always returns false:
If GdipCreateBitmapFromHBITMAP(hBitmap, 0&, lBitmap) = 0 Then
hBitmap is always 0 in my project, and always has a nice juicy number in yours. Even when I copied all your code over verbatim, and even copied the picturebox control from your project directly into mine. Still nada.
Please help!
Ellis Dee. Actually GDI+ is an external reference for systems less than XP. Win2k, NT4, and lower requires users to download the dll or get it from elsewhere (i.e., some other app installed it).
Regarding the error, hBitmap should be the GDI bitmap (i.e., picturebox's image handle), lBitmap should be non-zero if all went fine -- that would be the GDI+ reference.
One of my favorite reference sites for GDI+
http://www.com.it-berater.org/gdiplus/GdiPlus.htm
I copied all of his code verbatim, but the .Picture property of the picturebox gets passed as 0, which causes the call to fail.Quote:
Originally Posted by LaVolpe
Why would his picturebox's .Picture property have a value in his project, but then return 0 when I copy & paste it into my project?
And LaVolpe, not sure what's going on with the project you attached, but I get a blizzard of popups.
When opening the project, it first tells me an rtf file isn't found, which I ignore and continue to load anyway.
Then when I run it, it tells me I need to extract a dll from a RES file, which I say yes to. Then it tells me it wrote the dll to the app.path and that I should try again.
Lather, rinse, repeat. All it ever does is repeat these three messages, endlessly.
This just means you have no Bitmap loaded into the picturebox in your project.Quote:
Originally Posted by Ellis Dee
Ellis, sorry about that. The zLIB1.dll it wrote to your project folder obviously isn't in the system's DLL path. If you toss that DLL into your system path, all should be fine. I didn't include the RTF because it is rather large. If you decide you like the project, I can post the rtf too. Let me know.Quote:
Originally Posted by Ellis Dee
No tricks, just a valid image handle should do, for example instead of a picbox you could use the StdPicture obj.Quote:
Originally Posted by Ellis Dee
Code:Dim StdPic As StdPicture
Set StdPic = LoadPicture("FileName.BMP")
SavePictureFromHDC StdPic.Handle, "FileName.PNG"
Set StdPic = Nothing
Does an image file have to exist in order to have an image handle?
I'm drawing the image on the picturebox using Line and Print methods. I never do a LoadPicture() call.
Is that why I have no handle?
Try doing it like this,Quote:
Originally Posted by Ellis Dee
Picture1.AutoRedraw = True ' autodraw on
Picture1.Line (40, 40)-(400, 400) ' Draw stuff
Picture1.Picture = Picture1.Image ' copy image
SavePictureFromHDC.... ' save it
I'll try that later this morning when I have a minute and let you know how it works.
I really appreciate your help.
Try using the CommonDialog control to Open, change the extension. And then Close it, using those commands. Also I am not quite sure on the command to change the filename, but it will either be in a batch file usage or in VB.
Also you shouldn't try to use image compression, becuase it will remove chunks of code away from the file itself and then continue use of file compression will screw with the file's image quality. Image compression, is the last process on any image that you make using an image processing package, like Paint Shop Pro or even Photo Shop.
Or instead of using .Picture in the save command try using the .Image.Quote:
Originally Posted by Ellis Dee
' AutoRedraw on
' Draw stuff
SavePictureFromHDC(Picture1.Image,.... ' save it
Finally got around to trying your suggestions.Quote:
Originally Posted by Edgemeal
This works flawlessly; you rock. Thanks!
I'm marking this thread resolved, though I may post some followup questions as I get time to play with the PNG format. GIF will suffice in the meantime.