rahavtom Posted April 8, 2005 Posted April 8, 2005 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. Quote
Leaders snarfblam Posted April 8, 2005 Leaders Posted April 8, 2005 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. '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() 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). Quote [sIGPIC]e[/sIGPIC]
rahavtom Posted April 8, 2005 Author Posted April 8, 2005 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. '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() 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! I'll try it and tell you how it goes. Tom. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.