Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

You could create a Bitmap object from the PictureBox's Image object and then use the GetPixel method of the Bitmap object.

Dim b as Bitmap
b = Picturebox1.Image
dim c as Color = b.getpixel(100,100)

Posted

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 doesn't exist?

Posted

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 doesn't exist as a file on disc. Is there any way to assign the bitmap displayed to the PictureBox.Image property?

  • *Experts*
Posted
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)?
Posted

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 ?

Posted

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:

 

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 ?

  • *Experts*
Posted

What you are doing is simply drawing over the picturebox, not to any bitmap object. If you want to draw onto the PictureBox's Image then you have to create graphics object for the image:

Dim gr As Graphics = Graphics.FromImage(PictureBox.Image)
'then draw using that graphics object

Now everything will be drawn onto the actual image.

Posted

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.

  • 2 weeks later...
Posted

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.

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...