Display Metadata-thumbnail of Jpeg in picturebox

ぃ、小莉子 提交于 2019-12-13 05:35:44

问题


I need to display a Image's thumbnail, that is saved in its Metadata in a picturebox. I'm using VB.NET

http://msdn.microsoft.com/en-us/library/windows/desktop/ee719904%28v=vs.85%29.aspx#_jpeg_metadata

So far i came up with this. Adding a breakpoint displays that GETQUERY returns empty even if i know that the file does indeed have a thumbnail

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim imagepath = "C:\xampp\htdocs\Downloads\IMG_1322.JPG" ' path to file
    Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
    Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
    Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)

    Dim ms As New System.IO.MemoryStream

    Dim bm As Bitmap
    Dim arData() As Byte

    arData = metadata.GetQuery("/app0/{ushort=6}") '<--- Breakpoint here: Query returns nothing!


    ms.Write(arData, 78, arData.Length - 78)

    bm = New Bitmap(ms)

    PictureBox1.Image = bm
    stream.Close()

End Sub

回答1:


You can try something like this:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim imagepath = "C:\xampp\htdocs\Downloads\IMG_1322.JPG" ' path to file
    Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
    Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
    Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)

    Dim thumb As BitmapMetadataBlob
    thumb = metadata.GetQuery("/app1/thumb/")
    If Not (thumb Is Nothing) Then
        Dim src As New BitmapImage
        Dim ms As MemoryStream = New MemoryStream(thumb.GetBlobValue())
        src.BeginInit()
        src.StreamSource = ms
        src.EndInit()
        PictureBox1.Source = src
    End If
    stream.Close()

End Sub


来源:https://stackoverflow.com/questions/18442901/display-metadata-thumbnail-of-jpeg-in-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!