Hallo, hatte leider zwei Tage keine Zeit und dann finde ich folgendes im Forum
Spoiler anzeigen
Funktioniert im Testlauf. Denke ich werde es ein wenig anpassen und dann in mein Projekt so übernehmen.
VB.NET-Quellcode
- Private Sub MetaDataEditor()
- If Me.Halt Then Exit Sub
- If IO.File.Exists(Me.TempDatei) Then IO.File.Delete(Me.TempDatei)
- Me.Quelldatei = Me.QuellPfad & Me.DGV_Quelldaten.CurrentRow.Cells("Dateiname").Value.ToString
- Dim createOptions As BitmapCreateOptions = BitmapCreateOptions.PreservePixelFormat Or BitmapCreateOptions.IgnoreColorProfile
- Dim paddingAmount As UInteger = 2048
- ' 2Kb padding for this example, but really this can be any value.
- ' Our recommendation is to keep this between 1Kb and 5Kb as most metadata updates are not large.
- ' High level overview:
- ' 1. Perform a lossles transcode on the JPEG
- ' 2. Add appropriate padding
- ' 3. Optionally add whatever metadata we need to add initially
- ' 4. Save the file
- ' 5. For sanity, we verify that we really did a lossless transcode
- ' 6. Open the new file and add metadata in-place
- Using originalFile As Stream = File.Open(Me.Quelldatei, FileMode.Open, FileAccess.Read)
- ' Notice the BitmapCreateOptions and BitmapCacheOption. Using these options in the manner here
- ' will inform the JPEG decoder and encoder that we're doing a lossless transcode operation. If the
- ' encoder is anything but a JPEG encoder, then this no longer is a lossless operation.
- ' ( Details: Basically BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile
- ' tell the decoder to use the original image bits and BitmapCacheOption.None tells the decoder to wait
- ' with decoding. So, at the time of encoding the JPEG encoder understands that the input was a JPEG
- ' and just copies over the image bits without decompressing and recompressing them. Hence, this is a
- ' lossless operation. )
- Dim original As BitmapDecoder = BitmapDecoder.Create(originalFile, createOptions, BitmapCacheOption.None)
- If Not original.CodecInfo.FileExtensions.Contains("jpg") Then
- MsgBox("The file you passed in is not a JPEG.")
- 'Return
- End If
- Dim output As New JpegBitmapEncoder()
- ' If you're just interested in doing a lossless transcode without adding metadata, just do this:
- 'output.Frames = original.Frames;
- ' If you want to add metadata to the image using the InPlaceBitmapMetadataWriter, first add padding:
- If original.Frames(0) IsNot Nothing AndAlso original.Frames(0).Metadata IsNot Nothing Then
- ' Your gut feel may want you to do something like:
- ' output.Frames = original.Frames;
- ' BitmapMetadata metadata = output.Frames[0].Metadata as BitmapMetadata;
- ' if (metadata != null)
- ' {
- ' metadata.SetQuery("/app1/ifd/PaddingSchema:Padding", paddingAmount);
- ' }
- ' However, the BitmapMetadata object is frozen. So, you need to clone the BitmapMetadata and then
- ' set the padding on it. Lastly, you need to create a "new" frame with the updated metadata.
- Dim metadata As BitmapMetadata = TryCast(original.Frames(0).Metadata.Clone(), BitmapMetadata)
- ' Of the metadata handlers that we ship in WIC, padding can only exist in IFD, EXIF, and XMP.
- ' Third parties implementing their own metadata handler may wish to support IWICFastMetadataEncoder
- ' and hence support padding as well.
- metadata.SetQuery("/app1/ifd/PaddingSchema:Padding", paddingAmount)
- metadata.SetQuery("/app1/ifd/exif/PaddingSchema:Padding", paddingAmount)
- metadata.SetQuery("/xmp/PaddingSchema:Padding", paddingAmount)
- metadata.Title = "Das ist der Titel"
- metadata.SetQuery("System.Keywords", "Das ist eine Markierung")
- metadata.SetQuery("System.Author", "Ich bin der Autor von diesem Foto")
- metadata.Copyright = "Das ist das Copyrigh von dem Foto"
- metadata.Comment = "Der Kommentar"
- metadata.Rating = CInt("5")
- ' Create a new frame identical to the one from the original image, except the metadata will have padding.
- ' Essentially we want to keep this as close as possible to:
- ' output.Frames = original.Frames;
- output.Frames.Add(BitmapFrame.Create(original.Frames(0), original.Frames(0).Thumbnail, metadata, original.Frames(0).ColorContexts))
- End If
- Using outputFile As Stream = File.Open(Me.TempDatei, FileMode.Create, FileAccess.ReadWrite)
- output.Save(outputFile)
- End Using
- End Using
- ' For sanity, let's verify that the original and the output contain image bits that are the same.
- Using originalFile As Stream = File.Open(Me.Quelldatei, FileMode.Open, FileAccess.Read)
- Dim original As BitmapDecoder = BitmapDecoder.Create(originalFile, createOptions, BitmapCacheOption.None)
- Using savedFile As Stream = File.Open(Me.TempDatei, FileMode.Open, FileAccess.Read)
- Dim output As BitmapDecoder = BitmapDecoder.Create(savedFile, createOptions, BitmapCacheOption.None)
- End Using
- End Using
- IO.File.Delete(Me.Quelldatei)
- Rename(Me.TempDatei, Me.Quelldatei)
- End Sub
Funktioniert im Testlauf. Denke ich werde es ein wenig anpassen und dann in mein Projekt so übernehmen.
Gruß Markus