Creating a Bitmap

PhilH

Member
Joined
Aug 22, 2003
Messages
18
I am drawing a tile map onto a pictureBox. In the paint event of the PictureBox I declare a bitmap:

Dim screendisplay as New Bitmap (C:\sourcebitmap.bmp)

Where sourcebitmap is the file holding my tiles.

I then draw the tile map by using e.DrawImage , a loop and an array to assign different regions (ie the tiles) of sourcebitmap to the picturebox surface.

When the user clicks on the display I want to retrieve the colour of the pixel clicked. To do this I need (I think) a bitmap object that represents the screen display. How can this be done?
 
You could create a Bitmap object from the PictureBoxs Image object and then use the GetPixel method of the Bitmap object.
Code:
Dim b as Bitmap
b = Picturebox1.Image
dim c as Color = b.getpixel(100,100)
 
I tried that but received the error message "Object reference not set to an instance of an object".

I presume this means that PictureBox1.Image doesnt exist?
 
You are right. When I assign a bitmap to Picturebox.image explicitly.I can acess the pixel color.

However the bitmap displayed in the pictureBox is produced by using e.Drawimage to tile regions of another bitmap, it doesnt exist as a file on disc. Is there any way to assign the bitmap displayed to the PictureBox.Image property?
 
Yes, you can assign any bitmap to the Image property. When you are using the DrawImage, is onto your picturebox (for example in Paint event) or are you drawing to an image/bitmap object (by creating Graphics object for it)?
 
I am drawing in the paint event of the pictureBox.
I have an array of "Tile" objects called TileArray, then:

Dim I As Integer = 0
Dim J As Integer = 0

Dim b as New Bitmap (C:\sourcebitmap.bmp)

For I = 0 To TileArray.GetUpperBound(0)
For J = 0 To TileArray.GetUpperBound(1)

e.Graphics.DrawImage(b, TileArray(I, J).XPosition,_ TileArray(I, J).YPosition, TileArray(I, J).SourceRectangle,_ GraphicsUnit.Pixel)
Next J
Next I

This displays ok, but how do I assign the display to PictureBox.image ?
 
Sorry my code came garbled. Let me try again..

I am drawing in the paint event of the pictureBox.
I have an array of "Tile" objects called TileArray, then:

[VB]Dim I As Integer = 0
Dim J As Integer = 0

Dim b as New Bitmap (C:\sourcebitmap.bmp)

For I = 0 To TileArray.GetUpperBound(0)
For J = 0 To TileArray.GetUpperBound(1)

e.Graphics.DrawImage(b, TileArray(I, J).XPosition, TileArray(I, J).YPosition, TileArray(I, J).SourceRectangle, GraphicsUnit.Pixel)

Next J
Next I[/VB]

This displays ok, but how do I assign the display to PictureBox.image ?
 
What you are doing is simply drawing over the picturebox, not to any bitmap object. If you want to draw onto the PictureBoxs Image then you have to create graphics object for the image:
Code:
Dim gr As Graphics = Graphics.FromImage(PictureBox.Image)
then draw using that graphics object
Now everything will be drawn onto the actual image.
 
I assigned a blank bitmap object of the correct size to picturebox.image .

I then created a graphics object as you suggested. However the but the drawing Is still done onto the the graphics object , not onto the bitmap in picturebox.image.
 
try invalidating the picturebox
this.pictureB1.Invalidate();

If that does not work, create a new Bitmap, get the graphics object from that and then assign that Bitmap to the picturebox.
 
Back
Top