How to combine two images

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

How to combine two images

Post by bill-lancaster »

I have two images, square.png and circle.png, both the same size and are black & white.
How can I merge them so that that are both shown in a new image?
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to combine two images

Post by BruceSteers »

please describe "merge"

should they be next to each other or one on top of the other.

do they have transparent backgrounds?

Can you not just use Paint.classs to paint one image on the other?


Paint.Begin(hSquarePic)
Paint.DrawPicture(hRoundPic, 0, 0, Paint.W, Paint.H)
Paint.End

If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to combine two images

Post by bill-lancaster »

This does it!
For x = 0 To hImageSquare.W - 1
For y = 0 To hImageSquare.H - 1
If hImageSquare[x, y] <> -16777216 Then hImageNew[x, y] = hImageSquare[x, y]
If hImageCircle[x, y] <> -16777216 Then hImageNew[x, y] = hImageCircle[x, y]
Next
Next

The value 16777216 is the colour of the background. 16777215 = White
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to combine two images

Post by bill-lancaster »

Thanks Bruce, by 'merge' I mean the overlaying one image over the other.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to combine two images

Post by BruceSteers »

Whatever works for you dude.

does it have to be an image that you read/overlay?
you could just draw it


Dim p As Picture = New Picture(300, 300)
p.Image.Fill(Color.White) ' fill white background

Paint.Begin(p)

'  draw a blue square with 10 px margin
Paint.FillRect(10, 10, 280, 280, Color.blue)

' draw a green circle, centered and radius 10px less than image size
Paint.Background = Color.green
Paint.Arc(150, 150, 140)
Paint.Fill()
Paint.End
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to combine two images

Post by bill-lancaster »

and Paint.DrawImage(hImageCircle, 0, 0, 112, 112)
works just fine.
Thanks again
Post Reply