PhilH Posted September 5, 2003 Posted September 5, 2003 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? Quote
*Experts* mutant Posted September 5, 2003 *Experts* Posted September 5, 2003 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) Quote
PhilH Posted September 5, 2003 Author Posted September 5, 2003 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? Quote
*Experts* mutant Posted September 5, 2003 *Experts* Posted September 5, 2003 Did you insert any picture into your picture box? If you dont then this error will occur. Quote
PhilH Posted September 6, 2003 Author Posted September 6, 2003 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? Quote
*Experts* mutant Posted September 6, 2003 *Experts* Posted September 6, 2003 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)? Quote
PhilH Posted September 6, 2003 Author Posted September 6, 2003 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 ? Quote
PhilH Posted September 6, 2003 Author Posted September 6, 2003 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 ? Quote
*Experts* mutant Posted September 6, 2003 *Experts* Posted September 6, 2003 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. Quote
PhilH Posted September 9, 2003 Author Posted September 9, 2003 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. Quote
aewarnick Posted September 19, 2003 Posted September 19, 2003 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. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.