Results 1 to 9 of 9

Thread: Using a bitmap type in picturebox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Using a bitmap type in picturebox

    Hi all

    I've got Dim bmp As New Bitmap(10, 10), and I put some more data in the bitmap, but now I want to display it inside a picturebox but it doesn't work because it's not an image type. How can I display a Bitmap inside a picturebox?

    Thanks in advance

  2. #2
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Using a bitmap type in picturebox

    vb Code:
    1. Dim fs As IO.FileStream = New IO.FileStream("image file's path", IO.FileMode.Open)
    2.         Dim image1 As Image = Image.FromStream(fs)
    3.         fs.Close()
    4.         Dim bm As Bitmap
    5.         bm = image1
    6.         picturebox1.image = bm

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Re: Using a bitmap type in picturebox

    The image doesn't come from a file, that's the problem. It's from a screenshot with this code, wich should return the area 5 pixels to each side of the mouse cursor:

    Code:
    Using bmp As New Bitmap(10, 10)
                Using g As Graphics = Graphics.FromImage(bmp)
                    g.CopyFromScreen(New Point(Windows.Forms.Cursor.Position.X - 5, Windows.Forms.Cursor.Position.Y - 5), New Point(0, 0), New Size(10, 10))
                End Using
    End Using
    When I use bmp for picturebox.image, it returns an error so I figured something is wrong with the type. Is it possible it's another kind of error though?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Using a bitmap type in picturebox

    What does the error say? Bitmaps are certainly images, and there shouldn't be any issue displaying them in pictureboxes.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Re: Using a bitmap type in picturebox

    It throws an error on runtime, not while compiling. It says:
    Argument unhandled
    Parameter is invalid

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Using a bitmap type in picturebox

    I wouldn't expect a compiler error, so a runtime error is what I would expect. Which line is throwing the error? It's not an error message that I have seen before, and I don't see anything that appears wrong, except that the source for the copyFromScreen could do some odd things if you clicked in such a place that the source point had a negative number. That seems unlikely, though.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Re: Using a bitmap type in picturebox

    This is my entire code for the event, the error happens on the pctImg.image = i part.

    Code:
    Private Sub frmGetColor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            Dim c As Color
            Dim i As Image
            Using bmp As New Bitmap(10, 10)
                Using g As Graphics = Graphics.FromImage(bmp)
                    g.CopyFromScreen(New Point(Windows.Forms.Cursor.Position.X - 5, Windows.Forms.Cursor.Position.Y - 5), New Point(0, 0), New Size(10, 10))
                End Using
                c = bmp.GetPixel(5, 5)
                i = bmp
            End Using
            pctImg.Image = i
            pnlColor.BackColor = c
        End Sub
    EDIT: the point of the c part is to get the color of the actual pixel the mouse is on, it's going to be kind of a color picker.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Using a bitmap type in picturebox

    Yeah, there's a problem there. You have that Using block for bmp. i holds is declared as type image, which may make you think that it holds an image, but it doesn't. Instead, it holds the address of an image object, just like bmp does. When this line is executed:

    i = bmp

    that doesn't copy the bitmap pointed to by bmp into a new image pointed to by i. All that happens is that i is set to point to the same object that bmp is pointing to. An object which you then destroy when execution hits the End Using line. By the time pctImg.Image = i is executed, the object that i is pointing to has been disposed. That's probably causing you the trouble.

    So what's the solution? Get rid of i, and get rid of the Using block for bmp. After all, you don't want to destroy the image, you want to show it in the picturebox, so there is no reason to have the Using block there. Just set pctImg.Image = bmp, rather than i.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Re: Using a bitmap type in picturebox

    Wow, apparently I still am a beginner with VB... Thanks for your help, it works like a charm now

Tags for this Thread

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