Site Wide Message: (current site time 9/10/2010 2:20:42 AM EDT)
- We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
- Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
If so, then click here to give your feedback. |
'Ask the .Net Pros Discussion Forum
You are in:
Discussion Home | other
| Read and Write Meta/IPTC data in jpeg files
Read and Write Meta/IPTC data in jpeg files
|
Message: |
I have wriiten the following code in vb.net.I can display the jpg IPTC data but cannot write/set IPTC data.....Please help
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, ts As String, W As Integer, H As Integer Dim encoding As System.Text.Encoding = _ System.Text.Encoding.UTF8 Dim OpenFileDialog1 As New OpenFileDialog() OpenFileDialog1.Filter = "Image Files(*.JPG;*.BMP;*.GIF;*.tif;*.png)|*.JPG;*.BMP;*.GIF;*.TIF;*.PNG" OpenFileDialog1.ShowReadOnly = False If Not OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then 'Load image into the autosizing picture box 'The form is autoscrolling so it will fit nicely PictureBox1.Image = _ Image.FromFile(OpenFileDialog1.FileName) Dim MyInfo As Image = _ New Bitmap(OpenFileDialog1.FileName)
Dim AProp As Imaging.PropertyItem 'Scan through all the property items (if any) in image 'and put them into the string s For Each AProp In MyInfo.PropertyItems Try 'Some cameras are non-standard; hence TryCatch 'There are a zillion codes. I've included common ones. 'See the EXIF specification for all the others. Select Case AProp.Id.ToString("x") Case "9c9b" 'Title ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine
Me.TextBox1.Text = ts Dim Str2 = "This is a test to save data" Dim oEncoder As New System.Text.ASCIIEncoding() Dim bytes As Byte() = oEncoder.GetBytes(Str2) 'Me.TextBox1.Text = AProp.Id AProp.Value = bytes MyInfo.SetPropertyItem(AProp) '40091
End If Case "9c9c" ' comments ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine Me.TextBox2.Text = ts End If
Case "9c9d" ' Author ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine Me.TextBox3.Text = ts End If Case "9c9f" ' Subject ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine Me.TextBox4.Text = ts End If Case "9c9e" ' Keywords ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine Me.TextBox5.Text = ts End If Case "10e" 'Image name ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then s &= "Image" & vbTab & ts & vbNewLine End If Case "110" 'Model If Trim(encoding.GetString(AProp.Value)) <> _ "" Then s &= "Model" & vbTab & encoding.GetString(AProp.Value) & vbNewLine End If Case "112" 'Orientation Select Case BitConverter.ToInt16(AProp.Value, 0) Case 1 ts = "normal" Case 2 ts = "flip horizontal" Case 3 ts = "rotate 180" Case 4 ts = "flip vertical" 'cases 5-9 similar; see source Case Else ts = "unknown" End Select s &= "Orient." & vbTab & ts & vbNewLine Case "9003" 'Date If Trim(encoding.GetString(AProp.Value)) <> _ "" Then s &= "Date" & vbTab & encoding.GetString(AProp.Value) & vbNewLine End If Case "a002" 'Width-use later as 1st item on output W = BitConverter.ToInt32(AProp.Value, 0) Case "a003" 'Height H = BitConverter.ToInt32(AProp.Value, 0)
Case "8827" 'ISO Equivalent s &= "ISO Equ." & vbTab & (BitConverter.ToInt16(AProp.Value, 0)).ToString & vbNewLine Case "8822" 'Shutter control Select Case BitConverter.ToInt16(AProp.Value, 0) Case 1 ts = "manual control" Case 2 ts = "program normal" Case 3 ts = "aperture priority" 'cases 4-8 similar; see source Case Else ts = "unknown" End Select s &= "Program" & vbTab & ts & vbNewLine
Case "9207" 'Metering Select Case BitConverter.ToInt16(AProp.Value, 0) Case 1 ts = "average" Case 2 ts = "center weighted average" Case 3 ts = "spot" 'cases 4,5,6 similar; see source Case Else ts = "unknown" End Select s &= "Meter" & vbTab & ts & vbNewLine Case "9208" 'White balance Select Case BitConverter.ToInt16(AProp.Value, 0) Case 1 ts = "daylight" Case 2 ts = "fluorescent" Case 3 ts = "tungsten" Case 10 ts = "flash" 'six additional Cases omitted; see source Case Else ts = "unknown" End Select s &= "Light" & vbTab & ts & vbNewLine Case "9209" 'Flash Select Case BitConverter.ToInt16(AProp.Value, 0) Case 1 ts = "flash fired" Case 5 ts = "flash fired but no strobe return" Case 7 ts = "flash fired, strobe return detected" Case Else ts = "no flash" End Select
End Select Catch End Try Next s = Replace(s, Chr(0), "") If s = "" Then s = "No EXIF information with this image" Else If W * H <> 0 Then s = "Image" & vbTab & _ W.ToString & "x" & H.ToString & vbCrLf & s End If 'After all that hard work, show info as a message box 'In "real world" you'd be more sophisticated :-) 'MsgBox(s) Me.TextBox6.Text = OpenFileDialog1.FileName End If End Sub Private Function Rational(ByVal MyProp As Imaging.PropertyItem) _ As Double Rational = BitConverter.ToInt32(MyProp.Value, 0) _ / BitConverter.ToInt32(MyProp.Value, 4) End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim MyInfo As Image = New Bitmap(Me.TextBox6.Text) Dim TS, S, Q Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 Dim str1 As Object
str1 = Me.TextBox1.Text Dim oEncoder As New System.Text.ASCIIEncoding() Dim bytes As Byte() = oEncoder.GetBytes(str1) 'S = Do1
Dim AProp As Imaging.PropertyItem For Each AProp In MyInfo.PropertyItems Select Case AProp.Id.ToString("x") Case "9c9b" 'Title ts = Trim(Replace(encoding.GetString(AProp.Value), Chr(0), "")) If ts <> "" Then S &= "Updating the title section from '" & TS & "'" & vbNewLine & " to '" & Me.TextBox1.Text & "'" 'Me.TextBox1.Text = ts 'MsgBox(S)
'AProp.Id = 100 'AProp.Value = encoding2.GetBytes(str1) AProp.Value = bytes
MyInfo.SetPropertyItem(AProp)
'40091
End If End Select Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.PictureBox1.Image = Nothing Me.TextBox1.Text = "" Me.TextBox2.Text = "" Me.TextBox3.Text = "" Me.TextBox4.Text = "" Me.TextBox5.Text = "" Me.TextBox6.Text = "" End Sub End Class
|
To reply, please login using the link on the upper left hand corner of the screen.
|
|