VB.NET - VB 2010 code for saving a picturebox, with a drawing on it.

Asked By geert on 28-Feb-12 04:06 AM
'I  wanted a resultfile with an image ,wherupon a red line was painted,
'but what I got was only the image of the picturebox,
'without te red Line.
'the code-line:
'myPictureBox.Image.Save("c:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
'is wrong,
'What is the right  code for this?
 
Imports System.Drawing.Imaging
Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    myPictureBox.BackColor = Color.Yellow
    Dim file_name = "C:\TEMP\test.jpg"
    myPictureBox.Image = Image.FromFile(file_name)
  End Sub
  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim redPen As Pen = New Pen(Color.Red, 20)
    Dim point1 As Point = New Point(0, 0)
    Dim point2 As Point = New Point(130, 150)
    Dim G As Graphics
    G = myPictureBox.CreateGraphics
    G.DrawLine(redPen, point1, point2)
  End Sub
  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    myPictureBox.Image.Save("c:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
    picDownloaded.Image = Image.FromFile("c:\Temp\MyPicture.jpg")
  End Sub
End Class

I  wanted a resultfile with an image ,wherupon a red line was painted,
but what I got was only the image of the picturebox,
without te red Line.
the code-line:
myPictureBox.Image.Save("c:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
is wrong,
What is the right  code for this?
Somesh Yadav replied to geert on 28-Feb-12 04:10 AM
graphics drawn on a picturebox that way aren't persistent + aren't drawn to the picturebox image property. you need to draw directly to the underlying bitmap, either with fixed coordinates like you're using, or taking coordinates from the picturebox's mousedown + or mousemove events.

here's an example:

vb Code:

    Public Class Form1
      Dim img As Bitmap
      Dim gr As Graphics
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        img = New Bitmap("dummy.jpg")
        gr = Graphics.FromImage(img)
        PictureBox1.Image = img
      End Sub
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pen1 As New System.Drawing.Pen(System.Drawing.Color.Green, 4)
        gr.DrawEllipse(pen1, 70, 10, 100, 150)
        PictureBox1.Image = img
        img.Save("dummy.jpg", Drawing.Imaging.ImageFormat.Jpeg)
      End Sub
    End Class
kalpana aparnathi replied to geert on 28-Feb-12 04:25 AM
hi,

Try below code:

Dim sPath As String = "c:\Temp\MyPicture.jpg"
Dim bmp As Bitmap
  
Try
bmp = Me.PictureBox1.Image
bmp.Save(sPath)
Catch ex As Exception
MsgBox(ex.Message)
End Try
geert replied to Somesh Yadav on 28-Feb-12 05:36 AM
Thank you very much.

I'm glad to find a solution for saving such pictureboxes.
I searched a lot for this solution.
 
I will use this solution many times, I'm sure.

Geert Vullings
Somesh Yadav replied to geert on 28-Feb-12 06:09 AM
Welcome!