1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Public Class Form1
Private PicBoxList As New List(Of PicBoxInfo)
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.PictureBox1.Image = My.Resources.laptop
Me.PictureBox2.Image = My.Resources.mouse
Me.PictureBox3.Image = My.Resources.giraffe
End Sub
'Create a list of PictureBox info with every picture box on the form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateListPicInfo.Click
For Each Control As Control In Me.Controls
If TypeOf Control Is PictureBox Then
Dim ThisPictureBox As PictureBox = DirectCast(Control, PictureBox)
'//Get the image info
Dim BaseImage As New Bitmap(ThisPictureBox.Image.Width, PictureBox1.Image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
BaseImage = CType(ThisPictureBox.Image, Bitmap)
Dim rect As New Rectangle(0, 0, BaseImage.Width, BaseImage.Height)
Dim bitmapData As System.Drawing.Imaging.BitmapData = BaseImage.LockBits(rect, _
Drawing.Imaging.ImageLockMode.ReadWrite, _
BaseImage.PixelFormat)
Dim Pointer As IntPtr = bitmapData.Scan0
Dim bytes As Integer = bitmapData.Stride * BaseImage.Height
Dim Pixels(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(Pointer, Pixels, 0, bytes)
BaseImage.UnlockBits(bitmapData)
'//Put these info in the list
Dim ThisPicBoxInfo As New PicBoxInfo
With ThisPicBoxInfo
'if needed, add other properties
.width = BaseImage.Width
.height = BaseImage.Height
.stride = bitmapData.Stride
.Pixels = Pixels
.Name = ThisPictureBox.Name
.Location = ThisPictureBox.Location
End With
PicBoxList.Add(ThisPicBoxInfo)
End If
Next
End Sub
'Serialize the list to a file
Private pathApp As String = Directory.GetCurrentDirectory & "\GameImage.bin"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSerializeListPicInfo.Click
Dim BinFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim Stream As System.IO.Stream = File.Open(pathApp, FileMode.Create)
BinFormatter.Serialize(Stream, PicBoxList)
Stream.Close()
PicBoxList.Clear()
End Sub
'read back the file and deserialize
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeserializeListPicInfo.Click
Dim BinFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim Stream As System.IO.Stream = File.Open(pathApp, FileMode.Open)
PicBoxList = DirectCast(BinFormatter.Deserialize(Stream), List(Of PicBoxInfo))
Stream.Close()
End Sub
'Create a picturebox from one of the picturebox info in the list
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Create a picture box
Dim ThisPictureBox As New PictureBox
ThisPictureBox.Width = PicBoxList.Item(0).width
ThisPictureBox.Height = PicBoxList.Item(0).height
ThisPictureBox.Location = New Point(300, 200)
'Create the image from the info
Dim BaseImage As New Bitmap(PicBoxList.Item(0).width, PicBoxList.Item(0).height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim rect As New Rectangle(0, 0, BaseImage.Width, BaseImage.Height)
Dim bitmapData As System.Drawing.Imaging.BitmapData = BaseImage.LockBits(rect, _
Drawing.Imaging.ImageLockMode.ReadWrite, _
BaseImage.PixelFormat)
Dim Pointer As IntPtr = bitmapData.Scan0
Dim bytes As Integer = PicBoxList.Item(0).stride * BaseImage.Height
Dim Pixels() As Byte = PicBoxList.Item(0).Pixels
System.Runtime.InteropServices.Marshal.Copy(Pixels, 0, Pointer, bytes)
BaseImage.UnlockBits(bitmapData)
' put the image in a picture box
ThisPictureBox.Image = BaseImage
'Add the picture box to the form
Me.Controls.Add(ThisPictureBox)
End Sub
End Class
<Serializable()> _
Class PicBoxInfo
Public Pixels() As Byte
Public width As Integer
Public stride As Integer
Public height As Integer
Public Name As String
Public Location As Point
End Class |