Question Paint program Problem (graphics dissapearing)

12padams

Well-known member
Joined
Feb 19, 2010
Messages
48
Programming Experience
Beginner
Ok major problem with a paint program i am making. When i have a window overlap or anything and move to side of screen and minimize and maximize i lose whats in the picturebox...

Here is an example of the code I use to draw a line after click a button:

VB.NET:
    Private Sub btndraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlinedraw.Click
        lxs = txtslinelocationx.Text
        lxe = txtelinelocationx.Text
        lys = txtslinelocationy.Text
        lye = txtelinelocationy.Text
        PictureBox1.CreateGraphics.DrawLine(drawingpen, lxs, lys, lxe, lye)
    End Sub

Here is a picture of my program running:
drawingprogramexample.png


How do I stop everything being deleted when things pass it...?

Also not very important right now but how can i add a save feature to this program?
 
- You can create a structure for each type of drawing operation
- Create an array of each structure
- Store data in each array of structure
- on the resize event you can control the formwindowstate
- on the layout event rebuild the picture with the arrays
.....
- you can save the arrays in a file
- can rebuild the picture from the file
 
You need to do all graphics drawing in the Paint event. Example:

VB.NET:
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Drawing.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.DrawLine(xStart, yStart, xEnd, yEnd)
End Sub
 
Ok with this paint event you are talking about...

How can I make this work for if you press a button?

I have about 5 draw buttons which work in different ways with different lines of code and I put the draw event into the button click code.

How can I active this picturebox1 paint event if I click a button?

Also i remeber the picturebox paint event being actvated when the picturebox appears on the screen.
 
Example about what I said in last post:

Module Module1
Structure structLine
Public drawingpen As Pen
Public lxs As Single
Public lys As Single
Public lxe As Single
Public lye As Single
End Structure

End Module

Public Class Form1
Dim LineArray(0) As structLine
Dim bMinimized As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'To replace -- just for simulation!
Dim i As Integer = UBound(LineArray)
i += 1
ReDim Preserve LineArray(i)

LineArray(i).drawingpen = New Pen(Color.Black)
LineArray(i).lxs = 10
LineArray(i).lxe = 50
LineArray(i).lys = 50
LineArray(i).lye = 150

pic.CreateGraphics.DrawLine(LineArray(i).drawingpen, LineArray(i).lxs, LineArray(i).lys, LineArray(i).lxe, LineArray(i).lye)
End Sub
Private Sub RedoPicture()
Me.Invalidate()
Dim i As Integer
If UBound(LineArray) > 0 Then
For i = 1 To UBound(LineArray)
pic.CreateGraphics.DrawLine(LineArray(i).drawingpen, LineArray(i).lxs, LineArray(i).lys, LineArray(i).lxe, LineArray(i).lye)
Next
End If
End Sub

Private Sub Form1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
If bMinimized Then
pic.Refresh()
RedoPicture()
bMinimized = False
End If
End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
bMinimized = True
End If
End Sub
End Class
 
I use this method to draw stuff and it functions to me very well
First of all i set DoubleBuffered property of the form where i draw to true, than:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawLine(0,0,100,100) 'example draw
MyBase.OnPaint(e)
End Sub
I generally make a timer, and on Timer.Tick i use this code:
Me.Refresh()
 
Back
Top