|
-
Feb 4th, 2006, 12:11 PM
#1
Thread Starter
Junior Member
Merge images on picturebox
How I can join images after to have loaded them to the inside of the picturebox?
Sorry for English but I am Italian.
-
Feb 4th, 2006, 03:21 PM
#2
Addicted Member
Re: Merge images on picturebox
You'll need to take a copy of one of the images and draw it over the other with a selected transparecy level.
Here's a great class MrPolite made, but unfortunetly i didnt dave the project, so maybe you'll have figure it out yourself
Code:
Imports System.Drawing.Imaging
Namespace MrPolite
Public NotInheritable Class BitmapTexture
Dim a1 As Single = 0.299
' Класс texture
' Modifies the ORIGINAL bitmap
' textureTransparency has to be between 0 and 1,
' with 0 being the least transparent, and 1 the most transparent
Public Shared Sub ApplyTexture(ByVal bmp As Bitmap, _
ByVal texture As Bitmap, _
ByVal textureTransparency As Single)
'Публичеая функция применения текстуры(переменная как картинка, текстура как картинка,
'прозрачность текстуры как дробное число)
If (bmp Is Nothing) OrElse (texture Is Nothing) Then Throw New ArgumentNullException
'Если не определена начальная картинка или текстура, то выбросить ошибку
If textureTransparency < 0 OrElse textureTransparency > 1 Then
'Если указанная прозрачность не в рамках от 0 до 1, то выдать ошибку
Throw New ArgumentOutOfRangeException("textureTransparency must be between 0 and 1.")
End If
' Convert the texture to grayscale before using it
MakeImageGrayscale(texture)
'
Dim x, y, alpha As Integer
' Adjust the alpha value of each pixel in the texture bitmap.
' The white-most pixels will have the the most transparency (highest alpha),
' and the dark-most pixels will have the least transparency.
For x = 0 To texture.Width - 1
For y = 0 To texture.Height - 1
Dim c As Color = texture.GetPixel(x, y)
' c.R -> all of the RGB values are the same since the image is in grayscale
alpha = CInt(c.R * textureTransparency)
c = Color.FromArgb(alpha, c)
texture.SetPixel(x, y, c)
Next
Next
Dim gr As Graphics = Graphics.FromImage(bmp)
Dim myBrush As New TextureBrush(texture)
' Draw the texture over the original bitmap
gr.FillRectangle(myBrush, bmp.GetBounds(GraphicsUnit.Pixel))
myBrush.Dispose()
gr.Dispose()
End Sub
' Converts the image to grayscale
Private Shared Sub MakeImageGrayscale(ByVal bmp As Bitmap)
Dim cMatrix As New ColorMatrix(New Single()() _
{New Single() {0, 0, 1, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim imageAttrib As New ImageAttributes
imageAttrib.SetColorMatrix(cMatrix)
Dim gr As Graphics = Graphics.FromImage(bmp)
' Apply the grayscale image attribute
gr.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttrib)
gr.Dispose()
End Sub
End Class
End Namespace
Edit: ingore the strange comments, i forgot to take them away
They shout: "Give us cheap oil!"
What will they shout when all the oil is gone? "Give us another planet full of oil!"
1 Cigarete takes away 15 minutes of life, 1 minute of laughter gives you 15 minutes of life. What should you do?
Smoke weed! 
-
Feb 4th, 2006, 03:33 PM
#3
Re: Merge images on picturebox
So do you have say, 4 images in a picturebox, all arranged nice and neat, and now you just want to capture the image of the entire picturebox into one image?
If so, check out my signature, for the "Screen, Form, Control Image Capture" link. There is a way to get the image of a control, using the "capturewindow" method...
Last edited by gigemboy; Feb 4th, 2006 at 05:26 PM.
-
Feb 4th, 2006, 06:18 PM
#4
Thread Starter
Junior Member
Re: Merge images on picturebox
Perhaps they are not explained to me well, the two not overlapped images I must insert them to the inside of the picturebox but. But one of flank to the other.
-
Feb 4th, 2006, 06:25 PM
#5
Thread Starter
Junior Member
-
Feb 4th, 2006, 06:33 PM
#6
Re: Merge images on picturebox
you would essentially need to create a new bitmap equal to the height and width of both images together, and draw the new images onto the new bitmap... then set the new bitmap image onto the image property of the picturebox...
-
Feb 4th, 2006, 06:55 PM
#7
Thread Starter
Junior Member
Re: Merge images on picturebox
-
Feb 4th, 2006, 07:15 PM
#8
Re: Merge images on picturebox
Something like this:
VB Code:
'Create a new Bitmap to contain both the existing images.
Dim imageComposite As New Bitmap(imageLeft.Width + imageRight.Width, Math.Max(imageLeft.Height, imageRight.Height))
Dim g As Graphics = Graphics.FromImage(imageComposite)
'Draw the existing images on to the new composite image
g.DrawImage(imageLeft, 0, 0)
g.DrawImage(imageRight, imageLeft.Width, 0)
g.Dispose()
-
Feb 4th, 2006, 07:28 PM
#9
Thread Starter
Junior Member
Re: Merge images on picturebox
Since I have not never used the gdi would be best to have of the working code if it is possible.
-
Feb 4th, 2006, 07:47 PM
#10
Re: Merge images on picturebox
Are you saying that my code doesn't work? Have you tested it? If you've never used GDI+ before then it's probably best that you do some reading and experimentation of your own. Get an understanding of what you're doing rather than just letting someone else do it for you. How can you ever debug or change code that you don't understand?
-
Feb 5th, 2006, 04:02 AM
#11
Thread Starter
Junior Member
Re: Merge images on picturebox
It is not that I do not understand the code is that in place of imageleft and imageright if I insert the names of the picturebox as an example pic1 and pic2 the code gives error to me.
Why?
-
Feb 5th, 2006, 04:29 AM
#12
Re: Merge images on picturebox
'imageLeft' and 'imageRight' are not PictureBoxes. They are Images, as implied by the names and the fact that they were passed as parameters to DrawImage.
-
Feb 5th, 2006, 04:33 AM
#13
Thread Starter
Junior Member
Re: Merge images on picturebox
Inasmuch as I had not never tried the gdi+ I have read is I have tried this code and it works.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim imgAttributes As New ImageAttributes
Dim smallImage As Image = Image.FromFile("1.png")
e.Graphics.DrawImage(smallImage, New Rectangle(32, 32, 32, 32), 0, 0, smallImage.Width, smallImage.Height, GraphicsUnit.Pixel, imgAttributes)
End Sub
If I want to design such image in the picturebox as I make?
-
Feb 5th, 2006, 04:45 AM
#14
Thread Starter
Junior Member
Re: Merge images on picturebox
They are successful to design an image to the called inside of the picturebox toolbar with this code.
Dim g As Graphics = Toolbar.CreateGraphics
Dim smallImage As Image = Image.FromFile("1.png")
g.DrawImage(smallImage, 0, 0)
-
Feb 5th, 2006, 05:08 AM
#15
Thread Starter
Junior Member
Re: Merge images on picturebox
Ok ce I have made it! The code is this and the use for the other rows that I must load in the picturebox.
With OpenFile
Dim myGraphics As Graphics
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "Tutti i file immagine(*.png)|*.png;"
.FilterIndex = 2
If .ShowDialog = DialogResult.OK Then
Dim g As Graphics = Toolbar.CreateGraphics
Dim smallImage As Image = Image.FromFile(.FileName)
g.DrawImage(smallImage, 0, 0)
g.Dispose()
End If
End With
For the other rows change the coordinates. (32,0) (64,0).
thank you so much for the aid.
-
Feb 5th, 2006, 05:26 AM
#16
Re: Merge images on picturebox
I'm confused. Are you placing images on a toolbar? That seems odd if you are. Would you not just put buttons on the toolbar and then images on the buttons? Images on toolbar buttons is a very standard thing so the functionality is already built-in.
-
Feb 5th, 2006, 05:30 AM
#17
Thread Starter
Junior Member
Re: Merge images on picturebox
No toolbar is the name of the picturebox.
-
Feb 5th, 2006, 05:44 AM
#18
Re: Merge images on picturebox
That code still isn't going to work properly because if you use GDI+ to draw directly on a control you have to do it every time the control is repainted. As you are only doing it once the images will disappear as soon as the PictureBox is refreshed. Try dragging another form over it and I think you'll find that the images will be gone. Why not just do what you were asking for originally and create the composite image? Drawing three seperate images on a Bitmap is basically the same code as drawing them on a PictureBox. You just have to create the Bitmap object with the correct dimensions first. The code I provided is generic and will work regardless of the sizes of the images. If you know exactly what the dimensions are then you can use literal values instead of the properties that I've used. The advantage of using a Bitmap is that it is permanent and you just have to assign it to the PictureBox's Image property once.
-
Feb 5th, 2006, 05:58 AM
#19
Thread Starter
Junior Member
Re: Merge images on picturebox
In fact I have tried and when loaded the image in picturebox it hides. Therefore I must save the image in memory and then to pass it to the picturebox?
-
Feb 5th, 2006, 06:03 AM
#20
Thread Starter
Junior Member
Re: Merge images on picturebox
Ok i have made it!
How I make to save the contained image in the picturebox in a file?
-
Feb 5th, 2006, 06:27 AM
#21
Re: Merge images on picturebox
You would need to call the Save method of the Image class. If you have created a Bitmap already and assigned it to the Image property of the PictureBox then it's as simple as:
VB Code:
myPictureBox.Image.Save(filePath)
If you have used GDI+ to draw directly on the PictureBox then you're going to have to create a Image object anyway, draw on it and save it. If you aren't already drawing on a Bitmap like I suggested then you may as well switch as now that you want to save you have create an Image anyway.
-
Feb 5th, 2006, 07:26 AM
#22
Thread Starter
Junior Member
Re: Merge images on picturebox
Toolbar.Image.Save(.FileName, ImageFormat.Png)
Error!!!!
-
Feb 5th, 2006, 08:39 AM
#23
Thread Starter
Junior Member
Re: Merge images on picturebox
I have used gdi in order to copy the images in picturebox the hour I want to save the image of the picturebox in file as I make?
-
Feb 5th, 2006, 01:47 PM
#24
Re: Merge images on picturebox
Are you sure you set the Image property of the picturebox? If so, use Image.Save like JM sugggested, and you have to give it the FULL path to the file, as well as the filename and extension...
-
Feb 5th, 2006, 05:09 PM
#25
Re: Merge images on picturebox
 Originally Posted by ikaroweb
Toolbar.Image.Save(.FileName, ImageFormat.Png)
Error!!!!
Perhaps you'd consider giving us a little more information about that error? I'm not in the mood for guessing today.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|