Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In VB 2010:
How do i save a drawing on a picturebox to a file on harddisk

VB
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
        myPictureBox.Image = Image.FromFile("C:\TEMP\test.jpg")
    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)
    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
End Class

the save command ( Button1) did not work.
I got the mesage:
Use the New keyword to create the instance.

when I load an image in myPictureBox,
as I now did in the line:
myPictureBox.Image = Image.FromFile("C:\TEMP\test.jpg")

The Save command worked,but the result is not what I want.
I wanted the redline ON my image.
(What I am trying to do is making a program for a child to give color on an loaded bitmap,
and the possebility to store the colored bitmap)

[Edit - Added code block]
Posted
Updated 27-Feb-12 9:04am
v4

Try:
VB
myPictureBox.Image.Save("D:\Temp\MyPicture.jpg", ImageFormat.Jpeg)
 
Share this answer
 
Comments
geert vullings 26-Feb-12 3:04am    
I became an error saying:
{"Object reference not set to an instance of an object."}
OriginalGriff 26-Feb-12 3:28am    
Then you haven't set the Image property (in which case you have nothing to save) or you haven't changed "myPictureBox" to the name of your actual PictureBox!
geert vullings 27-Feb-12 13:57pm    
the save command ( Button1) did not work
I got the mesage:
Use the New keyword to create the instance.
I have updated my question.
OriginalGriff 27-Feb-12 14:06pm    
I have looked at your code, now you have posted it, and two things occur:
1) Whenever you call CreateGraphics to get a graphics context, you are responsible for calling Dispose on it - if you do not, you will have a problem as the supply of such graphics contexts is limited, and you will run out. At that point your program will fail - but it may not fail in your code! It can be very difficult to spot such problems.

2) You see to be under the illusion that drawing a line on the PictureBox changes the Image inside it - this is not the case.
You do no appear to allocate an Image to the PictureBox at all - which is why you get the "not set to an instance of an object" error, and you do not draw into the Image anyway - so it would not save your changes if you did.

Take a step back, think about what you are trying to do, and decide: do I want to draw a line on the screen? Or on a Image control which will be shown on the screen?

The two processes are very similar in execution, but they have profoundly different results.
geert vullings 27-Feb-12 15:05pm    
when I load an image in myPictureBox,
as I now did in the line:
myPictureBox.Image = Image.FromFile("C:\TEMP\test.jpg")

The Save command worked,but the result is not what I want.
I wanted the redline ON my image.
(What I am trying to do is making a program for a child to give color on an loaded bitmap,
and the possebility to store the colored bitmap)
If you have a need to save an image of the PictureBox, the chances are, you misuse this class. Usually, you don't need it at all. It is good only to present some picture; in more complex cases it does not help, only adds some hassles.

I explained why and what to do instead in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

Just in case, please review it.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900