Making graphics stay?

bonedoc

Well-known member
Joined
May 4, 2006
Messages
112
Programming Experience
Beginner
On my form load, I am getting the image from picturebox1 and making a bitmap called canvas. All graphics are drawn to this. When I save the image I see, I actually save the canvas, and all is good. This works good until I do a rotation on the the picture. When I rotate the canvas, it rotates, but all graphics I previously did will disappear. How can i keep the graphics from leaving the image?




VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            canvas = New Bitmap(PictureBox1.Width, PictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim g As Graphics = Graphics.FromImage(canvas)
            g.DrawImage(PictureBox1.Image, New Rectangle(0, 0, canvas.Width, canvas.Height))
            g.Dispose()
End sub

Private Sub picturebox1_draw(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawImageUnscaled(canvas, 0, 0)
End Sub



Private Sub RotateThisPic(ByVal Rotation As Integer)
        Dim mx As New Drawing2D.Matrix
        Dim g As Graphics

        mx.RotateAt(Rotation, New PointF(PictureBox1.Width / 2, PictureBox1.Height / 2))
        g = Graphics.FromImage(canvas)
        g.Transform = mx

        g.Clear(Color.Black)

        g.DrawImage(PictureBox1.Image, 0, 0, PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Invalidate(New Rectangle(0, 0, (PictureBox1.Width), (PictureBox1.Height)))
End Sub
 
It is your 'canvas' bitmap that holds the rotated image. You don't set the new Picturebox.Image here but instead draw it over the picturebox control from the paint event. So in case you tried to save picturebox.image, that was some previous image, instead save the 'canvas' bitmap image. (or set picturebox.image to display the new image instead of painting over the control)
 
Back
Top