http://www

advertisement
http://www.vbforums.com/showthread.php?t=529373
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'comment out different for/next loops to see different
drawing methods
Dim i As Integer = 0
Dim y As Integer = 0
Me.BackColor() = Color.Green
'iteration to draw horizontal lines
For i = 0 To Me.Height Step 10
e.Graphics.DrawLine(Pens.Red, 0, i, Me.Width, i)
Next i
'iteration to draw vertical lines
For i = 1 To Me.Width Step 10
e.Graphics.DrawLine(Pens.Red, i, 0, i, Me.Height)
Next i
'iteration to draw ellipses
For y = 0 To Me.Height Step 10
For i = 1 To Me.Width Step 10
e.Graphics.DrawEllipse(Pens.Blue, i, y, 10, 10)
Next i
Next y
End Sub
End Class
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/15f6af6c-cb3142fd-8fe6-eaafbe1af981/
With AutoRedraw = True, you drew to an image and with AutoRedraw = False you only drew
to the screen. In .Net the AutoRedraw property is elimated and you explicity do what it
implied.
Code Snippet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
'With AutoRedraw = False
Dim G1 As Graphics = PictureBox1.CreateGraphics
G1.DrawRectangle(Pens.Red, New Rectangle(10, 10, 40, 90))
G1.Dispose()
'With AutoRedraw = True
PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim G2 As Graphics = Graphics.FromImage(PictureBox1.Image)
G2.DrawRectangle(Pens.Red, New Rectangle(10, 10, 40, 90))
G2.Dispose()
End Sub
You can also draw on the Bitmap that is stored in the Picturebox's Image property.
This will persist. You need to call Refresh to tell windows to repaint the picturebox
after drawing something. (Edit: which was just shown. I had my window open for
ages...)
Code Snippet
Public Class Form1
Private pb As New PictureBox
Private g As Graphics
Private WithEvents t As New Timer
Private rand As New Random
Sub New()
InitializeComponent()
Me.ClientSize = New Size(300, 300)
Me.Controls.Add(pb)
pb.Dock = DockStyle.Fill
pb.Image = New Bitmap(300, 300)
' Create a graphics object that
' will draw on the bitmap assigned to
' the picturebox's image property.
' This will persist.
g = Graphics.FromImage(pb.Image)
g.Clear(Color.Red)
t.Interval = 100
t.Start()
End Sub
' get a random solid color.
Private Function RandomColor() As Color
Return Color.FromArgb(rand.Next(Integer.MinValue, Integer.MaxValue) Or
&HFF000000)
End Function
' get a random square.
Private Function RandomSquare() As Rectangle
Dim width As Integer = rand.Next(1, 301)
Dim x As Integer = rand.Next(-width, 300)
Dim y As Integer = rand.Next(-width, 300)
Return New Rectangle(x, y, width, width)
End Function
' Draw on the bitmap assigned to the picturebox.
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles
t.Tick
' Pen and SolidBrush have Dispose methods which
' frees up the memory when you have finished using
' the object.
' The Using block will call Dispose automatically
' when execution hits the End Using statement.
Using b As New SolidBrush(RandomColor)
Using p As New Pen(RandomColor)
' Rectangle does have a dispose method.
Dim rec As Rectangle = RandomSquare()
g.FillEllipse(b, rec)
g.DrawEllipse(p, rec)
' We won't see the drawing until we call
' pb.Refresh() ' Tell windows to rpeaint the whole picturebox.
' Or, more economically, call invalidate and then refresh.
pb.Invalidate(New Rectangle(rec.X - 1, rec.Y - 1, rec.Width + 2, rec.Height + 2)) '
Tell windows to repaint just the rectangle.
pb.Update()
End Using
End Using
End Sub
End Class
If PictureBox1.Image Is Nothing Then
PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
End If
Using G2 As Graphics = Graphics.FromImage(PictureBox1.Image)
G2.DrawRectangle(Pens.Black, New Rectangle(0, 0, e.X, e.Y))
http://vbnet.mvps.org/index.html?code/bitmap/setpixelv.htm
Option Explicit
Private Declare Function SetPixelV Lib "gdi32" _
(ByVal hDC As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal crColor As Long) As Byte
Private Declare Function GetPixel Lib "gdi32" _
(ByVal hDC As Long, _
ByVal x As Long, _
ByVal y As Long) As Long
Private Sub Command1_Click()
'variables for brightness, colour calculation, positioning
Dim Brightness As Single
Dim NewColour As Long
Dim pixHdc As Long
Dim x As Long, y As Long
Dim r As Integer, g As Integer, b As Integer
'change the brightness to a percent
Brightness = CSng(Val(Text1.Text) / 100)
pixHdc = Picture1.hDC
'run a loop through the picture to change every pixel
For x = 0 To Picture1.ScaleWidth
For y = 0 To Picture1.ScaleHeight
'get the current colour value
NewColour = GetPixel(pixHdc , x, y)
'extract the R,G,B values from the long returned by GetPixel
r = (NewColour Mod 256)
b = (Int(NewColour \ 65536))
g = ((NewColour - (b * 65536) - r) \ 256)
'change the RGB settings to their appropriate brightness
r = r * Brightness
b = b * Brightness
g = g * Brightness
'make
If r
If r
If b
If b
If g
If g
sure the new
> 255 Then r
< 0 Then r =
> 255 Then b
< 0 Then b =
> 255 Then g
< 0 Then g =
variables aren't too high or too low
= 255
0
= 255
0
= 255
0
'set the new pixel
SetPixelV pixHdc, x, y, RGB(r, g, b)
'continue through the loop
Next y
'refresh the picture box
'(a nice visual progress effect)
Picture1.Refresh
Next x
'final picture refresh
Picture1.Refresh
End
Download