Results 1 to 27 of 27

Thread: draw a transparented image to form background (vb.net)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    draw a transparented image to form background (vb.net)

    Hi
    I have a project that I have written about 90% of it.
    This project is an image guessing project.
    There is a black rectangle in which a circle is moving.
    In this circle, a part of an image appears. And every time it moves, another part and so on.
    It occurred to me to simplify this game by transparenting the black pixels of the image that correspond to the rectangle and recording the circle somewhere. Each time the circle is moved, I record another transparent image and save all these images on top of each other, and finally, a complete image is drawn, which is the sum of the images of the circles that the desired image is obtained.
    The steps I did
    1- I transferred the game image to Visual studio via webcam with emgu.cv (the code is inside a timer)
    Code:
    Imports Emgu.CV
    Imports Emgu.CV.UI
    Imports Emgu.CV.Structure
    
    Public Class Form1
    Dim takepic As New Capture
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    PictureBox1.Image = takepic.QueryFrame.ToBitmap
    End Sub
    End Class
    2- I made the received image transparent.
    Code:
            
    PictureBox1.Image = takepic.QueryFrame.ToBitmap
    Dim bm_label1 As New Bitmap(PictureBox1.Image)
    bm_label1.MakeTransparent(Color.FromArgb(24, 53, 73))
    3- and printed it in the form through a code I found in Google.
    Code:
    Dim gameGraphics As System.Drawing.Graphics = Me.CreateGraphics
    gameGraphics.DrawImage(bm_lable1, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
    I wanted to use the picture box but I did not use it because the transparency is not printed.

    NOW

    the problem now is that the final image is not fully printed on the form (Left image (inside photo)). In the correct position, the image on the left and the image on the right should be the same size

    Name:  Screenshot 2021-09-12 154940.jpg
Views: 165
Size:  19.1 KB

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,254

    Re: draw a transparented image to form background (vb.net)

    First… Don’t use Me.CreateGraphics
    Use the Form’s Paint event…

    Code:
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        ‘e.Graphics is the graphics object
    End Sub
    This will give you persistent graphics. If you want to build up an image step by step, you need to use variables of some sort that define the stages of the images you want to draw, then in the Paint event, use those variables to create your drawn image.
    Last edited by .paul.; Sep 12th, 2021 at 11:29 PM.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    You might like to follow the CodeBank link in my signature and check out my Simple Drawing thread. It demonstrates what .paul. suggested by storing Line objects in a generic List assigned to a field and then drawing them in the form's Paint event handler.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by .paul. View Post
    First… Don’t use Me.CreateGraphics
    Use the Form’s Paint event…

    Code:
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        ‘e.Graphics is the graphics object
    End Sub
    This will give you persistent graphics. If you want to build up an image step by step, you need to use variables of some sort that define the stages of the images you want to draw, then in the Paint event, use those variables to create your drawn image.
    thank you for the answer
    I read about the paint before in this forums but I don't know how can i do that!
    this game page (black rectangle with a circle in it) just running for 5 second, so 5*60fps = 300, i have finally 300 frame
    how can I do store this 300 frames to a list of images or something like that and paint it in the form or paint it directly on top of each other?
    Last edited by vardipoor.m; Sep 13th, 2021 at 04:24 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Code:
    Imports Emgu.CV                        'emgu.cv for show webcam
    Imports Emgu.CV.UI                     'emgu.cv for show webcam
    Imports Emgu.CV.Structure              'emgu.cv for show webcam
    
    Public Class Form1
        Dim takepic As New Capture
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PictureBox1.Image = takepic.QueryFrame.ToBitmap                                'add frame from my webcam to picturebox
            Dim bm_label1 As New Bitmap(PictureBox1.Image)
    
            bm_label1.MakeTransparent(Color.FromArgb(24, 53, 73))                           'transparent the background of rectangle 
            PictureBox2.Image = bm_label1
    		
        End Sub
    
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Start()
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Timer1.Stop()
    		
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            Application.Exit()
    		
        End Sub
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
        End Sub
    End Class
    this is all my code
    can you add your lines to it?
    Last edited by vardipoor.m; Sep 13th, 2021 at 04:11 AM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    Instead of expecting us to write your code for you, how about you check out the example I pointed you to, learn how to do it yourself and then write your own code? If there's something you don't understand or you encounter an issue along the way then we can help with that but just not even trying and expecting us to do your work for you is a bit much.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by vardipoor.m View Post
    I read about the paint before in this forums but I don't know how can i do that!
    If only someone had directed you to an example that you could learn from. Oh look, they did.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by jmcilhinney View Post
    Instead of expecting us to write your code for you, how about you check out the example I pointed you to, learn how to do it yourself and then write your own code? If there's something you don't understand or you encounter an issue along the way then we can help with that but just not even trying and expecting us to do your work for you is a bit much.
    You seem to be upset that I told you to write this
    I'm sorry
    I tried before but in the paint event but could not complete
    Because I have 300 photos that need to be printed on top of each other and the form prints only one photo and I have to refresh the form every time

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by jmcilhinney View Post
    If only someone had directed you to an example that you could learn from. Oh look, they did.
    no i saw the codes in the google

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    I'm not upset that you told us to write it. I'm annoyed that you think it's OK to put in no effort of your own and ask strangers on the internet to do your work for you. I created an example specifically to help people like you and I directed you to it, yet it is apparently too much trouble for you to follow the link and learn something for yourself. That's your prerogative, but why should we make an effort that you're not prepared to make for yourself. If you can't do something then that is one thing, but if you can't be bothered to then that is something else entirely. Did you take a look at my CodeBank example? If not, why not? If you did look at it, is there something about it that you don't understand? If so, why haven't you asked about that specifically? If you have looked at it and you did understand it, have you tried to implement the same principles in your own scenario? If not, why not? If so, what happened? If you tried and it didn't work then why haven't you shown us what you did and told us what happened? Don't just sit on your hands and wait for someone else to do your work for you. Use the information already provided, do what you can for yourself and then ask specific questions if and when you need to. We'll still be here to help if you encounter an issue but you have to actually do something for that to happen.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Code:
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            e.Graphics.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
    
        End Sub
    i try this but i get error
    An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication3.exe

    Additional information: Object reference not set to an instance of an object.


    The form_paint event is apparently executed before the timer starts and before the image is saved in the picture box.
    but if i load a picture to picturebox1, run with no problem!
    Code:
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Pain
            PictureBox1.Load("e:\1.png")
            e.Graphics.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
    
        End Sub
    Last edited by vardipoor.m; Sep 13th, 2021 at 04:56 AM.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by vardipoor.m View Post
    The form_paint event is apparently executed before the timer starts and before the image is saved in the picture box.
    Of course it is. The Paint event is raised every time any part of the form needs to be painted/repainted on the screen, so it will be raised when the form is first displayed, as well as any other time the form has or may have changed.

    Obviously you only want to draw that image IF there is an image to draw. I wonder whether VB has a code construct that will only execute code IF a condition is true.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    check out my Simple Drawing thread
    i cant find this in your code bank can you give me a link?

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    ok this worked
    Code:
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    
            'PictureBox1.Load("e:\1.png")
            If Not PictureBox1.Image Is Nothing Then
                e.Graphics.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
    
            Else
    
            End If
    but i dont know how can i tell to the form_paint to draw a image to the form every time my timer runs and picturebox1 update.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Is it possible for 300 images that are transparent to be stored on top of each other in a picture box, so that the transparent parts of each image do not appear?
    I mean there is no other way but to use form_paint?

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by vardipoor.m View Post
    i cant find this in your code bank can you give me a link?
    How can you hope to write software if you can't even use a web browser? Click the VB CodeBank link in my signature and search for the keywords I gave you on the page. It's not rocket surgery. We shouldn't have to explain this stuff.

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: draw a transparented image to form background (vb.net)

    Quote Originally Posted by vardipoor.m View Post
    i dont know how can i tell to the form_paint to draw a image to the form every time my timer runs and picturebox1 update.
    There is no Timer, which you would know if you could click a link and had read my example. I'm done here.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    ah
    before i post "i cant find it" i searched it but cant find
    are you mean this
    https://www.vbforums.com/showthread....Simple+Drawing
    Last edited by vardipoor.m; Sep 13th, 2021 at 06:50 AM.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    i have read that tread and have questions
    this program draw multiple lines permanently but i have multiple images! what do i do?
    and its drawing with mouse down and up events, i want to draw with timer because my picture must be placed in timer according to emgu.cv
    Last edited by vardipoor.m; Sep 13th, 2021 at 07:23 AM.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Code:
    Imports Emgu.CV
    Imports Emgu.CV.UI
    Imports Emgu.CV.Structure
    
    Public Class Form1
        Dim takepic As New Capture
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim bm_label1 As New Bitmap(640, 360)
            bm_label1 = takepic.QueryFrame.ToBitmap
            bm_label1.MakeTransparent(Color.FromArgb(31, 58, 79))
            PictureBox2.Image = bm_label1                              'picturebox2 have the transparented image
        End Sub
    
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Me.Drawimage(e.Graphics)
        End Sub
    	 'The lines that have been drawn but not saved.
        Private images As New List(Of Image)
    
        'The start point of the line currently being drawn.
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Place a blank image in the PictureBox control.
            Me.PictureBox2.Image = New Bitmap(Me.PictureBox2.Width, Me.PictureBox2.Height)
    
        End Sub
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            'Draw each line on the control.
            Me.Drawimage(e.Graphics)
        End Sub
    
        Private Sub Save()
            'Create a Graphics object from the Image in the PictureBox.
            Using g As Graphics = Graphics.FromImage(Me.PictureBox2.Image)
                'Draw each line on the image to make them permanent.
                Me.Drawimage(g)
    
            End Using
    
            'Clear the temporary lines that were just saved.
            Me.Clear()
        End Sub
    
        Private Sub Clear()
            'Clear all unsaved lines.
            Me.images.Clear()
    
            'Force the control to repaint so the lines are removed.
            Me.PictureBox1.Refresh()
        End Sub
    
        Private Sub Drawimage(ByVal g As Graphics)
            For Each image As Image In Me.images
                g.DrawImage(PictureBox1.Image, 0, 0)
            Next image
        End Sub
    
    End Class
    oh its very hard for me
    i edited your codes but its not worked
    where is wrong?
    Last edited by vardipoor.m; Sep 13th, 2021 at 09:10 AM.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    dear jmcilhinney

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    i think i must edit this lines
    Code:
        Private Sub Drawimage(ByVal g As Graphics)
            For Each image As Image In Me.images
                g.DrawImage(PictureBox1.Image, 0, 0)
            Next image
        End Sub
    and this lines save picture in picturebox but i dont know how can i save a picture from form!
    Code:
        Private Sub Save()
            'Create a Graphics object from the Image in the PictureBox.
            Using g As Graphics = Graphics.FromImage(Me.PictureBox2.Image)
                'Draw each line on the image to make them permanent.
                Me.Drawimage(g)
    
            End Using
    
            'Clear the temporary lines that were just saved.
            Me.Clear()
        End Sub
    Last edited by vardipoor.m; Sep 13th, 2021 at 09:40 AM.

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,254

    Re: draw a transparented image to form background (vb.net)

    Put all of your images in a (correctly ordered) Form level List(of BitMap)…

    Code:
    Dim images as New List(of BitMap)
    Images.add(imgName)
    Use a Form level maximum variable…

    Code:
    Dim maxImage as integer = 0
    Then in your timer_Tick event handler, you can specify how many images to draw.
    In your Form_Paint event handler, you just draw the specified number of images.

    Code:
    Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        If maxImage > 0 Then
            For x as Integer = 1 to maxImage
                e.Graphics.DrawImage(images(x - 1), New Point(theLocationX, theLocationY))
            Next
        End If
    End Sub
    Using this method, there’s no way to save what you’ve drawn, but if you use this method…

    Code:
    Dim img as Bitmap
    Dim gr as Graphics
    In your Timer_Tick event…

    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            ‘ I commented these as they’re outside the scope of this example
            ‘Dim bm_label1 As New Bitmap(640, 360)
            ‘bm_label1 = takepic.QueryFrame.ToBitmap
            ‘bm_label1.MakeTransparent(Color.FromArgb(31, 58, 79))
            ‘PictureBox2.Image = bm_label1
            'picturebox2 have the transparented image
        img = New BitMap(Me.ClientSize.Width, Me.ClientSize.Height)
        gr = Graphics.FromImage(img)
        If maxImage > 0 Then
            For x as Integer = 1 to maxImage
                gr.DrawImage(images(x - 1), New Point(theLocationX, theLocationY))
            Next
        End If
        Me.Refresh
    End Sub
    Then in your Form_Paint event handler…

    Code:
    Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        If Not img Is Nothing Then
            e.Graphics.DrawImage(img, Point.Empty)
        End If
    End Sub
    Edit: What I omitted to say, is that you can save img at any point…
    Last edited by .paul.; Sep 13th, 2021 at 02:12 PM.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    Thank you for your time

    I used your code, the whole image is printed in the form, but apparently only one image is printed.
    You mentioned that I should save my photos in a list, but I can not do that
    As I said, this is an online game and I only have 5 seconds to guess the image, so I have to transparent and save each frame in order, and finally the sum of the transparent images make up the main image!

    Code:
    Imports Emgu.CV
    Imports Emgu.CV.UI
    Imports Emgu.CV.Structure
    Public Class Form1
        Dim takepic As New Capture
        Dim bmp1 As New Bitmap(640, 360)
        Dim images As New List(Of Bitmap)
        Dim maxImage As Integer = 1
        Dim img As Bitmap
        Dim gr As Graphics
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            ' I commented these as they’re outside the scope of this example        'i cant remove this lines because they show a fream of my webcam 
            Dim bm_label1 As New Bitmap(640, 360)                                   
            bm_label1 = takepic.QueryFrame.ToBitmap                                 
            bm_label1.MakeTransparent(Color.FromArgb(31, 58, 79))
            PictureBox2.Image = bm_label1
            'picturebox2 have the transparented image
            images.Add(bm_label1)
            img = New Bitmap(640, 360)
            gr = Graphics.FromImage(img)
            If maxImage > 0 Then
                For x As Integer = 1 To maxImage
                    gr.DrawImage(images(x - 1), New Point(0, 0))
                Next
            End If
            Me.Refresh()
        End Sub
    
    
    
        Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            If Not img Is Nothing Then
                e.Graphics.DrawImage(img, 0, 0)
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Start()
        End Sub
    Last edited by vardipoor.m; Sep 13th, 2021 at 06:41 PM.

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,254

    Re: draw a transparented image to form background (vb.net)

    Ok. How often does your Timer tick?

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Sep 2021
    Posts
    16

    Re: draw a transparented image to form background (vb.net)

    if you mean is interval then interval=17 milliseconds
    It should work for about 5 seconds. My camera has 60 frames per second. So if we count 1000/60 = 17. Approximately every 17 milliseconds one frame should be taken to achieve 60 frames per second. And for 5 seconds to work, the timer is ticked approximately 5 * 60 = 300 times Or 60 times per second
    Last edited by vardipoor.m; Sep 14th, 2021 at 03:33 AM.

  27. #27
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,374

    Re: draw a transparented image to form background (vb.net)

    You simply want to merge the images as you get them into a single bitmap.

    I would probably use a Picturebox the size of the form and use its .Image property, but since you specify a form, then simply assign a bitmap to the form's BackgroundInmage, get a graphics object for that bitmap and update that bitmap, then have the form update itself from the bitmap by calling the Refresh method.
    Code:
    Imports Emgu.CV
    Imports Emgu.CV.UI
    Imports Emgu.CV.Structure
    
    Public Class Form1
      Private bmp As Bitmap
      Private g As Graphics
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bmp = New Bitmap(ClientSize.Width, ClientSize.Height)
        BackgroundImage = bmp
        g = Graphics.FromImage(bmp)
      End Sub
    
    
      Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        PictureBox1.Image = takepic.QueryFrame.ToBitmap
    
        Dim bm_label1 As New Bitmap(PictureBox1.Image)
        bm_label1.MakeTransparent(Color.FromArgb(24, 53, 73))
        g.DrawImage(bm_label1, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
        Me.Refresh()
      End Sub
    
    End Class
    Last edited by passel; Sep 14th, 2021 at 04:39 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

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