posts - 112, comments - 215, trackbacks - 0, articles - 34
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

.net 序列化文件(图像)成xml

Posted on 2007-12-11 16:08 济公 阅读(868) 评论(0)  编辑 收藏 引用 所属分类: XML

Imports System.Xml.Serialization
Imports System.Xml
Imports System.Drawing
Imports System.IO.File
Imports System.IO


Public Class SendImageReportXml
    '图像信息
    Public img1 As Object
    Public img2 As Object
    Public img3 As Object
    Public img4 As Object
    Public img5 As Object
    Public ImgByte1() As Byte
    Public ImgByte2() As Byte
    Public ImgByte3() As Byte
    Public ImgByte4() As Byte
    Public ImgByte5() As Byte
    Public ImgByte6() As Byte
    Public ImgByte7() As Byte
    Public ImgByte8() As Byte
    Public ImgByte9() As Byte
    Public ImgByte10() As Byte
    Public Image1type As Object
    Public Image2type As Object
    Public Image3type As Object
    Public Image4type As Object
    Public Image5type As Object
    Public Image6type As Object
    Public Image7type As Object
    Public Image8type As Object
    Public Image9type As Object
    Public Image10type As Object


    Public Image1name As Object
    Public Image2name As Object
    Public Image3name As Object
    Public Image4name As Object
    Public Image5name As Object
    Public Image6name As Object
    Public Image7name As Object
    Public Image8name As Object
    Public Image9name As Object
    Public Image10name As Object
    Public IntNum As Integer
    '头信息
    Public bill_type As String
    Public operation_type As String
    Public loc_code As String
    Public receive_code As String
    '病人信息
    Public SYSTEM_AUTO_ID As String
    Public PatientUID As String
    Public StudyInstanceUID As String
    Public SeriesInstanceUID As String
    Public PatientID As String
    Public PatientName As String
    Public PatientNameC As String
    Public PatientFN As String
    Public PatientFNC As String
    Public PatientSN As String
    Public PatientSNC As String
    Public PatientSex As String
    Public PatientBirthday As String
    Public PatientAge As String
    Public OutPatientNo As String
    Public InPatientNo As String
    Public InsuranceType As String
    Public CaseNO As String
    Public Patient_Stature As String
    Public PATIENT_WEIGHT As String
    Public PATIENT_AGE_UNIT As String
    Public Patient_Tel As String
    Public Patient_Address As String
    Public Create_By As String
    Public Create_Date As String

    '检查信息
    Public study_id As String
    Public SeriesDate As String
    Public StudyDate As String
    Public study_part As String
    Public study_doctor As String
    Public CLINIC_diagnose As String
    Public study_diagnose As String
    Public series_image As String
    Public diagnose_INFO As String
    Public diagnose_doctor As String
    Public apply_doctor As String
    Public SopInstanceUID1 As String
    Public SopInstanceUID2 As String
    Public SopInstanceUID3 As String
    Public SopInstanceUID4 As String
    Public SopInstanceUID5 As String
    Public songjiandanwei As String
    Public JianChayishi As String
    'patient_info 信息
    Public Patient_IDCard As String
    Public Patient_medicareno As String
    Public PATIENT_USERMEDICINE As String
    Public user_field1 As String
    Public user_field2 As String
    Public ISSMOKE As String
    Public ISDIABETES As String
    Public ISHYPERTENSION As String
    Public ISHIGHBLOODFAT As String
    Public ISMEDICINERECORD As String
    Public ISCARDIOPATHY As String
    Public ISTYPEANGINA As String
    Public ISNOTYPEANGINA As String
    Public ABSENCEANGINA As String
    Public ASYMPTOMATIC As String
    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

 

    Public Function INIRead(ByVal INIpath As String, ByVal sectionName As String, ByVal keyName As String, ByVal defaultValue As String) As String
        Dim n As Int32
        Dim sData As String
        sData = Space(1024)
        n = GetPrivateProfileString(sectionName, keyName, defaultValue, _
        sData, sData.Length, INIpath)
        If n > 0 Then
            INIRead = sData.Substring(0, n)
        Else
            INIRead = ""
        End If
    End Function
    Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
        WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
    End Function

    Public Function SaveReportXml(ByVal strHtmlPath As String, ByVal strHtmlName As String) As String
        Dim skin As New SendImageReportXml
        Dim strXmlFileName As String
        Dim dtImage As New DataTable
        Dim strXmlFilePath As String
        strXmlFilePath = "c:\\" & System.DateTime.Now.ToString("yyyy-MM-dd") & "\"
        If System.IO.Directory.Exists(strXmlFilePath) = False Then
            System.IO.Directory.CreateDirectory(strXmlFilePath)
        End If
        strXmlFileName = strXmlFilePath + "test.xml"
        Dim i As Integer

        Try

            Dim dir As New System.IO.DirectoryInfo(strHtmlPath)
            i = 0
            Dim fs = New System.IO.FileStream(strHtmlPath & strHtmlName, FileMode.OpenOrCreate, FileAccess.Read)
            Dim Tempimg(fs.Length) As Byte
            skin.Image1type = "html"
            skin.Image1name = strHtmlName
            skin.ImgByte1 = Tempimg
            i = 1
            fs.Read(Tempimg, 0, Convert.ToInt32(fs.Length))

            Dim fileList As String() = System.IO.Directory.GetFileSystemEntries(strHtmlPath & "images\")
            '遍历所有的文件和目录
            For Each file As String In fileList
                fs = New System.IO.FileStream(file, FileMode.OpenOrCreate, FileAccess.Read)
                ReDim Tempimg(fs.Length)
                fs.Read(Tempimg, 0, Convert.ToInt32(fs.Length))
                Dim fi As FileInfo = New FileInfo(file)
                If i = 1 Then
                    skin.ImgByte2 = Tempimg
                    skin.Image2name = fi.Name
                    'skin.Image2 = TempImage.FromFile(strFileName)
                    skin.Image2type = "jpg"

                ElseIf i = 2 Then
                    skin.ImgByte3 = Tempimg
                    skin.Image3name = fi.Name
                    ' skin.Image3 = TempImage.FromFile(strFileName)
                    skin.Image3type = "jpg"

                ElseIf i = 3 Then
                    skin.ImgByte4 = Tempimg
                    skin.Image4name = fi.Name
                    'skin.Image4 = TempImage.FromFile(strFileName)
                    skin.Image4type = "jpg"

                ElseIf i = 4 Then
                    skin.ImgByte5 = Tempimg
                    skin.Image5name = fi.Name
                    'skin.Image5 = TempImage.FromFile(strFileName)
                    skin.Image5type = "jpg"

                End If
                i = i + 1
                fs = Nothing
            Next

            skin.IntNum = i

            Dim serializer As New Xml.Serialization.XmlSerializer(GetType(SendImageReportXml))
            Dim xmlWriter As New Xml.XmlTextWriter(strXmlFileName, System.Text.Encoding.GetEncoding("GB2312"))
            'Dim xmlWriter As New Xml.XmlTextWriter(strXmlFileName, "GB2312")
            xmlWriter.Formatting = Formatting.Indented
            serializer.Serialize(xmlWriter, skin)
            xmlWriter.Flush()
            xmlWriter.Close()

        Catch ex As Exception
            strXmlFileName = "False"
        Finally
            GC.Collect()
        End Try
        Return strXmlFileName

    End Function

  

    Public Function ReadReportXml(ByVal strTempXmlFileName As String) As String
        Dim Skin As SendImageReportXml
        Dim objDataConnect As New MedExDataConnect

        Dim deSerializer As New Xml.Serialization.XmlSerializer(GetType(SendImageReportXml))
        'Dim colReturn As New Collection
        Dim strReturn As String
        Dim strXmlFileName As String
        Dim strTempImagePath As String
        Dim strTempHtmlPath As String
        strTempHtmlPath = "c:\\received\\"
        strTempImagePath = strTempHtmlPath & "images\\"
        strReturn = strTempHtmlPath

        Try
            'strXmlFileName = System.IO.Directory.GetParent(strTempXmlFileName).FullName() & "\temp\"
            'If System.IO.Directory.Exists(strXmlFileName) = False Then
            '    System.IO.Directory.CreateDirectory(strXmlFileName)
            'End If

            Skin = deSerializer.Deserialize(New Xml.XmlTextReader(strTempXmlFileName))
            IntNum = Skin.IntNum

            If System.IO.Directory.Exists(strTempHtmlPath) = False Then
                System.IO.Directory.CreateDirectory(strTempHtmlPath)
            End If

            If System.IO.Directory.Exists(strTempImagePath) = False Then
                System.IO.Directory.CreateDirectory(strTempImagePath)
            End If
            Dim i As Integer
            Dim ImageTempByte() As Byte

            For i = 0 To IntNum - 1
                If i = 0 Then
                    Image1type = Skin.Image1type
                    Image1name = Skin.Image1name
                    ImageTempByte = Skin.ImgByte1
                    Dim fsFile As New System.IO.FileStream(strTempHtmlPath + Image1name, FileMode.OpenOrCreate, FileAccess.Write)
                    fsFile.Write(ImageTempByte, 0, ImageTempByte.Length)
                    fsFile = Nothing


                End If

                If i = 1 Then
                    Image2type = Skin.Image2type
                    ImageTempByte = Skin.ImgByte2
                    Image2name = Skin.Image2name
                    Dim fsFile As New System.IO.FileStream(strTempImagePath + Image2name, FileMode.OpenOrCreate, FileAccess.Write)
                    fsFile.Write(ImageTempByte, 0, ImageTempByte.Length)
                    fsFile = Nothing

                End If

                If i = 2 Then
                    Image3type = Skin.Image3type
                    ImageTempByte = Skin.ImgByte3
                    Image3name = Skin.Image3name
                    Dim fsFile As New System.IO.FileStream(strTempImagePath + Image3name, FileMode.OpenOrCreate, FileAccess.Write)
                    fsFile.Write(ImageTempByte, 0, ImageTempByte.Length)
                    fsFile = Nothing

                End If

                If i = 3 Then
                    Image4type = Skin.Image4type

                    ImageTempByte = Skin.ImgByte4
                    Image4name = Skin.Image4name
                    Dim fsFile As New System.IO.FileStream(strTempImagePath + Image4name, FileMode.OpenOrCreate, FileAccess.Write)
                    fsFile.Write(ImageTempByte, 0, ImageTempByte.Length)
                    fsFile = Nothing

                End If

                If i = 4 Then
                    Image5type = Skin.Image5type

                    ImageTempByte = Skin.ImgByte5
                    Image5name = Skin.Image5name
                    Dim fsFile As New System.IO.FileStream(strTempImagePath + Image5name, FileMode.OpenOrCreate, FileAccess.Write)
                    fsFile.Write(ImageTempByte, 0, ImageTempByte.Length)
                    fsFile = Nothing

                End If
            Next

 

        Catch ex As Exception
            deSerializer = Nothing
            Skin = Nothing
            strReturn = "False"

            ' colReturn = Nothing
        Finally
            Skin.Finalize()

            Skin = Nothing
            deSerializer = Nothing
            GC.Collect()

        End Try

          Return strReturn
    End Function

End Class

只有注册用户登录后才能发表评论。