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
Re: Using a bitmap type in picturebox
vb Code:
Dim fs As IO.FileStream = New IO.FileStream("image file's path", IO.FileMode.Open)
Dim image1 As Image = Image.FromStream(fs)
fs.Close()
Dim bm As Bitmap
bm = image1
picturebox1.image = bm
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?
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.
Re: Using a bitmap type in picturebox
It throws an error on runtime, not while compiling. It says:
Quote:
Argument unhandled
Parameter is invalid
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.
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.
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.
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 :D