السلام عليكم ورحمة الله وبركاته
قمت بتصميم Class صغيره لتسهيل التعامل مع الصور من خلال ADO.NET من حيث عمليات الادخال والحذف والتنقل والتأكد من حجم الصور قبل ادخالها , وقمت بعمل مثال لاختبارها و أرجو منكم اختبارها في مشاريع أخري لاكتشاف نقاط الضعف فيها ومعالجتها وكذلك وضع أي اضافات أخري , منتظر اقتراحاتكم , وأي استفسار ...,
Namespace SmartSoft #Region " About Author " 'بسم الله الرحمن الرحيم 'This class is developed by: Hazem Mohammed 3amer 'Assiut-Egypt 'mobile (+2)0102319009 'You can use it freely but you can`t replace my name with any other name 'This Class is mainly developed to manage adding pitures into databases and navigation 'between images in the database 'enjoy it #End Region Friend Class ImageDBManager #Region " General Variables " Public _position As Integer ' the position of current field Dim _DataSource As DataSet ' the dataset that contains the table Dim _DatatableName As String ' table name Dim _ImageFieldName As String ' column which have images Dim _PictureBox As PictureBox ' our picture box which display images #End Region #Region " Convert Image to ByteArray and Clear PictureBox " Public Sub ImageBinding(ByVal DataSource As DataSet, ByVal DatatableName As String, _ ByVal ImageFieldName As String, ByVal PictureBox As PictureBox) 'put the specified values(Parameters) in the fields to use them in the other sub procedures 'Remember _position = 0 since the class created _DataSource = DataSource _DatatableName = DatatableName _ImageFieldName = ImageFieldName _PictureBox = PictureBox 'get the first image in the column and put it in the picture box as binding method If DataSource.Tables(DatatableName).Rows.Count = 0 Then Exit Sub Else 'select the first image in the column and put it in the picturebox ' via converting it into byte array and pass it to memory stream 'and put it in the picturebox Dim Dataaray() As Byte = DataSource.Tables(DatatableName).Rows(0)(ImageFieldName) Dim mems As New IO.MemoryStream(Dataaray) PictureBox.Image = Image.FromStream(mems) End If End Sub Public Function IsImageLargerThan(ByVal ImageFilePath As String, ByVal SizeinKB As Integer) As Boolean 'getting image as byte array from file via filestream and messure its length 'and check the length in KB Dim fs As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read) If fs.Length / 1024 > SizeinKB Then Return True Else Return False End If End Function Public Function IsImageLargerThan(ByVal PictureBox As PictureBox, ByVal SizeinKB As Integer) As Boolean 'overloded method to check the size from the picturebox Dim mems As New IO.MemoryStream PictureBox.Image.Save(mems, PictureBox.Image.RawFormat) If mems.Length / 1024 > SizeinKB Then Return True Else Return False End If End Function Public Function ImagetoArray(ByVal PictureBox As PictureBox) As Byte() 'get the image as byte array from the picturebox to be used in the method 'that inserting data by the developer in the project Dim imgarr As Byte() Dim ms As New IO.MemoryStream() PictureBox.Image.Save(ms, PictureBox.Image.RawFormat) imgarr = ms.GetBuffer ms.Close() Return imgarr End Function Public Function ImagetoArray(ByVal ImageFilePath As String) As Byte() 'overloded method to do it by the image file Dim imgarr As Byte() Dim fs As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read) Dim sr As New IO.BinaryReader(fs) imgarr = sr.ReadBytes(fs.Length) sr.Close() fs.Close() Return imgarr End Function Public Sub DeleteCurrent(ByVal PictureBox As PictureBox) 'handling image deleting to simulate (Binding.BindingManagerBase.RemoveAt) method If Me._position = 0 Then 'if the field is the first field put the next image in the picture box If _DataSource.Tables(_DatatableName).Rows.Count = 1 Then PictureBox.Image = Nothing 'to synchronise the positions of the other fields because the coming ' image take the position of the deleted image Me.NextImage() Me._position -= 1 'here we check if the we are in the last record before check if position > 0 beacuase 'the last record is > 0 but not all records > 0 is the last record ElseIf Me._position = _DataSource.Tables(_DatatableName).Rows.Count - 1 Then Me.PreviousImage() ElseIf Me._position > 0 Then Me.NextImage() Me._position -= 1 End If End Sub #End Region #Region " Navigation " Public Sub NextImage() 'get the next image by increasing the position and validate the position value _position += 1 If _position > _DataSource.Tables(_DatatableName).Rows.Count - 1 Then _position -= 1 Else Dim Dataaray() As Byte = _DataSource.Tables(_DatatableName).Rows(_position)(_ImageFieldName) Dim mems As New IO.MemoryStream(Dataaray) _PictureBox.Image = Image.FromStream(mems) End If End Sub Public Sub PreviousImage() 'get the previous image by decreasing the position and validate the position value _position -= 1 If _position < 0 Then _position += 1 Else Dim Dataaray() As Byte = _DataSource.Tables(_DatatableName).Rows(_position)(_ImageFieldName) Dim mems As New IO.MemoryStream(Dataaray) _PictureBox.Image = Image.FromStream(mems) End If End Sub Public Sub FisrtImage() 'get the first image if the records number more than 0 If _DataSource.Tables(_DatatableName).Rows.Count = 0 Then Exit Sub Else _position = 0 Dim Dataaray() As Byte = _DataSource.Tables(_DatatableName).Rows(_position)(_ImageFieldName) Dim mems As New IO.MemoryStream(Dataaray) _PictureBox.Image = Image.FromStream(mems) End If End Sub Public Sub LastImage() 'get the last image if the records number more than 0 If _DataSource.Tables(_DatatableName).Rows.Count = 0 Then Exit Sub Else _position = _DataSource.Tables(_DatatableName).Rows.Count - 1 Dim Dataaray() As Byte = _DataSource.Tables(_DatatableName).Rows(_position)(_ImageFieldName) Dim mems As New IO.MemoryStream(Dataaray) _PictureBox.Image = Image.FromStream(mems) End If End Sub #End Region End Class End Namespace

