Need some urgent help please

rahavtom

Well-known member
Joined
Apr 2, 2004
Messages
63
Hi!

Can someone tell me please how can I merge two different images into one using visual basic .net?
I have one picturebox loaded with fixed image. I want to crreate a new "layer" of different image, allow the user to move and resize it, and then merge both images into the fixed one.

Thanks!
Tom Rahav.
 
Start by creating a bitmap. You can use the Graphics.FromImage() method to create a graphics object with which to edit your bitmap. Then use the DrawImage() method to merge the two images into one. If you have a color (other than one that is already transparent) which you want to represent transparency, the Bitmap class has a method, MakeTransparent(), which will make any one color become transparent.

Alternatively, you could create a graphics object for one of your existing images and simply draw the second image onto the first, resulting in less code and work, but you would lose the original image.

[VB]
This code assumes that both images are the same size
Create new bitmap
Dim MergedImage As New Bitmap(Layer1.Width, Layer1.Height, Imaging.PixelFormat.Format32bppArgb)
And graphics object to edit the image
Dim gMergedImage As Graphics = Graphics.FromImage(MergedImage)

Draw the first image
gMergedImage.DrawImage(Layer1, 0, 0)
Magenta will be treated as transparent
Layer2.MakeTransparent(Color.Magenta)

Draw the second image normally
gMergedImage.DrawImage(Layer2, 0, 0)

Draw the second image zoomed in
You can specify any size rectangle in any location to get zoom and offset
Dim DestRect As New Rectangle(-Layer2.Width \ 4, Layer2.Height \ 4, Layer2.Width * 2, Layer2.Height * 2)
gMergedImage.DrawImage(Layer2, DestRect)

If you want to show it to the user in a picturebox...
Picturebox1.Image = MergedImage
Picturebox1.Invalidate()
[/VB]

If you want your user to be able to move and resize the image with a preview, keep a reference to the bitmap and graphics objects. You can use the Graphics.Clear() method to clear the image and re-draw the composite image each time the user makes a change to the location or zoom (calling the PictureBox.Invalidate method to refresh the picturebox if you are using one).
 
marble_eater said:
Start by creating a bitmap. You can use the Graphics.FromImage() method to create a graphics object with which to edit your bitmap. Then use the DrawImage() method to merge the two images into one. If you have a color (other than one that is already transparent) which you want to represent transparency, the Bitmap class has a method, MakeTransparent(), which will make any one color become transparent.

Alternatively, you could create a graphics object for one of your existing images and simply draw the second image onto the first, resulting in less code and work, but you would lose the original image.

[VB]
This code assumes that both images are the same size
Create new bitmap
Dim MergedImage As New Bitmap(Layer1.Width, Layer1.Height, Imaging.PixelFormat.Format32bppArgb)
And graphics object to edit the image
Dim gMergedImage As Graphics = Graphics.FromImage(MergedImage)

Draw the first image
gMergedImage.DrawImage(Layer1, 0, 0)
Magenta will be treated as transparent
Layer2.MakeTransparent(Color.Magenta)

Draw the second image normally
gMergedImage.DrawImage(Layer2, 0, 0)

Draw the second image zoomed in
You can specify any size rectangle in any location to get zoom and offset
Dim DestRect As New Rectangle(-Layer2.Width \ 4, Layer2.Height \ 4, Layer2.Width * 2, Layer2.Height * 2)
gMergedImage.DrawImage(Layer2, DestRect)

If you want to show it to the user in a picturebox...
Picturebox1.Image = MergedImage
Picturebox1.Invalidate()
[/VB]

If you want your user to be able to move and resize the image with a preview, keep a reference to the bitmap and graphics objects. You can use the Graphics.Clear() method to clear the image and re-draw the composite image each time the user makes a change to the location or zoom (calling the PictureBox.Invalidate method to refresh the picturebox if you are using one).


Thank you so much!
Ill try it and tell you how it goes.
Tom.
 
Back
Top