Community discussion forum

Save picture box drawing image.

  • 11 months ago
    Hi genius, I have a small requirement in my project. a mini drawing tools, which draw some lines, circle and rectangles over an image. so I use a picture box and write the following code to draw lines only by draging mouse. dim startPoint as Point dim endPoint as point dim bolMouseDown as boolen Private Sub pcbBodyImage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseDown startPoint = New Point(e.X, e.Y) bolMouseDown = True End Sub Private Sub pcbBodyImage_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseUp bolMouseDown = False End Sub Private Sub pcbBodyImage_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseMove If bolMouseDown Then endPoint = New System.Drawing.Point(e.X, e.Y) dim m_Pen as Pen m_Pen.ResetTransform() m_Pen.Width = 1 graphic.DrawLine(m_Pen, StartPoint, EndPoint) startPoint = endPoint End If End Sub These all are working fine but I am woundering to save the image after drawing. because when I save the image from the picturebox It only save the picture box image not the drawing image. Please help me regarding the same. How can I save both picturebox image as well as drawing image. Thanks in advance
  • 11 months ago
    in above post the code format is changed I don't know why I angain post it. Here I am using the picture box name pcbBodyImage Dim startPoint As Point Dim endPoint As Point Dim bolMouseDown As Boolean Private Sub pcbBodyImage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseDown startPoint = New Point(e.X, e.Y) bolMouseDown = True End Sub Private Sub pcbBodyImage_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseUp bolMouseDown = False End Sub Private Sub pcbBodyImageMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pcbBodyImage.MouseMove If bolMouseDown Then endPoint = New System.Drawing.Point(e.X, e.Y) Dim mPen As Pen m_Pen.ResetTransform() m_Pen.Width = 1 graphic = pcbBodyImage.CreateGraphics graphic.DrawLine(m_Pen, StartPoint, EndPoint) startPoint = endPoint End If End Sub Please help me its urgent
  • 11 months ago
    Hi Akthar, Instead of using the CreateGraphics() and obtaining the PictureBox Graphics Canvas to draw , try using the Bitmap object and follow the code as given below....to show and save... Public Class GraphicsForYou Private Graphics As System.Drawing.Graphics Private Image As System.Drawing.Bitmap Private Pen As System.Drawing.Pen Private Brush As System.Drawing.SolidBrush Private MyFont As System.Drawing.Font Private Sub DrawSaveImage() Image = New System.Drawing.Bitmap(MyPictureBox.Width, MyPictureBox.Height) Graphics = System.Drawing.Graphics.FromImage(Image) Graphics.FillRectangle(Brushes.LightYellow, 0, 0, MyPictureBox.Width, MyPictureBox.Height) Graphics.PageUnit = GraphicsUnit.Millimeter Pen = New System.Drawing.Pen(Color.Black, 0.2) Graphics.DrawRectangle(Pen, 10, 10, 140, 70) Pen.Width = 0.4 Graphics.DrawLine(Pen, 10, 50, 150, 25) MyPictureBox.Image = Image Image.Save("D:\Demo.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) End Sub End Class
  • 11 months ago
    Hi Akthar, Instead of using the CreateGraphics() and obtaining the PictureBox Graphics Canvas to draw , try using the Bitmap object and follow the code as given below....to show and save... Public Class GraphicsForYou Private Graphics As System.Drawing.Graphics Private Image As System.Drawing.Bitmap Private Pen As System.Drawing.Pen Private Brush As System.Drawing.SolidBrush Private MyFont As System.Drawing.Font Private Sub DrawSaveImage() Image = New System.Drawing.Bitmap(MyPictureBox.Width, MyPictureBox.Height) Graphics = System.Drawing.Graphics.FromImage(Image) Graphics.FillRectangle(Brushes.LightYellow, 0, 0, MyPictureBox.Width, MyPictureBox.Height) Graphics.PageUnit = GraphicsUnit.Millimeter Pen = New System.Drawing.Pen(Color.Black, 0.2) Graphics.DrawRectangle(Pen, 10, 10, 140, 70) Pen.Width = 0.4 Graphics.DrawLine(Pen, 10, 50, 150, 25) MyPictureBox.Image = Image Image.Save("D:\Demo.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) End Sub

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!