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
| Dim bitmaptest As Bitmap = New Bitmap("..\testimage.png")
Dim bitmap As Bitmap = New Bitmap(bitmaptest) 'transforme en 32 bits
Dim Graph As Graphics = Graphics.FromHwnd(Me.PictureBox1.Handle)
Dim Graph2 As Graphics = Graphics.FromHwnd(Me.PictureBox2.Handle)
Graph.DrawImage(bitmaptest, Me.PictureBox1.DisplayRectangle) 'Affichage de l'image en couleur
Dim width As Integer = bitmap.Width
Dim height As Integer = bitmap.Height
Dim x, y As Integer
For y = 0 To height - 1
For x = 0 To width - 1
Dim c As Drawing.Color = bitmap.GetPixel(x, y)
Dim luma As Int32 = CInt((c.R * 0.299 + c.G * 0.587 + c.B * 0.114))
bitmap.SetPixel(x, y, Drawing.Color.FromArgb(luma, luma, luma))
Next
Next
Dim Size As System.Drawing.Size = bitmap.Size 'Juste pour me rendre compte de la taille lors du débogage
Graph2.DrawImage(bitmap, Me.PictureBox2.DisplayRectangle) 'Affichage de l'image en nuance de gris
bitmap.Save("..\Sauvegarde.png", ImageFormat.Png) 'Sauvegarde de l'image
'Transformation en Byte Array
Dim bytImage As Byte()
Dim MS As MemoryStream = New MemoryStream()
' MS.SetLength(786433) , ce que je veux obtenir
bitmap.Save(MS, ImageFormat.Png)
bytImage = MS.ToArray()
Dim SW As StreamWriter = File.CreateText("..\textfile.txt")
For Each Q As Int32 In bytImage
SW.Write(bytImage(Q) & " ")
Next
'MessageBox.Show("fichier ecrit")
'On reconvertit le byteArray en image pour tester le fonctionnement
Dim Stream As New MemoryStream(bytImage, 0, bytImage.Length)
Dim Img As Bitmap = Image.FromStream(Stream, True)
Img.Save("..\TEST.png", ImageFormat.Png) |
Partager