I need help. I am developing an application wherein the user selects multiple images and are displayed on a form. With this application, i need to convert these multiple images into a single image. Any ideas?

Recommended Answers

All 9 Replies

Like, by having all the images side by side? If you can give me a better description, I can see what I can do.

Like, by having all the images side by side? If you can give me a better description, I can see what I can do.

Hello,
Thanks for the interest.
Its like - the user has a choice of 15 -20 images, he may select some images, usually in 2 x 2 or 2 x 3 or 2 x 4 or 3 x 3 formats. At present I am displaying them in single picture boxes, placed according to the selected format on the form.
The requirement now is to group all these images to form a single jpg image, through the VB program, without the user opening any other programs like Photoshop or anything else.
I tried displaying them on Crystal report, but it becomes bulky.
I tried exporting the Crystal report to pdf and saving it, but the requirement is to save as jpg.
Anyway to convert pdf to jpg? There are a few softwares available, but can we do it through VB code?

None that I know of.... in fact, to my knowledge, I haven't found VB code that will save a picturebox to a jpg.... all the methods I know of, save the files as bitmaps. I can tell you how to put all the pictures in 1 picturebox, and I can tell you how to save that picture to a .bmp file.... but that doesn't do you any good. Now, I have found this class, but I've never used it before, and it claims to work: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=50065&lngWId=1.

I will download the class and try it.
You mentioned about displaying multiple images in one picture box and save it as a bmp, how is this done?

Saving images as .jpg, .png, .gif, etc. from within VB is possible with GDI+, but since GDI+ is a technology that came around after VB6, you either have to upgrade to .NET (which I'm avoiding ATM), or get something which will allow you to access the GDI+ library.

You're in luck when it comes to the latter option. At vbAccelerator there is a project - still in development, unfortunately, but read on - which exposes the GDI+ library to VB6 (and likely VB5). The most important part is the GDI+ type library (which is fully functional and not in development). This gives you direct access to the GDI+ API. However, since any API is hard to work with in VB, there is also a GDI+ wrapper DLL (the part which is in development) designed to abstract all of the API stuff with VB classes, objects, methods, and properties. So far, image and bitmap support are complete. For more details, see the links.

As to drawing multiple images in the same PictureBox, here's where you would use the PaintPicture method of the PictureBox. Also, just so you know, Forms also have the PaintPicture method.

Let's say you use your current set-up, and you only want to make everything into a single image during the save process. You could in fact use this same code if you were to allow the user to move the PictureBoxes around on the form into whatever position pleases them most. The code is deliberately designed to work no matter how many PictureBoxes you have on the form - just be sure to set their Tag properties to "Collage" before you run this Sub. The Sub uses a hidden PictureBox control large enough to hold everything you want to save (pbxHiddenCollage), so don't forget to create that, too.

Public Sub CombineCollage()
    '   This array is needed because of the way the ZOrder works.
    Dim colPbx() As PictureBox
    ReDim colPbx(0)
    '   Common Dialog to let the user pick a filename for saving.
    cdlImage.DefaultExt = ".bmp"
    cdlImage.DialogTitle = "Save Collage to an Image"
    cdlImage.Filter = "Bitmap File (*.bmp)|*.bmp"
    '   CancelError is set to True.  I always do this.
    On Error GoTo SaveImageCancelled
    cdlImage.ShowSave
    On Error GoTo 0
    '   Get the hidden PictureBox set up to create the combined image.
    pbxHiddenCollage.AutoRedraw = True
    pbxHiddenCollage.Cls
    '   Find every PictureBox on the form whose Tag is "Collage".
    For Each ctlObj In Me.Controls
        If ((TypeOf ctlObj Is PictureBox)) And ctlObj.Tag = "Collage" Then
            ReDim Preserve colPbx(UBound(colPbx) + 1)
            Set colPbx(UBound(colPbx)) = ctlObj
        End If
    Next
    '   Draw the combined image on the hidden PictureBox.
    For countVar = UBound(colPbx) To 1 Step -1
        pbxHiddenCollage.PaintPicture colPbx(countVar).Image, _
            colPbx(countVar).Left, colPbx(countVar).Top, _
            colPbx(countVar).Width, colPbx(countVar).Height, _
            0, 0, colPbx(countVar).Width, colPbx(countVar).Height
    Next
    '   Here's where you save to .bmp
    SavePicture pbxHiddenCollage.Image, cdlImage.FileName
    '   Be careful not to execute the error handler!
    Exit Sub
SaveImageCancelled:    '   Error handler - the user pressed Cancel
    On Error GoTo 0
    Exit Sub           '   Makes adding new error handlers easier.
End Sub

Hope this helps!

- Sendoshin

Well said, beyond that you could also use the bitblt API call, with device contexts (which, is how making Video games with just VB and API is done). I don't suggest building an app that relies on GDI+, since it's added overhead, and possibly an install requirement for the user using the application, but you are very limited to working with JPEGs in VB6. The bitblt API call will be much faster in processing the images, but chances are you are not going to notice a difference either way.... since delving into the API can become a fairly complex playground, I suggest the code above.

Thanks. I will try this.

save the code below as .vbs and dragndrop one or multiple bmp files on it. all files will be converted to png in a new created subdirectory.

' File Name:MultiBmpToJpg.vbs
Const xlHtml = 44: Dim Args, pPath, file, fso, f
Set Args = WScript.Arguments
If Args.Count = 0 Then WScript.Quit

'msgbox args.count
for a = 0 to Args.count - 1
CreateObject("WScript.Shell").Popup "Converting image " & a + 1 & " of " & args.count & "...", 1, " ", vbOKOnly

If LCase(Right(Args(a), 4)) <> ".bmp" Then WScript.Quit

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(Args(a))
file = f.name
pPath = left(Args(a),Len(Args(a)) - len(file))

Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(pPath & "\png") Then
Set objFolder = oFSO.CreateFolder(pPath & "\png")
End If

With CreateObject("Excel.Application")
With .Workbooks.Add(1)
With .ActiveSheet
.Pictures.Insert(Args(a)): .Shapes(1).Cut
.PasteSpecial "Picture (JPEG)"
End With
.SaveAs pPath & "Temporary.htm", xlHtml: .Close False
End With
.Quit
End With

With CreateObject("Scripting.FileSystemObject")
.CopyFile pPath & "Temporary_files\image001.png", _
Replace(pPath & "\png\" & file, ".bmp", ".png", 1, -1, 1)
.DeleteFile pPath & "Temporary.htm"
.DeleteFolder pPath & "Temporary_files", True
End With

next
Msgbox "Conversation done." 

Please note that you are posting a solution to a question that was asked 9 years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.