Jump to content

Join two bitmap images?


qwert
 Share

Recommended Posts

Would anyone happen to know of a function ... or technique ... or utility ... to join two equal-sized bitmap (BMP) images horizontally? If it's a utility, it would need to be callable (with parameters) from a script -- not interactive from a GUI.

Thanks in advance for any help.

Edited by qwert
Link to comment
Share on other sites

I guess you could use GDI+, the parameters you need to find you'rself.

UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.
Link to comment
Share on other sites

GDIPlus udf can do it easily enough.

Have a look under User Defined Functions -> GDIPlus Management in the help file.

I'd write an example but I'm just about to head off to work.

Be back in 3 days, if no one has assisted or you haven't worked it out I'll post an example if need be.

Link to comment
Share on other sites

Would anyone happen to know of a function ... or technique ... or utility ... to join two equal-sized bitmap (BMP) images horizontally? If it's a utility, it would need to be callable (with parameters) from a script -- not interactive from a GUI.

Thanks in advance for any help.

Hi! Example:

#include <GDIPlus.au3>

_BMPJoin(@ScriptDir & "\01.bmp", @ScriptDir & "\02.bmp", @ScriptDir & "\result.bmp")

Func _BMPJoin($sFile1, $sFile2, $sResult)
    Local $hSourceIMG1, $hSourceIMG2, $iWidth, $iHeight, $hBitmap, $hImage, $hGraphic
    
    _GDIPlus_Startup()
    
    $hSourceIMG1 = _GDIPlus_ImageLoadFromFile($sFile1)
    $hSourceIMG2 = _GDIPlus_ImageLoadFromFile($sFile2)
    
    $iWidth = _GDIPlus_ImageGetWidth($hSourceIMG1)
    $iHeight = _GDIPlus_ImageGetHeight($hSourceIMG1)

    $hBitmap = _CreateBMP($iWidth * 2, $iHeight)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG1, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG2, 0, 0, $iWidth, $iHeight, $iWidth, 0, $iWidth, $iHeight)

    _GDIPlus_ImageSaveToFile($hImage, $sResult)

    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hSourceIMG1)
    _GDIPlus_ImageDispose($hSourceIMG2)
    _GDIPlus_Shutdown()
EndFunc   ;==>_BMPJoin

Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd, $hDC, $hBMP
    
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    Return $hBMP
EndFunc   ;==>_CreateBMP

about:

it would need to be callable (with parameters) from a script -- not interactive from a GUI.

I hope you make this himself. :)
Link to comment
Share on other sites

OK, i found a time for complete this:

#include <GDIPlus.au3>

Switch $CmdLine[0]
    Case 0
        _SyntaxHelp()
    Case 1
        _SyntaxHelp()
    Case 2
        If Not FileExists($CmdLine[1]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        ElseIf Not FileExists($CmdLine[2]) Then
            MsgBox(48, "JoinImage", "Second file not exists. Check path")
            Exit
        EndIf
        _BMPJoin($CmdLine[1], $CmdLine[2], @ScriptDir & "\result.bmp")
    Case 3
        If Not FileExists($CmdLine[1]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        ElseIf Not FileExists($CmdLine[2]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        EndIf
        _BMPJoin($CmdLine[1], $CmdLine[2], $CmdLine[3])
EndSwitch

Func _SyntaxHelp()
    MsgBox(64, "JoinImage", "Command line parameters:" & @LF & @LF & _
           "JoinImage.exe FirstFile.bmp SecondFile.bmp [Result.bmp]")
    Exit
EndFunc

Func _BMPJoin($sFile1, $sFile2, $sResult)
    Local $hSourceIMG1, $hSourceIMG2, $iWidth, $iHeight, $hBitmap, $hImage, $hGraphic
    
    If Not FileExists(StringRegExpReplace($sResult, "(^.*)\\.*", "\1")) Then $sResult = @ScriptDir & "\result.bmp"
    
    _GDIPlus_Startup()
    
    $hSourceIMG1 = _GDIPlus_ImageLoadFromFile($sFile1)
    $hSourceIMG2 = _GDIPlus_ImageLoadFromFile($sFile2)
    
    $iWidth = _GDIPlus_ImageGetWidth($hSourceIMG1)
    $iHeight = _GDIPlus_ImageGetHeight($hSourceIMG1)

    $hBitmap = _CreateBMP($iWidth * 2, $iHeight)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG1, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG2, 0, 0, $iWidth, $iHeight, $iWidth, 0, $iWidth, $iHeight)

    _GDIPlus_ImageSaveToFile($hImage, $sResult)

    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hSourceIMG1)
    _GDIPlus_ImageDispose($hSourceIMG2)
    _GDIPlus_Shutdown()
EndFunc   ;==>_BMPJoin

Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd, $hDC, $hBMP
    
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    Return $hBMP
EndFunc   ;==>_CreateBMP

Compile this script and use so: JoinImage.exe FirstFile.bmp SecondFile.bmp [Result.bmp] :)

Link to comment
Share on other sites

OK, i found a time for complete this ...

rasim, that is a wonderful script!

Not only does it do exactly what I needed, it's so well written that it also provides an excellent example of using the GDIPlus functions in general. I've learned a lot from it.

THANK YOU!

Link to comment
Share on other sites

OK, i found a time for complete this:

#include <GDIPlus.au3>

Switch $CmdLine[0]
    Case 0
        _SyntaxHelp()
    Case 1
        _SyntaxHelp()
    Case 2
        If Not FileExists($CmdLine[1]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        ElseIf Not FileExists($CmdLine[2]) Then
            MsgBox(48, "JoinImage", "Second file not exists. Check path")
            Exit
        EndIf
        _BMPJoin($CmdLine[1], $CmdLine[2], @ScriptDir & "\result.bmp")
    Case 3
        If Not FileExists($CmdLine[1]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        ElseIf Not FileExists($CmdLine[2]) Then
            MsgBox(48, "JoinImage", "First file not exists. Check path")
            Exit
        EndIf
        _BMPJoin($CmdLine[1], $CmdLine[2], $CmdLine[3])
EndSwitch

Func _SyntaxHelp()
    MsgBox(64, "JoinImage", "Command line parameters:" & @LF & @LF & _
           "JoinImage.exe FirstFile.bmp SecondFile.bmp [Result.bmp]")
    Exit
EndFunc

Func _BMPJoin($sFile1, $sFile2, $sResult)
    Local $hSourceIMG1, $hSourceIMG2, $iWidth, $iHeight, $hBitmap, $hImage, $hGraphic
    
    If Not FileExists(StringRegExpReplace($sResult, "(^.*)\\.*", "\1")) Then $sResult = @ScriptDir & "\result.bmp"
    
    _GDIPlus_Startup()
    
    $hSourceIMG1 = _GDIPlus_ImageLoadFromFile($sFile1)
    $hSourceIMG2 = _GDIPlus_ImageLoadFromFile($sFile2)
    
    $iWidth = _GDIPlus_ImageGetWidth($hSourceIMG1)
    $iHeight = _GDIPlus_ImageGetHeight($hSourceIMG1)

    $hBitmap = _CreateBMP($iWidth * 2, $iHeight)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG1, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG2, 0, 0, $iWidth, $iHeight, $iWidth, 0, $iWidth, $iHeight)

    _GDIPlus_ImageSaveToFile($hImage, $sResult)

    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hSourceIMG1)
    _GDIPlus_ImageDispose($hSourceIMG2)
    _GDIPlus_Shutdown()
EndFunc   ;==>_BMPJoin

Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd, $hDC, $hBMP
    
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    Return $hBMP
EndFunc   ;==>_CreateBMP

Compile this script and use so: JoinImage.exe FirstFile.bmp SecondFile.bmp [Result.bmp] :)

Yes a nice script rasim. It is sure to be useful to other people and I thought it would be better to assume that the image sizes could be different and allow having the second image either to the right of the first or below it. By swapping the image parameters you can then acheive any position of one image relative to the other.

#include <GDIPlus.au3>

_BMPJoin("C:\Documents and Settings\Martin\My Documents\images\xr4andsaw.jpg","C:\Documents and Settings\Martin\My Documents\images\cutlass.jpg","joined.bmp")
Func _BMPJoin($sFile1, $sFile2, $sResult, $Position = "right")
    Local $hSourceIMG1, $hSourceIMG2, $iWidth1, $width2, $iHeight1, $iHeight2, $hBitmap, $hImage, $hGraphic
    Local $iBwidth, $iBheight

    If Not FileExists(StringRegExpReplace($sResult, "(^.*)\\.*", "\1")) Then $sResult = @ScriptDir & "\result.bmp"

    _GDIPlus_Startup()

    $hSourceIMG1 = _GDIPlus_ImageLoadFromFile($sFile1)
    $hSourceIMG2 = _GDIPlus_ImageLoadFromFile($sFile2)

    $iWidth1 = _GDIPlus_ImageGetWidth($hSourceIMG1)
    $iHeight1 = _GDIPlus_ImageGetHeight($hSourceIMG1)
    $iHeight2 = _GDIPlus_ImageGetHeight($hSourceIMG2)

    $iWidth2 = _GDIPlus_ImageGetWidth($hSourceIMG2)
    $iHeight2 = _GDIPlus_ImageGetHeight($hSourceIMG2)
    If $Position = "right" Then
        $iBwidth = $iWidth1 + $iWidth2
        $iBheight = $iHeight1
        if $iHeight2 > $iHeight1 then $iBheight = $iHeight2
    Else;position = "below"
        $iBwidth = $iWidth1
        if $iWidth2 > $iWidth1 then $iBwidth = $iWidth2
        $iBheight = $iHeight1 + $iHeight2
    EndIf
    
    
    $hBitmap = _CreateBMP($iBwidth, $iBheight)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG1, 0, 0, $iWidth1, $iHeight1, 0, 0, $iWidth1, $iHeight1)
    If $Position = "right" Then
        _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG2, 0, 0, $iWidth2, $iHeight2, $iWidth1, 0, $iWidth2, $iHeight2)
    Else
        _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hSourceIMG2, 0, 0, $iWidth2, $iHeight2, 0, $iHeight1, $iWidth2, $iHeight2)
    EndIf


    _GDIPlus_ImageSaveToFile($hImage, $sResult)

    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hSourceIMG1)
    _GDIPlus_ImageDispose($hSourceIMG2)
    _GDIPlus_Shutdown()
EndFunc  ;==>_BMPJoin

Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd, $hDC, $hBMP

    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    Return $hBMP
EndFunc  ;==>_CreateBMP
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...