0

I'm using VB.NET. I am able to load the pics from a folder into a flowlayoutpanel. And then load the clicked picture into a separate picturebox and display the picture's filepath in a label.

Now I want to be able to add rating and description to each of the image in the flowlayoutpanel and save it to a text file in the folder from which the pictures have been loaded. The app should load be able to load the rating and description on the next launch or when the selected image is changed. How do I accomplish this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leeuwenhok
  • 13
  • 2
  • 7

1 Answers1

0

You should probably look at accessing the metadata of the pic. This way the info you want is carried with the pic. This is contained in the PropertyItems Class, which is a property of the Image class

Here's a link to an answered question about adding a comment to a jpg. Hope this helps.

Here's an untested conversion of that code in VB.net. You'll probably have to add a reference or 2 and import a couple of namespaces, but syntactically this is correct as near as I can tell.

Public Function SetImageComment(input As Image, comment As String) As Image
    Using memStream As New IO.MemoryStream()
        input.Save(memStream, Imaging.ImageFormat.Jpeg)
        memStream.Position = 0
        Dim decoder As New JpegBitmapDecoder(memStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)
        Dim metadata As BitmapMetadata
        If decoder.Metadata Is Nothing Then
            metadata = New BitmapMetadata("jpg")
        Else
            metadata = decoder.Metadata
        End If
        metadata.Comment = comment
        Dim bitmapFrame = decoder.Frames(0)
        Dim encoder As BitmapEncoder = New JpegBitmapEncoder()
        encoder.Frames.Add(bitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts))
        Dim imageStream As New IO.MemoryStream
        encoder.Save(imageStream)
        imageStream.Position = 0
        input.Dispose()
        input = Nothing
        Return Image.FromStream(imageStream)
    End Using
End Function
Community
  • 1
  • 1
tinstaafl
  • 6,908
  • 2
  • 15
  • 22