[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!
Re: Save controls and image inside Picturebox
i'd use a class + binary serializing + deserializing:
vb Code:
Public Class imageDetails
<System.Serializable()> Public Structure imageLabel
Dim location As Point
Dim text As String
End Structure
Private _image As Image
Public Property image() As Image
Get
Return _image
End Get
Set(ByVal value As Image)
_image = value
End Set
End Property
Private _imageLabels As New List(Of imageLabel)
Public ReadOnly Property imageLabels() As List(Of imageLabel)
Get
Return _imageLabels
End Get
End Property
Public Sub save(ByVal fileName As String, ByVal pb As PictureBox)
Me.image = pb.Image
Me.imageLabels.Clear()
For Each lbl As Label In pb.Controls.OfType(Of Label)()
Me.imageLabels.Add(New imageLabel With {.location = lbl.Location, .text = lbl.Text})
Next
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream(fileName, IO.FileMode.Create)
formatter.Serialize(fs, Me.image)
formatter.Serialize(fs, Me.imageLabels)
fs.Close()
End Sub
Public Sub New(ByVal fileName As String, ByVal pb As PictureBox)
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream(fileName, IO.FileMode.Open)
Me.image = DirectCast(formatter.Deserialize(fs), Image)
Me._imageLabels = DirectCast(formatter.Deserialize(fs), List(Of imageLabel))
fs.Close()
pb.Image = Me.image
For x As Integer = 0 To imageLabels.Count - 1
Dim lbl As New Label With { _
.Location = imageLabels(x).location, _
.Text = imageLabels(x).text}
pb.Controls.Add(lbl)
Next
End Sub
Public Sub New()
End Sub
End Class
this is the form code:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists("test.bin") Then
Dim details As New imageDetails("test.bin", PictureBox1)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim details As New imageDetails
details.save("test.bin", PictureBox1)
End Sub
End Class
Re: Save controls and image inside Picturebox
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.
Re: [RESOLVED] Save controls and image inside Picturebox
you need to specify a valid filename
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)
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
Re: [RESOLVED] Save controls and image inside Picturebox
so it isnt possible to save it as an image? i need to access the image.
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?
Re: [RESOLVED] Save controls and image inside Picturebox
yup, im sorry if i wasn't too clear.
Re: [RESOLVED] Save controls and image inside Picturebox
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
saveImage("test.bmp", Drawing.Imaging.ImageFormat.Bmp, PictureBox1)
End Sub
Private Sub saveImage(ByVal fileName As String, ByVal format As Drawing.Imaging.ImageFormat, ByVal pb As PictureBox)
Dim img As New Bitmap(DirectCast(pb.Image.Clone, Bitmap), pb.Width, pb.Height)
Dim gr As Graphics = Graphics.FromImage(img)
For Each lbl As Label In pb.Controls.OfType(Of Label)()
gr.DrawString(lbl.Text, lbl.Font, New SolidBrush(lbl.ForeColor), lbl.Left, lbl.Top)
Next
img.Save(fileName, format)
End Sub
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!
Re: [RESOLVED] Save controls and image inside Picturebox
vb Code:
Private Sub saveImage(ByVal fileName As String, ByVal format As Drawing.Imaging.ImageFormat, ByVal pb As PictureBox)
Dim img As New Bitmap(DirectCast(pb.Image.Clone, Bitmap), pb.image.Width, pb.image.Height)
Dim gr As Graphics = Graphics.FromImage(img)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Center
For Each lbl As Label In pb.Controls.OfType(Of Label)()
gr.FillRectangle(New SolidBrush(lbl.BackColor), lbl.Bounds)
gr.DrawString(lbl.Text, lbl.Font, New SolidBrush(lbl.ForeColor), lbl.Bounds, sf)
'this draws a red border
gr.DrawRectangle(Pens.Red, lbl.Bounds)
Next
img.Save(fileName, format)
End Sub
Re: [RESOLVED] Save controls and image inside Picturebox
again, thank you very much!