saving image.

gooden

Active member
Joined
May 18, 2007
Messages
32
Programming Experience
Beginner
Well i'm making a draw with external images and then i whant to save the image that is on the picture box but it say that the picturebox is empty

my code:

VB.NET:
Public Class Form1
    Dim button2 As Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ListBox1.SelectedIndex = DirectCast(sender, Button).Name.ToString
    End Sub
    Public Function create_button(ByVal i As Integer, ByVal nome As String)
        button2 = New Button
        button2.Name = nome
        button2.Image = ImageList1.Images(i)
        button2.FlatStyle = FlatStyle.Flat
        button2.ForeColor = Color.Black
        button2.Size = New Size(32, 32)
        AddHandler button2.Click, AddressOf Button1_Click
        FlowLayoutPanel1.Controls.Add(button2)
        create_button = True
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim imagem As Image
        Dim nome_da_imagem As String
        Dim numero
        Dim i As Integer

        'For Each itm As Object In My.Computer.FileSystem.GetFiles("c:\img\", FileIO.SearchOption.SearchTopLevelOnly, "*.bmp")
        '    Me.ListBox1.Items.Add(itm)  'add to list box
        ' Next
        Dim oRead As System.IO.StreamReader
        Dim linein
        oRead = IO.File.OpenText("Ground.txt")

        While oRead.Peek <> -1
            linein = oRead.ReadLine()
            Me.ListBox1.Items.Add(linein)
        End While

        oRead.Close()
        'percorrer items da lista
        For i = 0 To ListBox1.Items.Count - 1

            'obter nome deste item
            nome_da_imagem = ListBox1.Items.Item(i)
            numero = i

            'carregar do fixeiro
            imagem = Image.FromFile(nome_da_imagem)

            'por na lista, o magenta eh transparente
            ImageList1.Images.Add(imagem, Color.Magenta)
            create_button(i, numero)
            'FlowLayoutPanel1.Container.Add(newaa)
        Next i

    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        Dim graph As System.Drawing.Graphics
        Dim imagem As System.Drawing.Image

        Dim i As Integer

        'classe pa desenhar
        graph = PictureBox1.CreateGraphics()

        'obter imagem k tá seleccionada
        i = ListBox1.SelectedIndex
        imagem = ImageList1.Images.Item(i)


        'desenhar no sitio ond se clicou
        Dim x As Integer
        x = Int(Int(e.X) / 32) * 32
        Dim y As Integer
        y = Int(Int(e.Y) / 32) * 32
        graph.DrawImage(imagem, x, y)
    End Sub
   
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub
End Class
 
Assign the image to the Image property of Picturebox, or use the e.Graphics isntance in Paint event to have it draw on top of the control, for the latter case you have to do the same drawing to an image if you want it saved. CreateGraphics is not for drawing, once the form/control repaints for any reason your drawing will disappear.
 
VB.NET:
dim img as image = picturebox1.image
dim g as graphics = graphics.fromimage(img)
g.drawstuff()
g.dispose()
PictureBox1.Image = img
 
it gives me an error when i get the image of picturebox... it say that is null :|

code i have:
VB.NET:
Dim graph As System.Drawing.Graphics
        Dim imagem As System.Drawing.Image
        Dim img As Image = PictureBox1.Image
        Dim g As Graphics = Graphics.FromImage(img)

        Dim i As Integer

        'classe pa desenhar
        graph = PictureBox1.CreateGraphics()

        'obter imagem k tá seleccionada
        i = ListBox1.SelectedIndex
        imagem = ImageList1.Images.Item(i)


        'desenhar no sitio ond se clicou
        Dim x As Integer
        x = Int(Int(e.X) / 32) * 32
        Dim y As Integer
        y = Int(Int(e.Y) / 32) * 32
        graph.DrawImage(imagem, x, y)
        g.Dispose()
        PictureBox1.Image = img
 
I think you missed the point that CreateGraphics is not a Graphics instance you use to draw with.

About null image you could start a new one first.
 
In form load put this to start a new image:
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    End Sub
then use the code in post 4 in your mouse click.
VB.NET:
    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseClick
        Dim img As Image = PictureBox1.Image
        Dim g As Graphics = Graphics.FromImage(img)
        g.DrawString("hello", Me.Font, Brushes.Blue, e.X, e.Y)
        g.Dispose()
        PictureBox1.Image = img
    End Sub
Now anytime you can save the current image:
VB.NET:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button2.Click
        PictureBox1.Image.Save("save.bmp")
        Process.Start("save.bmp")
    End Sub
 
Back
Top