Results 1 to 7 of 7

Thread: [RESOLVED] combine jpg side by side

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2013
    Posts
    1,126

    Resolved [RESOLVED] combine jpg side by side

    I have this two or more jpeg images of same height and width. Now if I combine the images side by side, I manually paste those images on any image editor like mspaint then save it to create the new image of the combined jpeg.

    I am thinking if it is possible than a vb6 application can combine them programmatically and if it is possible, how?

    I have found the code of dilettante --> http://www.vbforums.com/showthread.p...mages-into-one

    yeah its the one that I look for but in only for two image. what about it involves more than 2? I have seen the code but it was hard code for the number of image.
    Last edited by codesearcher; Jul 24th, 2015 at 08:56 PM.

  2. #2
    gibra
    Guest

    Re: combine jpg side by side

    You can try to use PaintPicture method to copy the second picture into first. Read help about numerous parameters.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: combine jpg side by side

    As gibra suggested:

    1. Set a picturebox.AutoRedraw to True and make it borderless
    2. Size it to: ImageHeight x (ImageWidth * nrImages)
    3. In a loop, for each image, load image and use PaintPicture to draw it to the picturebox at the appropriate X-coordinate
    4. After loop is done, call SavePicture to save the picturebox's .Image property as a bitmap.

    VB cannot save a composed image as JPG. GDI+ could be used for saving to JPG format
    Last edited by LaVolpe; Jul 25th, 2015 at 09:08 AM. Reason: typo
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: combine jpg side by side

    Pretty much what's already been said. This will do what you want and save as jpg
    Attached Files Attached Files
    Last edited by jmsrickland; Jul 25th, 2015 at 12:02 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2013
    Posts
    1,126

    Re: combine jpg side by side

    jm, thanks for this one. the loop code is useful for incorporation with other codes.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] combine jpg side by side

    Ouch, if you find loops a novelty you're in trouble.


    Yeah, VB6 has PaintPicture and Render methods built in and those work fine for many things but won't get you JPEG format for saving or even a BMP format byte-stream if you need to send it over a communication link or store it in a database.

    You can fall back on API calls to address those things, and they aren't too much trouble. Or you can do the entire thing with API calls and avoid the issues and overhead involved in using a PictureBox as a compositing canvas.

    Then there's that WIA 2.0 library. Part of Windows, you always have it available. It wraps quite a number of GDI+ operations, making it a handy image-fiddling multitool.


    Considering the many, many posts here on image compositing techniques you already have the tools. The programming seems to be the part you need help with. There are no clever algorithms to implement here since the image processing tools do all of the heavy lifting. That leaves nothing to write but a little glue logic to put them to use.

    Code:
    Private Function Stitch(ByVal SearchPattern As String) As ImageFile
        Const NEW_JPEG_QUALITY As Integer = 70 'In % where 100% = no compression.
        Dim FileName As String
        Dim I As Integer
        Dim Images() As ImageFile
        Dim StitchedWidth As Long
        Dim CanvasBytes() As Byte
        Dim NextLeft As Long
    
        FileName = Dir$(SearchPattern)
        Do While Len(FileName)
            If GetAttr(FileName) And Not vbDirectory Then
                ReDim Preserve Images(I)
                Set Images(I) = New ImageFile
                Images(I).LoadFile FileName
                StitchedWidth = StitchedWidth + Images(I).Width
                I = I + 1
            End If
            FileName = Dir$()
        Loop
    
        'Create blank "canvas" to stamp on:
        ReDim CanvasBytes(((StitchedWidth * 3& + 3&) \ 4&) * 4& * Images(0).Height - 1)
        With New Vector
            .BinaryData = CanvasBytes
            Erase CanvasBytes
            Set Stitch = .ImageFile(StitchedWidth, Images(0).Height)
        End With
    
        'Stamp source images onto canvas, convert to JPEG after last one:
        With New ImageProcess
            For I = 0 To UBound(Images)
                .Filters.Add .FilterInfos!Stamp.FilterID
                With .Filters(I + 1).Properties
                    Set !ImageFile = Images(I)
                    !Left = NextLeft
                End With
                NextLeft = NextLeft + Images(I).Width
            Next
            .Filters.Add .FilterInfos!Convert.FilterID
            With .Filters(I + 1).Properties
                !FormatID = wiaFormatJPEG
                !Quality = NEW_JPEG_QUALITY
            End With
            Set Stitch = .Apply(Stitch)
        End With
    End Function
    If you don't have the WIA 2.0 SDK, which is no longer a separate download, you have to look in the Windows SDK where all of this got moved beginning with the Vista SDK. Most of the information is still online at Windows Image Acquisition Automation Layer.

    The attachment contains sample images so it can be run "out of the box."
    Attached Files Attached Files
    Last edited by dilettante; Jul 25th, 2015 at 10:12 PM.

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] combine jpg side by side

    And here for comparison a version which is using another helper-lib (vbRichClient5)...

    The example below is splitting this task into reading the JPGs from a given Path
    into ByteArrays first (storing those ByteArrays within a Collection then).

    The second little Helper-Function below, does the combining then
    (from the JPGByteArrays, which were stored as Items within the Collection).

    Code:
    Private Function ReadJPGFilesFromPath(DL As cDirList) As cCollection
      Set ReadJPGFilesFromPath = New_c.Collection(False) 'the Function returns a new cCollection
       
      Dim i As Long
      For i = 0 To DL.FilesCount - 1 'enumerate the JPGs and add their Bytes into the Collection
        ReadJPGFilesFromPath.Add New_c.FSO.ReadByteContent(DL.Path & DL.FileName(i))
      Next
    End Function
    
    Private Function CombineAll(JPGs As cCollection) As cCairoSurface
      Dim i As Long, Srf As cCairoSurface, CC As cCairoContext
     
      Set Srf = Cairo.CreateSurface(0, 0, , JPGs.ItemByIndex(0)) 'decode the 1st JpgBytes into Srf
      Set CC = Cairo.CreateSurface(JPGs.Count * Srf.Width, Srf.Height).CreateContext 'create the large Destination-CC
      
      For i = 0 To JPGs.Count - 1 'enumerate the JPGs-ByteContents and render them onto the CC
        CC.RenderSurfaceContent Cairo.CreateSurface(0, 0, , JPGs.ItemByIndex(i)), i * Srf.Width, 0
      Next
      
      Set CombineAll = CC.Surface 'return the Destination-CCs underlying Pixel-Surface
    End Function
    Usage of the above (reading all JPG-Files in a given Path, then displaying their combination in the Form.Picture):
    Code:
    Private Sub Form_Click()
      Dim JPGs As cCollection
      Set JPGs = ReadJPGFilesFromPath(New_c.FSO.GetDirList(App.Path, dlSortByDisplayNameLogically, "*.jpg"))
      
      Set Picture = CombineAll(JPGs).Picture
    End Sub
    Or a bit more explicit, now rendering a WYSIWYG-Preview in a given JPG-Quality of the combined result:
    Code:
    Private Sub Form_Click()
      Dim JPGs As cCollection
      Set JPGs = ReadJPGFilesFromPath(New_c.FSO.GetDirList(App.Path, dlSortByDisplayNameLogically, "*.jpg"))
     
      Dim Srf As cCairoSurface, JpgBytesCombined() As Byte
      Set Srf = CombineAll(JPGs)
          Srf.WriteContentToJpgByteArray JpgBytesCombined, 80 'encode the combination into a JPGByteArray
          
      Set Picture = Cairo.CreateSurface(0, 0, , JpgBytesCombined).Picture 'WYSIWYG from these JPG-Bytes now
    End Sub
    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width