Results 1 to 25 of 25

Thread: Merge images on picturebox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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.

  2. #2
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    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!

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    Re: Merge images on picturebox

    I have these two images.





    What I want to make is this.

    .

    OK.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    Re: Merge images on picturebox

    Yes. And as I make?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Merge images on picturebox

    Something like this:
    VB Code:
    1. 'Create a new Bitmap to contain both the existing images.
    2.         Dim imageComposite As New Bitmap(imageLeft.Width + imageRight.Width, Math.Max(imageLeft.Height, imageRight.Height))
    3.         Dim g As Graphics = Graphics.FromImage(imageComposite)
    4.  
    5.         'Draw the existing images on to the new composite image
    6.         g.DrawImage(imageLeft, 0, 0)
    7.         g.DrawImage(imageRight, imageLeft.Width, 0)
    8.  
    9.         g.Dispose()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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?

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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)

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    Re: Merge images on picturebox

    No toolbar is the name of the picturebox.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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?

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    Re: Merge images on picturebox

    Ok i have made it!
    How I make to save the contained image in the picturebox in a file?

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    Re: Merge images on picturebox

    Toolbar.Image.Save(.FileName, ImageFormat.Png)

    Error!!!!

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    25

    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?

  24. #24
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Merge images on picturebox

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width