Results 1 to 14 of 14

Thread: [RESOLVED] Save controls and image inside Picturebox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Resolved [RESOLVED] Save controls and image inside Picturebox

    I've googled the topic but all I see are also questions like mine and drawing shapes on a picture box then saving them.

    My current situation:
    I have a scrollable picturebox (achieved using panel + picturebox). On top of the image inside the picturebox I've added labels on different locations(the labels are added on run-time using PictureBox1.Controls.Add etc). I want to save the contents as well as the image inside the picturebox.

    Thank you!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Save controls and image inside Picturebox

    i'd use a class + binary serializing + deserializing:

    vb Code:
    1. Public Class imageDetails
    2.  
    3.     <System.Serializable()> Public Structure imageLabel
    4.         Dim location As Point
    5.         Dim text As String
    6.     End Structure
    7.  
    8.     Private _image As Image
    9.     Public Property image() As Image
    10.         Get
    11.             Return _image
    12.         End Get
    13.         Set(ByVal value As Image)
    14.             _image = value
    15.         End Set
    16.     End Property
    17.  
    18.     Private _imageLabels As New List(Of imageLabel)
    19.     Public ReadOnly Property imageLabels() As List(Of imageLabel)
    20.         Get
    21.             Return _imageLabels
    22.         End Get
    23.     End Property
    24.  
    25.     Public Sub save(ByVal fileName As String, ByVal pb As PictureBox)
    26.  
    27.         Me.image = pb.Image
    28.         Me.imageLabels.Clear()
    29.         For Each lbl As Label In pb.Controls.OfType(Of Label)()
    30.             Me.imageLabels.Add(New imageLabel With {.location = lbl.Location, .text = lbl.Text})
    31.         Next
    32.  
    33.         Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    34.         Dim fs As New IO.FileStream(fileName, IO.FileMode.Create)
    35.  
    36.         formatter.Serialize(fs, Me.image)
    37.         formatter.Serialize(fs, Me.imageLabels)
    38.         fs.Close()
    39.     End Sub
    40.  
    41.     Public Sub New(ByVal fileName As String, ByVal pb As PictureBox)
    42.         Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    43.         Dim fs As New IO.FileStream(fileName, IO.FileMode.Open)
    44.         Me.image = DirectCast(formatter.Deserialize(fs), Image)
    45.         Me._imageLabels = DirectCast(formatter.Deserialize(fs), List(Of imageLabel))
    46.         fs.Close()
    47.         pb.Image = Me.image
    48.         For x As Integer = 0 To imageLabels.Count - 1
    49.             Dim lbl As New Label With { _
    50.             .Location = imageLabels(x).location, _
    51.             .Text = imageLabels(x).text}
    52.             pb.Controls.Add(lbl)
    53.         Next
    54.     End Sub
    55.  
    56.     Public Sub New()
    57.  
    58.     End Sub
    59.  
    60. End Class

    this is the form code:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         If IO.File.Exists("test.bin") Then
    5.             Dim details As New imageDetails("test.bin", PictureBox1)
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Dim details As New imageDetails
    11.         details.save("test.bin", PictureBox1)
    12.     End Sub
    13.  
    14. End Class

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Save controls and image inside Picturebox

    thank you!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    quick question, i changed this:

    Code:
    details.save("C:\cdoEARTH_maps\Reports\[different file formats]", PictureBox1)
    but i get a "format not currently supported"

    sorry, im really new at this.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Save controls and image inside Picturebox

    you need to specify a valid filename

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    yup.. i tried these:

    Code:
    details.save("C:\cdoEARTH_maps\Reports\xxx.jpg", PictureBox1)
    details.save("C:\cdoEARTH_maps\Reports\xxx.png", PictureBox1)
    details.save("C:\cdoEARTH_maps\Reports\xxx.bmp", PictureBox1)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Save controls and image inside Picturebox

    it saves the image in the picturebox + the labels location + text as a binary file
    you shouldn't try to save it as a known image file type because it isn't

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    so it isnt possible to save it as an image? i need to access the image.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Save controls and image inside Picturebox

    so you want to merge your labels into the pb image + save it as an image file?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    yup, im sorry if i wasn't too clear.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Save controls and image inside Picturebox

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     saveImage("test.bmp", Drawing.Imaging.ImageFormat.Bmp, PictureBox1)
    3. End Sub
    4.  
    5. Private Sub saveImage(ByVal fileName As String, ByVal format As Drawing.Imaging.ImageFormat, ByVal pb As PictureBox)
    6.     Dim img As New Bitmap(DirectCast(pb.Image.Clone, Bitmap), pb.Width, pb.Height)
    7.     Dim gr As Graphics = Graphics.FromImage(img)
    8.  
    9.     For Each lbl As Label In pb.Controls.OfType(Of Label)()
    10.         gr.DrawString(lbl.Text, lbl.Font, New SolidBrush(lbl.ForeColor), lbl.Left, lbl.Top)
    11.     Next
    12.  
    13.     img.Save(fileName, format)
    14.  
    15. End Sub

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    thanks! i changed the dimensions of the image using pb.image.width and height.

    ill just figure out how to add background and border. thank you!

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Save controls and image inside Picturebox

    vb Code:
    1. Private Sub saveImage(ByVal fileName As String, ByVal format As Drawing.Imaging.ImageFormat, ByVal pb As PictureBox)
    2.     Dim img As New Bitmap(DirectCast(pb.Image.Clone, Bitmap), pb.image.Width, pb.image.Height)
    3.     Dim gr As Graphics = Graphics.FromImage(img)
    4.  
    5.     Dim sf As New StringFormat
    6.     sf.Alignment = StringAlignment.Near
    7.     sf.LineAlignment = StringAlignment.Center
    8.  
    9.     For Each lbl As Label In pb.Controls.OfType(Of Label)()
    10.         gr.FillRectangle(New SolidBrush(lbl.BackColor), lbl.Bounds)
    11.         gr.DrawString(lbl.Text, lbl.Font, New SolidBrush(lbl.ForeColor), lbl.Bounds, sf)
    12.         'this draws a red border
    13.         gr.DrawRectangle(Pens.Red, lbl.Bounds)
    14.     Next
    15.  
    16.     img.Save(fileName, format)
    17.  
    18. End Sub

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: [RESOLVED] Save controls and image inside Picturebox

    again, thank you very much!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width