الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

المساعدة في التخلص من Linq في هذا الكود ليعمل مع .net 2

بدأه sscvbnm في 2 مايو 2012 · 0 رد · 343 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم ورحمة الله وبركاته

حصلت على هذا الكود لمعرفة طول وعرض الصورة من خلال الهيدر..

ثم حولته آلياً من C# إلى vb.net

لم أعرف كيف أتخلص من الجزء المتعلق بكود Linq في هذا الكود لأنني أريد استخدامه ضصمن .نت 2

المصدر:

http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height

http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file

الكود:

Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Imports System.Linq

Namespace DevWilson
    ''' <summary>
    ''' Taken from http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file/111349
    ''' Minor improvements including supporting unsigned 16-bit integers when decoding Jfif and added logic
    ''' to load the image using new Bitmap if reading the headers fails
    ''' </summary>
    Public NotInheritable Class ImageHeader
        Private Sub New()
        End Sub
        Const errorMessage As String = "Could not recognise image format."

        Private Shared imageFormatDecoders As New Dictionary(Of Byte(), Func(Of BinaryReader, Size))() From { _
         {New Byte() {&H42, &H4D}, DecodeBitmap}, _
         {New Byte() {&H47, &H49, &H46, &H38, &H37, &H61}, DecodeGif}, _
         {New Byte() {&H47, &H49, &H46, &H38, &H39, &H61}, DecodeGif}, _
         {New Byte() {&H89, &H50, &H4E, &H47, &HD, &HA, _
          &H1A, &HA}, DecodePng}, _
         {New Byte() {&HFF, &HD8}, DecodeJfif} _
        }

        ''' <summary>        
        ''' Gets the dimensions of an image.        
        ''' </summary>        
        ''' <param name="path">The path of the image to get the dimensions of.</param>        
        ''' <returns>The dimensions of the specified image.</returns>        
        ''' <exception cref="ArgumentException">The image was of an unrecognised format.</exception>        
        Public Shared Function GetDimensions(ByVal path As String) As Size
            Try
                Using binaryReader As New BinaryReader(File.OpenRead(path))
                    Try
                        Return GetDimensions(binaryReader)
                    Catch e As ArgumentException
                        Dim newMessage As String = String.Format("{0} file: '{1}' ", errorMessage, path)

                        Throw New ArgumentException(newMessage, "path", e)
                    End Try
                End Using
            Catch generatedExceptionName As ArgumentException
                'do it the old fashioned way

                Using b As New Bitmap(path)
                    Return b.Size
                End Using
            End Try
        End Function

        ''' <summary>        
        ''' Gets the dimensions of an image.        
        ''' </summary>        
        ''' <param name="path">The path of the image to get the dimensions of.</param>        
        ''' <returns>The dimensions of the specified image.</returns>        
        ''' <exception cref="ArgumentException">The image was of an unrecognised format.</exception>            
        Public Shared Function GetDimensions(ByVal binaryReader As BinaryReader) As Size
            Dim maxMagicBytesLength As Integer = imageFormatDecoders.Keys.OrderByDescending(Function(x) x.Length).First().Length
            Dim magicBytes As Byte() = New Byte(maxMagicBytesLength - 1) {}
            For i As Integer = 0 To maxMagicBytesLength - 1
                magicBytes(i) = binaryReader.ReadByte()
                For Each kvPair As var In imageFormatDecoders
                    If StartsWith(magicBytes, kvPair.Key) Then
                        Return kvPair.Value(binaryReader)
                    End If
                Next
            Next

            Throw New ArgumentException(errorMessage, "binaryReader")
        End Function

        Private Shared Function StartsWith(ByVal thisBytes As Byte(), ByVal thatBytes As Byte()) As Boolean
            For i As Integer = 0 To thatBytes.Length - 1
                If thisBytes(i) <> thatBytes(i) Then
                    Return False
                End If
            Next

            Return True
        End Function

        Private Shared Function ReadLittleEndianInt16(ByVal binaryReader As BinaryReader) As Short
            Dim bytes As Byte() = New Byte(2 - 1) {}

            For i As Integer = 0 To 2 - 1
                bytes(2 - 1 - i) = binaryReader.ReadByte()
            Next
            Return BitConverter.ToInt16(bytes, 0)
        End Function

        Private Shared Function ReadLittleEndianUInt16(ByVal binaryReader As BinaryReader) As UShort
            Dim bytes As Byte() = New Byte(2 - 1) {}

            For i As Integer = 0 To 2 - 1
                bytes(2 - 1 - i) = binaryReader.ReadByte()
            Next
            Return BitConverter.ToUInt16(bytes, 0)
        End Function

        Private Shared Function ReadLittleEndianInt32(ByVal binaryReader As BinaryReader) As Integer
            Dim bytes As Byte() = New Byte(4 - 1) {}
            For i As Integer = 0 To 4 - 1
                bytes(4 - 1 - i) = binaryReader.ReadByte()
            Next
            Return BitConverter.ToInt32(bytes, 0)
        End Function

        Private Shared Function DecodeBitmap(ByVal binaryReader As BinaryReader) As Size
            binaryReader.ReadBytes(16)
            Dim width As Integer = binaryReader.ReadInt32()
            Dim height As Integer = binaryReader.ReadInt32()
            Return New Size(width, height)
        End Function

        Private Shared Function DecodeGif(ByVal binaryReader As BinaryReader) As Size
            Dim width As Integer = binaryReader.ReadInt16()
            Dim height As Integer = binaryReader.ReadInt16()
            Return New Size(width, height)
        End Function

        Private Shared Function DecodePng(ByVal binaryReader As BinaryReader) As Size
            binaryReader.ReadBytes(8)
            Dim width As Integer = ReadLittleEndianInt32(binaryReader)
            Dim height As Integer = ReadLittleEndianInt32(binaryReader)
            Return New Size(width, height)
        End Function

        Private Shared Function DecodeJfif(ByVal binaryReader As BinaryReader) As Size
            While binaryReader.ReadByte() = &HFF
                Dim marker As Byte = binaryReader.ReadByte()
                Dim chunkLength As Short = ReadLittleEndianInt16(binaryReader)
                If marker = &HC0 Then
                    binaryReader.ReadByte()
                    Dim height As Integer = ReadLittleEndianInt16(binaryReader)
                    Dim width As Integer = ReadLittleEndianInt16(binaryReader)
                    Return New Size(width, height)
                End If

                If chunkLength < 0 Then
                    Dim uchunkLength As UShort = CUShort(chunkLength)
                    binaryReader.ReadBytes(uchunkLength - 2)
                Else
                    binaryReader.ReadBytes(chunkLength - 2)
                End If
            End While

            Throw New ArgumentException(errorMessage)
        End Function
    End Class
End Namespace

وللعلم هو لم يعمل بعد التحويل بسبب خطأ في الصياغة حتى لو استخدمت .نت 3.5

جزاكم الله خيراً

قال صلى الله عليه وسلم: من قال إذا أصبح: رضيت بالله ربا وبالإسلام دينا وبمحمد نبيا فأنا الزعيم لآخذن بيده حتى أدخله الجنة. الصحيحة/2686

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…