Windows Develop Bookmark and Share   
 index > Windows Forms General > Layered "ImageMap" in windows forms? - Completely lost
 

Layered "ImageMap" in windows forms? - Completely lost

Hello All,

I have spent the better part of the morning searching for info on how to do this, and while I have found some helpful information, nothing was completely applicable to what I am trying to do. A good example would be an interactive image of the United States:

First Decent Google Image Result for an example


I need to have individual images of each state make up that larger image of the entire country. I need to be able to "swap" each states image out with an identical one in another color based on some information pulled from the database. Does that make sense?

The issue is that PictureBoxes do not support transparency. If I had a PictureBox for each state, they overlap each other and the top PictureBox blocks out some of the image in the PictureBox below. Furthermore, I would not be able to determine exactly which State/PictureBox was clicked on if they overlap. I found an article from Bob Powell that describes how to create transparent controls. However, there are two issues with this. The first being that it is wrote in C, and all my attempts to convert it to VB ended in failure. The second being that I think I would still have the issue of overlapping throwing off being able to determine which state image was clicked. I cant tell since I cant try it.

I also found an article over at CodeProject which is also in C. Even if I was able to convert it to VB, it would still not work for my application. My App is for touchscreens, so logging the location of the cursor before the click would not work at all.

I am desperate for any ideas, tips, tricks, or suggestions anyone would be willing to offer up!
DigitalFusion  Thursday, April 17, 2008 7:31 PM

You can have the PictureBox show a Bitmap that is in memory. As your program decides to overlay additional bitmaps, use Graphics.DrawBitmap to draw other bitmaps onto your Bitmap loaded into the PictureBox. The Graphics.DrawBitmap will observe the transparency found in images such as PNGs.

Code Snippet

Private _MemBitmap As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'This loads the initial image.

_MemBitmap = Bitmap.FromFile(fileName)

PictureBox1.Image = _MemBitmap

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'This draws another image as an overlay on top of _MemBitmap in memory.

'There are additional forms of DrawImage; there are ways to fully

'specify the source and destination rectangles. Here, we just draw the

'overlay at position (0,0).

Using g As Graphics = Graphics.FromImage(_MemBitmap)

g.DrawImage(Bitmap.FromFile(overlayFileName), 0, 0)

End Using

'Force control to redraw the bitmap to the screen.

PictureBox1.Invalidate()

End Sub

BinaryCoder  Saturday, April 19, 2008 1:37 AM
please?

DigitalFusion  Friday, April 18, 2008 3:09 PM

You can have the PictureBox show a Bitmap that is in memory. As your program decides to overlay additional bitmaps, use Graphics.DrawBitmap to draw other bitmaps onto your Bitmap loaded into the PictureBox. The Graphics.DrawBitmap will observe the transparency found in images such as PNGs.

Code Snippet

Private _MemBitmap As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'This loads the initial image.

_MemBitmap = Bitmap.FromFile(fileName)

PictureBox1.Image = _MemBitmap

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'This draws another image as an overlay on top of _MemBitmap in memory.

'There are additional forms of DrawImage; there are ways to fully

'specify the source and destination rectangles. Here, we just draw the

'overlay at position (0,0).

Using g As Graphics = Graphics.FromImage(_MemBitmap)

g.DrawImage(Bitmap.FromFile(overlayFileName), 0, 0)

End Using

'Force control to redraw the bitmap to the screen.

PictureBox1.Invalidate()

End Sub

BinaryCoder  Saturday, April 19, 2008 1:37 AM
thanks for the reply and the sample code!!!


what you are saying is that I would add a bitmap of the entire US, then draw each state on top of that, doing it 50 times for each state?
DigitalFusion  Monday, April 21, 2008 3:25 PM

> what you are saying is that I would add a bitmap of the entire US, then draw each state on top of that,

Yes, for each state that is a different color thanyourbitmap of the "entire US".

BinaryCoder  Tuesday, April 22, 2008 12:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom Form Graphics
• opaque forms
• Application hangs with a call to MessageBox.Show
• closing forms after page_load
• simple card game application
• my.computer.keyboard incorrect when form not focus
• Easiest way determine when outside a control was clicked?
• How to print the total number of pages on each page?
• generate thumbnail from a non-image document file?
• Changing Opacity of a form from another form