none
VCard Photo auslesen und als Image speichern RRS feed

  • Frage

  • Hallo,

    ich möchte die Inhalte einer VCard in eine Tabelle speichern.
    So auch das darin enthaltene Photo welches in  Form von:

    PHOTO;ENCODING=BASE64;TYPE=JPEG:
    /9j/4AAQSkZJRgABAgEAYABgAAD/7gAOQ
    WRvYmUAZAAAAAAB/+EN/kV4aWYAAE1N
    ACoAAAAIAAgBMgACAAAAFAAAAG4BOwAC
    AAAACwAAAIJHRgADAAAAAQAFAABHSQAD
    AAAAAQBYAACCmAACAA
    ......

    enthalten ist und anschließend die Inhalte der Tabelle mit
    Photos als Images in einem GriedView darstellen.


    Grüße aus Oberhausen,

    Steffen
    Sonntag, 10. Juli 2011 08:41

Antworten

  • Hallo Steffen,

    das Prinzip ist dasselbe wie hier beschrieben:

      http://social.msdn.microsoft.com/Forums/de-DE/aspnetajaxmvcde/thread/406c5d1b-8ba0-43c1-8e7d-16cef652c69c#ce9b2454-3966-4cee-8f89-5498266b1a69

     


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    Sonntag, 10. Juli 2011 12:59
    Moderator
  • Hallo Steffen,

    Fotos in VCF Dateien sind als Base64 codierte Zeichenfolgen eingebettet. Die daraus resultierende Bytefolge entspricht der Grafikdatei und kann bspw. über einen MemoryStream und der Image.FromStream Methode eingelesen werden. Anbei mal ein minimales Beispiel zum Lesen einer VCF Datei:

    Public Class VCardReader
     Public Shared Function Open(ByVal name As String) As VCard
      Dim lVcf As New VCard()
      Dim lReadImage As Boolean
      Dim lImageString As System.Text.StringBuilder = Nothing
      Dim lLines() As String = IO.File.ReadAllLines(name, _
                      System.Text.Encoding.Default)
    
      For Each lLine In lLines
       If lReadImage Then
        If lLine <> "" Then
         lImageString.Append(lLine.Trim())
    
        Else
         lReadImage = False
    
         Dim lImageStream As New IO.MemoryStream(Convert.FromBase64String(lImageString.ToString()))
         lVcf.Photo = Image.FromStream(lImageStream)
         lVcf.Photo.Tag = lImageStream
        End If
    
    
       ElseIf lLine.StartsWith("N") Then
        Dim lN() As String = lLine.Split(":")(1).Split(";")
        lVcf.Surname = lN(0)
    
        If lN.Length >= 1 Then
         lVcf.Name = lN(1)
        End If
    
       ElseIf lLine.StartsWith("FN") Then
        lVcf.DisplayName = lLine.Split(":")(1)
    
       ElseIf lLine.StartsWith("EMAIL") Then
        lVcf.EMail = lLine.Split(":")(1)
    
       ElseIf lLine.StartsWith("X-MS-TEXT") Then
        lVcf.CustomFields.Add(lLine.Split(":")(1))
    
       ElseIf lLine.StartsWith("PHOTO") Then
        Dim lAttributes() As String = lLine.Split(";")
    
        For Each lAttr As String In lAttributes
         If lAttr.StartsWith("TYPE") Then
          Select Case lAttr.Substring(5)
           Case "JPEG", "BMP", "PNG", "GIF", "TIFF"
            lImageString = New System.Text.StringBuilder()
            lReadImage = True
    
           Case Else
            Debug.Print("Unsupported image format.")
    
          End Select
         End If
        Next
       End If
      Next
    
      Return lVcf
     End Function
    End Class
    
    Public Class VCard
     Private m_CustomFields As New List(Of String)
    
     Public ReadOnly Property CustomFields() As List(Of String)
      Get
       Return m_CustomFields
      End Get
     End Property
    
     Public Property DisplayName As String
     Public Property EMail As String
     Public Property Name As String
     Public Property Photo As Image
     Public Property Surname As String
    End Class
    

    Angewandt:

      Dim lVcf As VCard = VCardReader.Open("D:\Temp\VCard.vcf")
      PictureBox1.Image = lVcf.Photo
    
      lblDisplayName.Text = lVcf.DisplayName
      lblName.Text = lVcf.Surname & ", " & lVcf.Name
      lblEMail.Text = lVcf.EMail
    
      For Each lItem In lVcf.CustomFields
       ListBox1.Items.Add(lItem)
      Next
    


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    Sonntag, 10. Juli 2011 13:07

Alle Antworten

  • Hallo Steffen,

    das Prinzip ist dasselbe wie hier beschrieben:

      http://social.msdn.microsoft.com/Forums/de-DE/aspnetajaxmvcde/thread/406c5d1b-8ba0-43c1-8e7d-16cef652c69c#ce9b2454-3966-4cee-8f89-5498266b1a69

     


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    Sonntag, 10. Juli 2011 12:59
    Moderator
  • Hallo Steffen,

    Fotos in VCF Dateien sind als Base64 codierte Zeichenfolgen eingebettet. Die daraus resultierende Bytefolge entspricht der Grafikdatei und kann bspw. über einen MemoryStream und der Image.FromStream Methode eingelesen werden. Anbei mal ein minimales Beispiel zum Lesen einer VCF Datei:

    Public Class VCardReader
     Public Shared Function Open(ByVal name As String) As VCard
      Dim lVcf As New VCard()
      Dim lReadImage As Boolean
      Dim lImageString As System.Text.StringBuilder = Nothing
      Dim lLines() As String = IO.File.ReadAllLines(name, _
                      System.Text.Encoding.Default)
    
      For Each lLine In lLines
       If lReadImage Then
        If lLine <> "" Then
         lImageString.Append(lLine.Trim())
    
        Else
         lReadImage = False
    
         Dim lImageStream As New IO.MemoryStream(Convert.FromBase64String(lImageString.ToString()))
         lVcf.Photo = Image.FromStream(lImageStream)
         lVcf.Photo.Tag = lImageStream
        End If
    
    
       ElseIf lLine.StartsWith("N") Then
        Dim lN() As String = lLine.Split(":")(1).Split(";")
        lVcf.Surname = lN(0)
    
        If lN.Length >= 1 Then
         lVcf.Name = lN(1)
        End If
    
       ElseIf lLine.StartsWith("FN") Then
        lVcf.DisplayName = lLine.Split(":")(1)
    
       ElseIf lLine.StartsWith("EMAIL") Then
        lVcf.EMail = lLine.Split(":")(1)
    
       ElseIf lLine.StartsWith("X-MS-TEXT") Then
        lVcf.CustomFields.Add(lLine.Split(":")(1))
    
       ElseIf lLine.StartsWith("PHOTO") Then
        Dim lAttributes() As String = lLine.Split(";")
    
        For Each lAttr As String In lAttributes
         If lAttr.StartsWith("TYPE") Then
          Select Case lAttr.Substring(5)
           Case "JPEG", "BMP", "PNG", "GIF", "TIFF"
            lImageString = New System.Text.StringBuilder()
            lReadImage = True
    
           Case Else
            Debug.Print("Unsupported image format.")
    
          End Select
         End If
        Next
       End If
      Next
    
      Return lVcf
     End Function
    End Class
    
    Public Class VCard
     Private m_CustomFields As New List(Of String)
    
     Public ReadOnly Property CustomFields() As List(Of String)
      Get
       Return m_CustomFields
      End Get
     End Property
    
     Public Property DisplayName As String
     Public Property EMail As String
     Public Property Name As String
     Public Property Photo As Image
     Public Property Surname As String
    End Class
    

    Angewandt:

      Dim lVcf As VCard = VCardReader.Open("D:\Temp\VCard.vcf")
      PictureBox1.Image = lVcf.Photo
    
      lblDisplayName.Text = lVcf.DisplayName
      lblName.Text = lVcf.Surname & ", " & lVcf.Name
      lblEMail.Text = lVcf.EMail
    
      For Each lItem In lVcf.CustomFields
       ListBox1.Items.Add(lItem)
      Next
    


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    Sonntag, 10. Juli 2011 13:07