|
-
Feb 13th, 2012, 03:01 AM
#1
Thread Starter
Addicted Member
[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!
-
Feb 13th, 2012, 06:28 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2012, 07:58 PM
#3
Thread Starter
Addicted Member
Re: Save controls and image inside Picturebox
-
Feb 13th, 2012, 08:41 PM
#4
Thread Starter
Addicted Member
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.
-
Feb 13th, 2012, 09:22 PM
#5
Re: [RESOLVED] Save controls and image inside Picturebox
you need to specify a valid filename
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2012, 09:25 PM
#6
Thread Starter
Addicted Member
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)
-
Feb 13th, 2012, 09:31 PM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2012, 10:22 PM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] Save controls and image inside Picturebox
so it isnt possible to save it as an image? i need to access the image.
-
Feb 13th, 2012, 10:38 PM
#9
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2012, 10:40 PM
#10
Thread Starter
Addicted Member
Re: [RESOLVED] Save controls and image inside Picturebox
yup, im sorry if i wasn't too clear.
-
Feb 13th, 2012, 10:50 PM
#11
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2012, 11:03 PM
#12
Thread Starter
Addicted Member
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!
-
Feb 13th, 2012, 11:17 PM
#13
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 14th, 2012, 12:17 AM
#14
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|