none
Modifying picturebox image RRS feed

  • Question

  • Hi Everyone:

    I am using vb.net 2019 and I need to manipulate a picturebox image.  Unfortunately, I am extremely new to bitmaps and graphics.  I tried to read on it, but I still could not get it.  Also, in other posts (for other issues), several persons (Tommytwotrain) had helped me, but still I cannot figure out my problem.So, here is my question/problem.

    I have two forms.

    On form1, I have a picturebox and a command button.  The image of the picturebox is set to the attached image (335 by 288 pixels).  When the user clicks on the command button, it will open form2.

    On form2, I have two pictureboxes and two command buttons.  Initially the two pictureboxes have the same image as for the picturebox in form1.  One command button is to draw a line, and the second command button is to reset.

    For some reason the code to reset does not work, and I have tried all sort of things.

    I appreciate all your help and any code.

    Bob

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            bmp = PictureBox1.Image
            Form2.ShowDialog()
            MsgBox("I am done")
        End Sub
    End Class
    
    
    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox2.Image = bmp
            PictureBox1.Image = bmp
    
        End Sub
    
        Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
            Using g As Graphics = Graphics.FromImage(bmp), p As New Pen(Color.Red, 8)
                g.DrawImage(PictureBox2.Image, 0, 0)
                g.DrawLine(p, 0, 50, 300, 50)
                PictureBox2.Image = bmp
            End Using
        End Sub
    
        Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
            PictureBox2.Image = PictureBox1.Image
        End Sub
    End Class
    
    Module Module1
        Public bmp As New Bitmap(335, 288)
    End Module

    Tuesday, July 9, 2019 3:44 AM

Answers

  • Hi,

    fix code:

    Public Class Form2

    Dim bitmap As Bitmap Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    bitmap = New Bitmap(PictureBox2.Width, PictureBox2.Height) PictureBox2.Image = bmp PictureBox1.Image = bmp End Sub Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click Using g As Graphics = Graphics.FromImage(bitmap), p As New Pen(Color.Red, 8) g.DrawImage(PictureBox2.Image, 0, 0) g.DrawLine(p, 0, 50, 300, 50) PictureBox2.Image = bmp End Using End Sub Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click PictureBox2.Image = PictureBox1.Image End Sub End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, July 9, 2019 7:14 AM
    Moderator

All replies

  • Hi,

    fix code:

    Public Class Form2

    Dim bitmap As Bitmap Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    bitmap = New Bitmap(PictureBox2.Width, PictureBox2.Height) PictureBox2.Image = bmp PictureBox1.Image = bmp End Sub Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click Using g As Graphics = Graphics.FromImage(bitmap), p As New Pen(Color.Red, 8) g.DrawImage(PictureBox2.Image, 0, 0) g.DrawLine(p, 0, 50, 300, 50) PictureBox2.Image = bmp End Using End Sub Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click PictureBox2.Image = PictureBox1.Image End Sub End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, July 9, 2019 7:14 AM
    Moderator
  • Bob,

    First in case it is not clear, there is an .image and a .backgroundimage and then a .sizemode and a .backgroundimagelayout. I set the image sizemode to zoom so I see the image fit to the small pic boxes in my example.

    I changed it slightly to reload the original part image at form2 load and keep it local to form 2 as I dont see any reason to do it otherwise at this time.

    I am not sure why your example reset button is not working. I suspect it is because you draw on the bmp and that changes every reference to the bmp all the way back to picture1, however I am not sure. And I dont want to make the two form example to find out. It is not worth explaining why it does not work at this time. I will just say vb is a byref lang. That means if you say this = that and then you change this it also changes that, sometimes.

    So I am going to set this new example up again slightly different so we are not mixing things and getting ref probs.

    Comprende? It does not matter at this time.

    In this next example we draw one thing with each button click as you can see.

    Furthermore, there is one drawBmp in memory we draw on and one Part1Bmp which is  a library original we dont change.

    Now in the example click draw image, draw line, reset. Works.

    NOw click draw line, draw image... where did the line go?

    You did not answer my last question... do you want to save the images? Do you want to edit them later?

    I am still not sure drawing on the bmp is the best way for you but its ok for now while learning and designing. Be aware you may need to change it later to the vector based paint event drawing method if you want to draw much or edit it.

    If you explain your overall purpose of the application we might have more opinions on that.

    To make this example you need to add the controls to your form and then paste the code to the empty form. Change the form name as reqd.

    Public Class Form6
        Public Part1Bmp As New Bitmap("c:\bitmaps\part1.png")
        Public DrawBmp As Bitmap
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            PictureBox1.Image = Part1Bmp
    
            PictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            PictureBox2.BackColor = Color.Black
            'dont link drawbmp byref to the original part image make new bmp
            ResetDrawBmp()
            PictureBox2.Image = DrawBmp
    
        End Sub
    
        Private Sub ResetDrawBmp()
            DrawBmp = New Bitmap(Part1Bmp.Width, Part1Bmp.Height)
        End Sub
    
        Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
    
            ResetDrawBmp()
            PictureBox2.Image = DrawBmp
    
        End Sub
    
        Private Sub BtnDraw_Click_1(sender As Object, e As EventArgs) Handles BtnDrawLIne.Click
    
            'make a surface to draw on drawbmp
            Using g As Graphics = Graphics.FromImage(DrawBmp), p As New Pen(Color.Red, 8)
    
                'draw line on drawbmp
                g.DrawLine(p, 0, 50, 300, 50)
    
            End Using
    
            'since drawbmp was updated need to update the picturebox ref to drawbmp
            PictureBox2.Refresh()
    
        End Sub
    
        Private Sub BtnDrawImage_Click(sender As Object, e As EventArgs) Handles BtnDrawImage.Click
    
            'make a surface to draw on drawbmp
            Using g As Graphics = Graphics.FromImage(DrawBmp), p As New Pen(Color.Red, 8)
    
                'draw image on drawbmp
                g.DrawImage(Part1Bmp, 0, 0)
    
            End Using
    
            'since drawbmp was updated need to update the picturebox ref to drawbmp
            PictureBox2.Refresh()
    
        End Sub
    End Class
    
    

    Tuesday, July 9, 2019 10:07 AM
  • Hi Alex:

    Thanks for the code.  There is a small error on it.  The way it is, it does not work.  However, in the BtnDraw_Click event, if you change the line "PictureBox2.Image = bmp" to "

    PictureBox2.Image = bitmap

    it will work.  I could not edit your code, but you may want to do it.  thanks for your help.

    Bob

    Tuesday, July 9, 2019 11:25 PM
  • Thanks TommyTwoTrain.  As I said, I am not familiar with the graphics stuff of VB.NET.  What you say, it does make sense.  The way I had things, I think bmp and Picturebox2 did share the same memory locations.  Also, a lot of other strange things were occurring in my code as well.  Thanks for all your help.

    Bob

    Tuesday, July 9, 2019 11:30 PM
  • Sorry ToomyTwoTrain, I did not read your explanation in detail, and that is why I did not answer your question.  Basically, the exact thing that I want to do with my program is:

    1- on the original picture in form1, the user clicks some points (A, B, C, etc) and the program opens form2.  On form2, there is a picturebox that contains a duplicate copy of the image from picturebox from form1.

    2- then on form2, the user selects to draw some lines/arrows on these points (based on various factors that they input in textboxes such as angle, length, etc), and clicks a Preview button.  At this point, the picturebox should draw and show the original image as well as the new line/arrow.

    3- If they like what they see, they click the OK button, and the new image is transferred to the image of the picturebox in form1.  And if they do not like it and want to try again they can click the reset button to store the original image before they draw on it, and try again.

    4- Finally, once everything is done, I would like to save the new image as well as the user's settings in my own file, and possibly the user to open the file at some point and edit the image or their settings (similar to a CAD package as you had suggested in another post).

    I hope this all makes sense.

    Bob

    Tuesday, July 9, 2019 11:45 PM