drawing in picture box

vbdog

Member
Joined
Apr 24, 2007
Messages
7
Programming Experience
Beginner
Iam calling the sub below a number of times until the picture is drawn. But when i Minimized the form the drawing clears.

can anyboddy help please.

Sub drawscreen()

Dim g As Graphics
Dim bmap As Bitmap
bmap = New Bitmap(picgame.Width, picgame.Height, _
picgame.CreateGraphics)

g = Graphics.FromImage(bmap)

Dim p As New Point()
p.X = 1 + x + (i * 4)
p.Y = y + (j * 4)


If bmap.GetPixel(p.X, p.Y) = System.Drawing.Color.FromArgb(255, 255, 255) Then
v(&HF) = 1

picgame.CreateGraphics.DrawLine(Pens.Black, 1 + x + (i * 4), y + (j * 4), 1 + x + (i * 4) + 3, y + (j * 4) + 3)

Else
picgame.CreateGraphics.DrawLine((Pens.White), 1 + x + (i * 4), y + (j * 4), 1 + x + (i * 4) + 3, y + (j * 4) + 3)

End If

g.Dispose()
end sub

I have tryed picgame.Image = bmap but it did not draw.

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
DrawSprite() ' calls drawscreen()

MyBase.OnPaint(e)

End Sub

thanks karl
 
Place the code in the Pictureboxes Paint event and use the e.Graphics instance that is provided to you there to draw with. If you need to call for painting anywhere else at specific times in app you just call the Pictureboxes .Refresh method and the Paint event handler code will be carried out.
 
Actually on second look I'm a little confused about if you want to draw to the control (explained post 2) or if you want to draw to a bitmap that is to be displayed by the control.
 
x,y,s s = Height (opcode4)

Sub DrawSprite()
v(&HF) = 0
For j = 0 To opcode4 - 1
datax = ram(Index + j)
i = 0
x = v(opcode2) * 4
y = v(opcode3) * 4
If datax >= 128 Then
drawscreen
datax = datax - 128
End If
i = i + 1
If datax >= 64 Then
drawscreen
datax = datax - 64
End If
i = i + 1
If datax >= 32 Then
drawscreen
datax = datax - 32
End If
i = i + 1
If datax >= 16 Then
drawscreen
datax = datax - 16
End If
i = i + 1
If datax >= 8 Then
drawscreen
datax = datax - 8
End If
i = i + 1
If datax >= 4 Then
drawscreen
datax = datax - 4
End If
i = i + 1
If datax >= 2 Then
drawscreen
datax = datax - 2
End If
i = i + 1
If datax >= 1 Then
drawscreen
datax = datax - 1
End If
Next j
End Sub

Sub drawscreen()

Dim g As Graphics
Dim bmap As Bitmap
bmap = New Bitmap(picgame.Width, picgame.Height, _
picgame.CreateGraphics)

g = Graphics.FromImage(bmap)

Dim p As New Point()
p.X = 1 + x + (i * 4)
p.Y = y + (j * 4)


If bmap.GetPixel(p.X, p.Y) = System.Drawing.Color.FromArgb(255, 255, 255) Then
v(&HF) = 1

picgame.CreateGraphics.DrawLine(Pens.Black, 1 + x + (i * 4), y + (j * 4), 1 + x + (i * 4) + 3, y + (j * 4) + 3)

Else
picgame.CreateGraphics.DrawLine((Pens.White), 1 + x + (i * 4), y + (j * 4), 1 + x + (i * 4) + 3, y + (j * 4) + 3)

End If

g.Dispose()
end sub

I have tryed picgame.Image = bmap but it did not draw.
 
Just change the code below but it just draws a pixel then clears the pixel then draws another then clears and so on

Dim m_imageList As ImageList
m_imageList = New ImageList
m_imageList.ImageSize = New Size(110, 42) 'Change to whatever size you are needing
Dim bmp As New Bitmap(110, 42, Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics
g = Graphics.FromImage(bmp)
bmp.CreateGraphics.DrawLine(.......stuff
g.Dispose()
m_imageList.Images.Add(bmp)
picgame.Image() = m_imageList.Images(0)​
 
Back
Top