Cannot Bitmap.SetPixel twice

chaos syndrome
01-29-2004, 01:31 PM
I'm using the following code to generate a sort of spotted texture which can then be mapped onto a sphere.

Unfortunately it doesn't seem to work: the first call to CreateCrater does the job fine but subsequent calls produce no result. On rewriting the code it seems that after using the Bitmap.SetPixel method it is impossible to subsequently use Bitmap.SetPixel on that same pixel.

Is there some method I need to call to reset the bitmap to a state where the pixels can be written again?


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Dim b As New Bitmap(800, 400, Imaging.PixelFormat.Format24bppRgb)
'For i = 1 To 10
'CreateCrater(b, RangedRandom(-180, 180), RangedRandom(-90, 90), RangedRandom(1000, 5000))
CreateCrater(b, 0, 0, 1000)
CreateCrater(b, 0, -90, 1000)
b.Save("cratered.bmp", Imaging.ImageFormat.Bmp)
Button1.Enabled = True
End Sub

Private Function SphereDistance(ByVal long1 As Single, ByVal lat1 As Single, ByVal long2 As Single, ByVal lat2 As Single)
' Maths stuff which is irrelevant to the topic. I think.
End Function

Private Sub CreateCrater(ByRef b As Bitmap, ByVal longitude As Single, ByVal latitude As Single, ByVal radius As Single)
Dim pixX, pixY As Integer
Dim myrect As RectangleF = b.GetBounds(GraphicsUnit.Pixel)
For pixX = myrect.Left To myrect.Right - 1
For pixY = myrect.Top To myrect.Bottom - 1
Dim lng As Single = pixX / myrect.Width * 360 - 180
Dim lat As Single = pixY / myrect.Height * 180 - 90
Dim dist As Single = SphereDistance(lng, lat, longitude, latitude)
Dim v As Integer = 0
If dist <= radius Then
v = 255 ' For now. Will be replaced by more advanced function later.
End If
Dim mycol As Color = Color.FromArgb(v, v, v)
b.SetPixel(pixX, pixY, mycol)
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum