Convert Bitmap To ByteArray & ViceVersa - silverlight - 22-09-17
الكلاسان التاليان يمكن استخدامهم في تحويل صورة الي مصفوفة بايت و العكس صحيح
الكلاس الأول: الهدف منه تحويل الصورة الي مصفوفة بايت
PHP كود :
'------------------------------------------------------------------------------ ' ' File Name : SecureBytes.vb ' Copyright (c) : RiverNile.Net 2017 All rights reserved. ' Date : 22/09/2017 ' Revision : 0.0.0.0 ' Description : A class to convert bitmap to bytes ' '------------------------------------------------------------------------------
Public Class SecureBytes Implements IDisposable
Private _valid As Integer Private _bmp As Bitmap Private _bytes As Byte()
Public Sub New(bmp As Bitmap) Me._bmp = bmp Me._valid = If(bmp IsNot Nothing, 1, 0) End Sub
Public ReadOnly Property ToBytes As Byte() Get Me.ConverToBytes() Return _bytes End Get End Property
Public ReadOnly Property Valid As Boolean Get Return Me._valid > 0 End Get End Property
Private Sub ConverToBytes() If _valid > 0 Then Dim widtheightBytes As Byte() = BitConverter.GetBytes(CUInt(_bmp.Width)) Dim heightBytes As Byte() = BitConverter.GetBytes(CUInt(_bmp.Height)) Dim data As Imaging.BitmapData = _bmp.LockBits(New Rectangle(0, 0, _bmp.Width, _bmp.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb) Dim bitmapBytes As Byte() = New Byte(data.Stride * data.Height - 1) {} Runtime.InteropServices.Marshal.Copy(data.Scan0, bitmapBytes, 0, bitmapBytes.Length) _bmp.UnlockBits(data) _bytes = New Byte(widtheightBytes.Length + heightBytes.Length + bitmapBytes.Length - 1) {} Array.Copy(widtheightBytes, 0, _bytes, 0, widtheightBytes.Length) Array.Copy(heightBytes, 0, _bytes, widtheightBytes.Length, heightBytes.Length) Array.Copy(bitmapBytes, 0, _bytes, widtheightBytes.Length + heightBytes.Length, bitmapBytes.Length) End If End Sub
Public Sub Dispose() If _bmp IsNot Nothing Then _bmp.Dispose() _bmp = Nothing End If Me._valid = 0 End Sub
Private Sub IDisposable_Dispose() Implements IDisposable.Dispose Me.Dispose() End Sub
End Class
الكلاس الثاني : الهددف منه تحويل مصفوفة البايت الي صورة
PHP كود :
'------------------------------------------------------------------------------ ' ' File Name : SecureBitmap.vb ' Copyright (c) : RiverNile.Net 2017 All rights reserved. ' Date : 23/09/2017 ' Revision : 0.0.0.0 ' Description : A class to convert bytes to bitmap ' '------------------------------------------------------------------------------
Public Class SecureBitmap Implements IDisposable
Private _valid As Integer Private _bmp As Bitmap Private _bytes As Byte()
Public Sub New(bytes As Byte()) Me._bytes = bytes Me._valid = If(bytes IsNot Nothing, 1, 0) End Sub
Public ReadOnly Property Valid As Boolean Get Return Me._valid > 0 End Get End Property
Public ReadOnly Property ToBitmap As Bitmap Get Me.ConverToBitmap() Return _bmp End Get End Property
Private Sub ConverToBitmap() If _valid > 0 Then Dim bitmapBytes As Byte() = _bytes.Skip(8).ToArray Dim bitmapDimensions As Byte() = _bytes.Take(8).ToArray Dim widthBytes As Byte() = bitmapDimensions.Take(4).ToArray Dim heightBytes As Byte() = bitmapDimensions.Skip(4).ToArray Me._bmp = New Bitmap(CInt(BitConverter.ToUInt32(widthBytes, 0)), CInt(BitConverter.ToUInt32(heightBytes, 0)))
Dim data As Imaging.BitmapData = _bmp.LockBits(New Rectangle(0, 0, _bmp.Width, _bmp.Height), Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format32bppArgb) Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, data.Scan0, bitmapBytes.Length) _bmp.UnlockBits(data)
End If End Sub
Public Sub Dispose() If _bytes IsNot Nothing Then _bytes = Nothing End If Me._valid = 0 End Sub
Private Sub IDisposable_Dispose() Implements IDisposable.Dispose Me.Dispose() End Sub
End Class
الاستخدام
التحويل الي مصفوفة بايت
PHP كود :
Dim bmp As Bitmap = "any image" Dim toBytes As Byte() = New SecureBytes(bmp).ToBytes
التحويل الي صورة
PHP كود :
Dim securedBytes As Byte() = toBytes Dim img As Bitmap = New SecureBitmap(securedBytes).ToBitmap
و من لا يريد استخدام الكلاسات أعلاه
فيمكنه استخدام الدوال التالية
PHP كود :
Private Function ToBytes(bmp As Bitmap) As Byte()
Dim widtheightBytes As Byte() = BitConverter.GetBytes(CUInt(bmp.Width)) Dim heightBytes As Byte() = BitConverter.GetBytes(CUInt(bmp.Height)) Dim data As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb) Dim bitmapBytes As Byte() = New Byte(data.Stride * data.Height - 1) {} Runtime.InteropServices.Marshal.Copy(data.Scan0, bitmapBytes, 0, bitmapBytes.Length) bmp.UnlockBits(data)
Dim result As Byte() = New Byte(widtheightBytes.Length + heightBytes.Length + bitmapBytes.Length - 1) {} Array.Copy(widtheightBytes, 0, result, 0, widtheightBytes.Length) Array.Copy(heightBytes, 0, result, widtheightBytes.Length, heightBytes.Length) Array.Copy(bitmapBytes, 0, result, widtheightBytes.Length + heightBytes.Length, bitmapBytes.Length)
Return result End Function
Private Function ToBitmap(buffer As Byte()) As Drawing.Bitmap
Dim bitmapBytes As Byte() = buffer.Skip(8).ToArray Dim bitmapDimensions As Byte() = buffer.Take(8).ToArray Dim widthBytes As Byte() = bitmapDimensions.Take(4).ToArray Dim heightBytes As Byte() = bitmapDimensions.Skip(4).ToArray
Dim bmp As Bitmap = New Bitmap(CInt(BitConverter.ToUInt32(widthBytes, 0)), CInt(BitConverter.ToUInt32(heightBytes, 0))) Dim data As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format32bppArgb) Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, data.Scan0, bitmapBytes.Length) bmp.UnlockBits(data) Return bmp
End Function
أتمني أن يكون الكود مفيدا للبعض منكم
تقبلوا تحياتي
|