Click here to Skip to main content
14,982,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am trying to create string metadata to be stored in a png file, depending on the value of a parameter called sc_status.

VB
Dim qualityParam As Object
Dim encoderParams As Object = New Imaging.EncoderParameters(1)
Dim ImgCodec As Imaging.ImageCodecInfo

ImgCodec = GetEncoderInfo("image/png")
qualityParam = New Imaging.EncoderParameter(Imaging.Encoder.ColorDepth, 32L)
encoderParams.Param(0) = qualityParam

'---
' img_src Image is created here
' file_name String is created here
'---

' Creating the PropertyItem
Dim propit As Imaging.PropertyItem = CType(System.Runtime.Serialization.FormatterServices.GetUninitializedObject(GetType(Imaging.PropertyItem)), Imaging.PropertyItem)

propit.Id = 270 '0x010E = Image description
propit.Type = 2

If sc_status = 3 Then
    propit.Value = System.Text.Encoding.UTF8.GetBytes("HQ")
ElseIf sc_status = 5 Then
    propit.Value = System.Text.Encoding.UTF8.GetBytes("LQ")
Else
    propit.Value = System.Text.Encoding.UTF8.GetBytes("UQ")
End If

' Storing the PropertyItem
img_src.SetPropertyItem(propit)

' Saving png
img_src.Save(file_name, ImgCodec, encoderParams)


When I have a look at what is stored in the png chunks, I expect to have in byte sequence (72,81,0) or (76,81,0) or (85,81,0), corresponding to the string "LQ","HQ","UQ" plus the vbNullChar which is automatically added at the end of the png chunk.

But for a reason I ignore, I sometimes have a longer byte sequence, e.g. (72,81,28,8,1,0) which gives after using System.Text.Encoding.UTF8.GetString the string HQ & ChrW(28) & vbBack & ChrW(1) & vbNullChar, or sometimes (72,81,22,8,1,0), or sometimes (72,81,19,8,1,0) or sometimes (72,81,23,8,1,0).

I don't understand why sometimes extra bytes are added in the metadata during the img_src.Save procedure. Any help is very welcome!

What I have tried:

I am a bit lost because 90% of the time, the metadata is correctly stored. I have not found what causes the extra bytes to be written. What am I doing wrong?
Posted
Updated 20-Apr-21 11:36am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900