EDN Admin
Well-known member
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small I made a painting on a picturebox in Visual Basic 2010.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small Now I wish to save that picture on my harddisk.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small But
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style=" <span style="font-size:small <span lang="EN-GB" style="font-family:Consolas; font-size:10pt pictureBox1.Image.Save(<span style="color:#a31515 "C:MyImage.jpg")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small does not work
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing.Drawing2D
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing.Imaging
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Text
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Public<span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<span style="color:blue Class <span style="color:#2b91af Form1
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green This example creates a PictureBox control on the form and draws to it.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green This example assumes that the Form_Load event handler method is connected
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green to the Load event of the form.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private pictureBox1 <span style="color:blue
As <span style="color:blue New <span style="color:#2b91af PictureBox()
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub Form1_Load(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.<span style="color:#2b91af EventArgs)
<span style="color:blue Handles <span style="color:blue MyBase.Load
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Dock the PictureBox to the form and set its background to white.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Dock = <span style="color:#2b91af DockStyle.Fill
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.BackColor = <span style="color:#2b91af Color.White
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Connect the Paint event of the PictureBox to the event handler method.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue AddHandler pictureBox1.Paint, <span style="color:blue
AddressOf <span style="color:blue Me.pictureBox1_Paint
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Add the PictureBox control to the Form.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Me.Controls.Add(pictureBox1)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue End <span style="color:blue Sub <span style="color:green
Form1_Load
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub pictureBox1_Paint(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.<span style="color:#2b91af PaintEventArgs)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Create a local version of the graphics object for the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Dim g <span style="color:blue As <span style="color:#2b91af
Graphics = e.Graphics
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Draw a string on the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
g.DrawString(<span style="color:#a31515 "This is a diagonal line drawn on the control", _
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue New <span style="color:#2b91af Font(<span style="color:#a31515 "Arial", 10),
<span style="color:#2b91af Brushes.Red, <span style="color:blue New
<span style="color:#2b91af PointF(30.0F, 30.0F))
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Draw a line in the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
g.DrawLine(System.Drawing.<span style="color:#2b91af Pens.Red, pictureBox1.Left, _
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green pictureBox1.Image.Save()
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green My.Computer.FileSystem.CreateDirectory("C:savedfiles")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue End <span style="color:blue Sub <span style="color:green
pictureBox1_Paint
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub Button1_Click(<span style="color:blue ByVal sender
<span style="color:blue As System.<span style="color:#2b91af Object,
<span style="color:blue ByVal e <span style="color:blue As System.<span style="color:#2b91af EventArgs)
<span style="color:blue Handles Button1.Click
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Image.Save(<span style="color:#a31515 "C:MyImage.jpg")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="font-family:Consolas; color:blue; font-size:7pt End<span style="font-family:Consolas; font-size:7pt
<span style="color:blue Sub
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt <span style="
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; color:blue; font-size:7pt End<span style="font-family:Consolas; font-size:7pt
<span style="color:blue Class
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-size:7pt <span style="font-family:Times New Roman
View the full article
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small Now I wish to save that picture on my harddisk.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small But
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style=" <span style="font-size:small <span lang="EN-GB" style="font-family:Consolas; font-size:10pt pictureBox1.Image.Save(<span style="color:#a31515 "C:MyImage.jpg")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small does not work
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Arial <span style="font-size:small
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing.Drawing2D
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Drawing.Imaging
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Imports<span lang="EN-GB" style="font-family:Consolas; font-size:7pt System.Text
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; color:blue; font-size:7pt Public<span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<span style="color:blue Class <span style="color:#2b91af Form1
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green This example creates a PictureBox control on the form and draws to it.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green This example assumes that the Form_Load event handler method is connected
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green to the Load event of the form.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private pictureBox1 <span style="color:blue
As <span style="color:blue New <span style="color:#2b91af PictureBox()
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub Form1_Load(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.<span style="color:#2b91af EventArgs)
<span style="color:blue Handles <span style="color:blue MyBase.Load
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Dock the PictureBox to the form and set its background to white.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Dock = <span style="color:#2b91af DockStyle.Fill
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.BackColor = <span style="color:#2b91af Color.White
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Connect the Paint event of the PictureBox to the event handler method.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue AddHandler pictureBox1.Paint, <span style="color:blue
AddressOf <span style="color:blue Me.pictureBox1_Paint
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Add the PictureBox control to the Form.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Me.Controls.Add(pictureBox1)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue End <span style="color:blue Sub <span style="color:green
Form1_Load
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub pictureBox1_Paint(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.<span style="color:#2b91af PaintEventArgs)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Create a local version of the graphics object for the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Dim g <span style="color:blue As <span style="color:#2b91af
Graphics = e.Graphics
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Draw a string on the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
g.DrawString(<span style="color:#a31515 "This is a diagonal line drawn on the control", _
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue New <span style="color:#2b91af Font(<span style="color:#a31515 "Arial", 10),
<span style="color:#2b91af Brushes.Red, <span style="color:blue New
<span style="color:#2b91af PointF(30.0F, 30.0F))
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green Draw a line in the PictureBox.
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
g.DrawLine(System.Drawing.<span style="color:#2b91af Pens.Red, pictureBox1.Left, _
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green pictureBox1.Image.Save()
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:green My.Computer.FileSystem.CreateDirectory("C:savedfiles")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue End <span style="color:blue Sub <span style="color:green
pictureBox1_Paint
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="color:blue Private <span style="color:blue Sub Button1_Click(<span style="color:blue ByVal sender
<span style="color:blue As System.<span style="color:#2b91af Object,
<span style="color:blue ByVal e <span style="color:blue As System.<span style="color:#2b91af EventArgs)
<span style="color:blue Handles Button1.Click
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
pictureBox1.Image.Save(<span style="color:#a31515 "C:MyImage.jpg")
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span lang="EN-GB" style="font-family:Consolas; font-size:7pt <span style="
<span style="font-family:Consolas; color:blue; font-size:7pt End<span style="font-family:Consolas; font-size:7pt
<span style="color:blue Sub
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt <span style="
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; color:blue; font-size:7pt End<span style="font-family:Consolas; font-size:7pt
<span style="color:blue Class
<p style="margin:0cm 0cm 0pt <span style="font-family:Consolas; font-size:7pt
<p style="margin:0cm 0cm 0pt <span style="font-size:7pt <span style="font-family:Times New Roman
View the full article