1
votes

I am working on the migration of a GIS program. The original code is written in VB6 and uses gdi32 lib. The new code is written in VB.Net. I have rewritten all the code excepting for the GIS-specific algorithms. I don't think it is a good idea to understand and migrate these algorithms because they have been developed and tested since 1988. So I decided to work around using PInvoke:

  Gdi32.Polygon(hdc, lpPoint, nCount)

In Gdi32 class:

<DllImport("gdi32.dll")> _
    Friend Shared Function Polygon(ByVal hdc As Integer, ByRef lpPoint As Object, ByVal nCount As  Integer) As Integer
    End Function

To get hdc, I was doing this:

hdc = MyPictureBox.CreateGraphics().GetHdc

So far so good. But then I learned that drawing directly on the PictureBox control is not a good idea because it is not persistent. Now I want to draw my polygons in a Bitmap and then set this bitmap as the Image property of PictureBox:

hdc = Graphics.FromImage(MyBitmap).GetHdc()
[...] 'Draw the polygons
MyPictureBox.Image = MyBitmap

But this doesn't seem to work. Nothing is shown on the PictureBox. Any ideas?

Thank you :)

2
There is no point to this, use the Graphics.DrawPolygon() method instead. - Hans Passant
Thanks for your answer Hans but that is exactly what I don't want to do. I need to use the gdi32 library methods. Translating Polygon(hdc, lpPoint, nCount) to graphics.DrawPolygon(Pen, Points()) forces me to understand each algorithm that they are using. - Mry
I think this does work. I have used a similar approach to draw text into a bitmap. Make sure that your coordinate system is the same - that what you think are pixels are pixels. Maybe try a big red rectangle first... - Rob

2 Answers

0
votes

In .net you can put the drawing calls into the paint event and it will appear persistent.

0
votes

Thanks everyone for your answers, finally I discussed this with the architect and we decided to translate the algorithms, despite of the potential risks. So I won't continue looking for the right answer. Thank you anyways guys :)