How to Add 'Comments' to a JPEG File Using C#

后端 未结 6 1302
眼角桃花
眼角桃花 2020-11-27 17:59

Within the property window of a JPEG image, there is a tab called \'Summary\'. Within this tab, there is a field called \'Comments\' I would like to write some c# code which

6条回答
  •  渐次进展
    2020-11-27 18:28

    A variant on Peter Kistler's solution to set Title, Subject and Comment. I found I had to create items as Unicode byte array (type 1) and the IDs for Title, Subject and Comment are the same as for EXIF XPTitle, XPSubject and XP Comment. sFileOut can be the same as sFile.

    public static void SetWindowsTags2(string sFile, string sFileOut, string Title = "", string Subject = "", string Comment = "", bool bShowError = false)
        {
            try
            {
    
                // Retrieve the Image
                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(sFile);
    
                // Get the list of existing PropertyItems. i.e. the metadata
                PropertyItem[] properties = originalImage.PropertyItems;
    
                /*foreach (PropertyItem propItem in properties)
                {
                    string sTag = System.Text.Encoding.UTF8.GetString(propItem.Value);
                    string sItem = sTag.Replace("\0", string.Empty);
    
                    Debug.Print(propItem.Id.ToString() + ", " +  propItem.Type + ", " + sItem);
                }*/
    
                // Create a bitmap image to assign attributes and do whatever else..
                Bitmap bmpImage = new Bitmap((Bitmap)originalImage);
    
                // Don't need this anymore
                originalImage.Dispose();
    
                // Get / setup a PropertyItem
                PropertyItem item = properties[0]; // We have to copy an existing one since no constructor exists
    
                var itemData = System.Text.Encoding.Unicode.GetBytes(Title);
                itemData[itemData.Length - 1] = 0;// Strings must be null terminated or they will run together
                item.Type = 1; //Unicode Byte Array
                item.Id = 40091; // Title ID
                item.Len = itemData.Length; // Number of items in the byte array
                item.Value = itemData; // The byte array
                bmpImage.SetPropertyItem(item); // Assign / add to the bitmap
    
    
                itemData = System.Text.Encoding.Unicode.GetBytes(Subject);
                itemData[itemData.Length - 1] = 0; // Strings must be null terminated or they will run together
                item.Type = 1; //Unicode Byte Array
                item.Id = 40095; // subject
                item.Len = itemData.Length;
                item.Value = itemData;
                bmpImage.SetPropertyItem(item); // Assign / add to the bitmap
    
    
                itemData = System.Text.Encoding.Unicode.GetBytes(Comment);
                itemData[itemData.Length - 1] = 0; // Strings must be null terminated or they will run together
                item.Type = 1; ////Unicode Byte Array
                item.Id = 40092; // comment
                item.Len = itemData.Length;
                item.Value = itemData;
                bmpImage.SetPropertyItem(item); // Assign / add to the bitmap
    
    
                // Save the image
                bmpImage.Save(sFileOut, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                //Clean up
                bmpImage.Dispose();
    
            }
            catch (Exception Ex)
            {
    
            }
    
        }
    

提交回复
热议问题