I'm tying to set the DateTaken parameter with C#, because I've got a lot of photos without this date.
I only found this comment changing-datetaken-of-a-photo
In this toppic they are changing it and not creating it.
If use this function, but DataTakenProperty1 or DataTakenProperty2 is null and can not be set.
private static void SetDateTaken(string path, DateTime NEWdate)
{
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
Encoding _Encoding = Encoding.UTF8;
var DataTakenPropert = propItems.SetValue(NEWdate.ToString("yyyy:MM:dd HH:mm:ss"), ??How do i know the index??);
theImage.SetPropertyItem(DataTakenProperty);
string new_path = Path.GetDirectoryName(path) + "\\_" + Path.GetFileName(path);
theImage.Save(new_path);
theImage.Dispose();
}
Thanks for your help
You can acheive this using a little trick, since you can't initiate PropertyItem when its null.
It is difficult to set property items, because the PropertyItem class
has no public constructors. One way to work around this restriction is
to obtain a PropertyItem by retrieving the PropertyItems property
value or calling the GetPropertyItem method of an Image that already
has property items. Then you can set the fields of the PropertyItem
and pass it to SetPropertyItem.
private static void SetDateTaken(string path, string samplePath, DateTime NEWdate)
{
Encoding _Encoding = Encoding.UTF8;
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
var DataTakenProperty1 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
var DataTakenProperty2 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
//// this is where you do the hack
if (DataTakenProperty1 == null)
{
Image sampleImage = new Bitmap(samplePath);
PropertyItem fakePropertyItem1 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
fakePropertyItem1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
fakePropertyItem1.Len = fakePropertyItem1.Value.Length;
theImage.SetPropertyItem(fakePropertyItem1);
PropertyItem fakePropertyItem2 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
fakePropertyItem2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
fakePropertyItem2.Len = fakePropertyItem2.Value.Length;
theImage.SetPropertyItem(fakePropertyItem2);
}
else
{
DataTakenProperty1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
DataTakenProperty2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '\0');
theImage.SetPropertyItem(DataTakenProperty1);
theImage.SetPropertyItem(DataTakenProperty2);
}
theImage.Save(newPath);
theImage.Dispose();
}
List of PropertyIds
Related
It would appear that something has changed in recent versions of iOS as it relates to saving GPS data in photos and subsequent to a bug in the Xam.Plugin.Media library, I am attempting to figure out how to take a photo and save it to the Photo Gallery with intact GPS data using iOS 13.4.1 and Xamarin.iOS 13.16.0.13. I have looked at the internals of Xam.Plugin.Media and using this as a starting point, tried to cobble something together on the hunch that because Xam.Plugin.Media uses ALAssetsLibrary to save to the Photo Gallery, perhaps this was why the GPS data is not being saved with the photo.
I am currently at the point where I had thought I would be able to take the photo, merge GPS data into the file and save the JPG output to a temporary location in the app folder (i.e. I haven't even gotten to saving the photo to the gallery yet). But when I examine this temp file using either the Preview app on MacOS or Photoshop to view the GPS metadata, the data is not there.
My handler for UIImagePickerController.FinishedPickingMedia is this:
private void OnFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
try
{
NSDictionary info = e.Info;
NSDictionary meta = null;
UIImage image = null;
string savedImagePath = null;
if (_imagePicker.SourceType == UIImagePickerControllerSourceType.Camera)
{
meta = (NSDictionary)info[UIImagePickerController.MediaMetadata];
image = (UIImage)info[UIImagePickerController.OriginalImage];
if (meta != null && meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
{
var newMeta = new NSMutableDictionary();
newMeta.SetValuesForKeysWithDictionary(meta);
var newTiffDict = new NSMutableDictionary();
newTiffDict.SetValuesForKeysWithDictionary(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
newTiffDict.SetValueForKey(meta[ImageIO.CGImageProperties.Orientation], ImageIO.CGImageProperties.TIFFOrientation);
newMeta[ImageIO.CGImageProperties.TIFFDictionary] = newTiffDict;
meta = newMeta;
}
if (_locationPermissionGranted)
{
meta = SetGpsLocation(meta);
}
savedImagePath = SaveImageWithMetadata(image, meta);
}
string aPath = null;
if (_imagePicker.SourceType != UIImagePickerControllerSourceType.Camera)
{
// Try to get the album path's URL.
var url = (NSUrl)info[UIImagePickerController.ReferenceUrl];
aPath = url?.AbsoluteString;
}
}
catch (Exception ex)
{
Console.WriteLine($"Unable to get metadata: {ex}");
}
}
Which calls:
private NSDictionary SetGpsLocation(NSDictionary meta)
{
var newMeta = new NSMutableDictionary();
newMeta.SetValuesForKeysWithDictionary(meta);
var newGpsDict = new NSMutableDictionary();
newGpsDict.SetValueForKey(new NSNumber(Math.Abs(_lat)), ImageIO.CGImageProperties.GPSLatitude);
newGpsDict.SetValueForKey(new NSString(_lat > 0 ? "N" : "S"), ImageIO.CGImageProperties.GPSLatitudeRef);
newGpsDict.SetValueForKey(new NSNumber(Math.Abs(_long)), ImageIO.CGImageProperties.GPSLongitude);
newGpsDict.SetValueForKey(new NSString(_long > 0 ? "E" : "W"), ImageIO.CGImageProperties.GPSLongitudeRef);
newGpsDict.SetValueForKey(new NSNumber(_altitude), ImageIO.CGImageProperties.GPSAltitude);
newGpsDict.SetValueForKey(new NSNumber(0), ImageIO.CGImageProperties.GPSAltitudeRef);
newGpsDict.SetValueForKey(new NSNumber(_speed), ImageIO.CGImageProperties.GPSSpeed);
newGpsDict.SetValueForKey(new NSString("K"), ImageIO.CGImageProperties.GPSSpeedRef);
newGpsDict.SetValueForKey(new NSNumber(_direction), ImageIO.CGImageProperties.GPSImgDirection);
newGpsDict.SetValueForKey(new NSString("T"), ImageIO.CGImageProperties.GPSImgDirectionRef);
newGpsDict.SetValueForKey(new NSString(_timestamp.ToString("hh:mm:ss")), ImageIO.CGImageProperties.GPSTimeStamp);
newGpsDict.SetValueForKey(new NSString(_timestamp.ToString("yyyy:MM:dd")), ImageIO.CGImageProperties.GPSDateStamp);
newMeta[ImageIO.CGImageProperties.GPSDictionary] = newGpsDict;
return newMeta;
}
private string SaveImageWithMetadata(UIImage image, NSDictionary meta)
{
string outputPath = null;
try
{
var finalQuality = 1.0f;
var imageData = image.AsJPEG(finalQuality);
// Continue to move down quality, rare instances.
while (imageData == null && finalQuality > 0)
{
finalQuality -= 0.05f;
imageData = image.AsJPEG(finalQuality);
}
if (imageData == null)
{
throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or " +
"lower quality level");
}
var dataProvider = new CGDataProvider(imageData);
var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default);
var imageWithExif = new NSMutableData();
var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1);
var cgImageMetadata = new CGMutableImageMetadata();
var destinationOptions = new CGImageDestinationOptions();
if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
{
destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] =
meta[ImageIO.CGImageProperties.Orientation];
}
if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth))
{
destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] =
meta[ImageIO.CGImageProperties.DPIWidth];
}
if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight))
{
destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] =
meta[ImageIO.CGImageProperties.DPIHeight];
}
if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary))
{
destinationOptions.ExifDictionary =
new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary);
}
if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary))
{
destinationOptions.TiffDictionary =
new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
}
if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary))
{
destinationOptions.GpsDictionary =
new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary);
}
if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary))
{
destinationOptions.JfifDictionary =
new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary);
}
if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary))
{
destinationOptions.IptcDictionary =
new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary);
}
destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions);
var success = destination.Close();
if (success)
{
outputPath = GetOutputPath();
imageWithExif.Save(outputPath, true);
}
}
catch (Exception e)
{
Console.WriteLine($"Unable to save image with metadata: {e}");
}
return outputPath;
}
private static string GetOutputPath()
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "temp");
Directory.CreateDirectory(path);
var timestamp = DateTime.UtcNow.ToString("yyyMMdd_HHmmss", CultureInfo.InvariantCulture);
var name = "IMG_" + timestamp + ".jpg";
return Path.Combine(path, GetUniquePath(path, name));
}
private static string GetUniquePath(string path, string name)
{
var ext = Path.GetExtension(name);
name = Path.GetFileNameWithoutExtension(name);
var fullName = name + ext;
var i = 1;
while (File.Exists(Path.Combine(path, fullName)))
{
fullName = name + "_" + (i++) + ext;
}
return Path.Combine(path, fullName);
}
The file is saved successfully, but without the expected GPS metadata. Which leads me to believe that the problem may be to do with the way I am saving the temporary photo or saving the GPS metadata to it in either SaveImageWithMetadata or SetGpsLocation.
If anyone can provide some info on what actually works now with iOS 13.4.1 and saving GPS data to photos, I would be very appreciative.
I am trying to create a xamarin forms application, I am using dependency service to pick multiple files from device. I have written the following code for getting the actual file from Android.Net.Uri:
private string GetPathToAudioFile(global::Android.Net.Uri uri)
{
string doc_id = "";
using (var c1 = ContentResolver.Query(uri, null, null, null, null))
{
c1.MoveToFirst();
String document_id = c1.GetString(0);
doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
}
string path = null;
// The projection contains the columns we want to return in our query.
string audioSelection = MediaStore.Audio.Media.InterfaceConsts.Id + " =? ";
using (var cursor = ManagedQuery(MediaStore.Audio.Media.ExternalContentUri, null, audioSelection, new string[] { doc_id }, null))
{
if (cursor == null) return path;
var columnIndex = cursor.GetColumnIndexOrThrow(MediaStore.Audio.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
return path;
}
But whenever, I try to do this I get the following exception. Cursor Exception
I am new to Xamarin and I don't know how to solve this problem. Please help me with this. Thanks in advance.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
In the below code i have a string value it has a path.I want to place the String value inside the static method But i get Object reference not set to an instance of an object.if i write path in that code it works but not string value which has path .pls help me to solve the issue.
var projectname = name.ProjectName;
var batchname = name.BatchName;
var imagename = name.ImageName;
string concatenatedStr = "/"+ projectname + "/" + batchname + "/Input/" + imagename;
[WebMethod]
public static string buttonclickImage(string pageNo)
{
int iPageNo = 0;
if (pageNo != string.Empty && pageNo != "undefined")
iPageNo = Int32.Parse(pageNo);
FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
FileDto file = fileTranz.GetTifftoJPEG("concatenatedStr", iPageNo, "gmasdll");
var fileData = Convert.ToBase64String(file.Content);//throws error
return fileData;
}
it means that either file is null or file.Content is null. You can avoid the exception by
if(file!=null && file.Content!=null)
{
//your remaining code
}
ideally though you should first check the reason why it is null
Edit:
From your comments i infer that you want to pass your varible. Either make your string static, or make your method not static or pass the string to your method
[WebMethod]
public static string buttonclickImage(string pageNo)
{
int iPageNo = 0;
if (pageNo != string.Empty && pageNo != "undefined")
iPageNo = Int32.Parse(pageNo);
FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
//note the change here. no double quotes.
FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll");
var fileData = Convert.ToBase64String(file.Content);//throws error
return fileData;
}
You're not passing in the value stored in the concatenatedStr variable... you're passing in the literal string "concatenatedStr".
Change this:
FileDto file = fileTranz.GetTifftoJPEG("concatenatedStr", iPageNo, "gmasdll");
To this:
FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll");
You'll also need to make your variables static, since your method is static. Or leave the variables as they are, and make the method non-static, if that's an option.
I'm a little confused about where those variables are located though. They appear to be class-level in scope, but then you wouldn't be able to use var in that location.
I guess you could also modify your method to accept an additional parameter, and then pass in the value from wherever you're calling this.
public static string buttonclickImage(string pageNo, string concatenatedStr)
{
...
This works
[WebMethod]
public static string buttonclickImage(string pageNo)
{
var name = (name)HttpContext.Current.Session["Projectname"];
var projectname = name.ProjectName;
var batchname = name.BatchName;
var imagename = name.ImageName;
string concatenatedStr = "/" + projectname + "/" + batchname + "/Input/" + imagename;
int iPageNo = 0;
if (pageNo != string.Empty && pageNo != "undefined")
iPageNo = Int32.Parse(pageNo);
FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll");
var fileData = Convert.ToBase64String(file.Content);
return fileData;
}
First of all construct value of "concatenatedStr" variable using String.Format.
For Eg:-
var projectname = name.ProjectName;
var batchname = name.BatchName;
var imagename = name.ImageName;
string concatenatedStr = string.Format("/{0}/{1}/Input/{2}", projectname, batchname, imagename);
Put debug point here and check what value "concatenatedStr" has.
If "concatenatedStr" is null then definitely you will get "Nullreference exception"....
So may be there is a problem with "concatenatedStr".....So deeply check concatenation variables too...
Hope this works......
I have develop a ASP.NET (C#) application to store the images and videos into Amazon S3. Images are being uploaded fine but when i try to upload videos it saves as an image format in Amazon S3.
Does anyone know what the issue is or how to I can upload videos?
private void Amzon(string imageName,string imgcontenttype,int imglength,byte[] fileData)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
// Create a signature for this operation
string strMySignature = S3Helper.GetSignature(mySecretAccessKeyId, "PutObjectInline", myTime);
// Create a new Access grant for anonymous users.
Grant myGrant = new Grant();
Grant[] myGrants = new Grant[1];
// Setup Access control, allow Read access to all
Group myGroup = new Group();
myGroup.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
myGrant.Grantee = myGroup;
myGrant.Permission = Permission.READ;
myGrants[0] = myGrant;
string key = imageName;
// Setup some metadata to indicate the content type
MetadataEntry myContentType = new MetadataEntry();
myContentType.Name = "ContentType";
myContentType.Value = imgcontenttype;
MetadataEntry[] myMetaData = new MetadataEntry[1];
myMetaData[0] = myContentType;
// Finally upload the object
PutObjectResult myResult = myS3.PutObjectInline(
bucketname,
key,
myMetaData,
fileData,
imglength,
myGrants,
StorageClass.STANDARD,
true,
myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
strMySignature, null
);
// Print out the results.
if (myResult != null)
{
cn.Open();
Url = "https://s3.amazonaws.com/" + bucketname + "/" + key;
string Query = "Insert into S3Image(ImageName,ImageUrl)Values('" + key + "','" + Url + "')";
SqlCommand cmd = new SqlCommand(Query, cn);
cmd.ExecuteNonQuery();
cn.Close();
//MyPrint("ETag: " + myResult.ETag);
MyPrint("<img src=https://s3.amazonaws.com/" + bucketname + "/" + key);
}
}
Thank you.
There's a lot of code to get up and running with Amazon's web service. I think this part of code is what you need, you might not be setting the right content type:
// Setup some metadata to indicate the content type
MetadataEntry myContentType = new MetadataEntry();
myContentType.Name = "ContentType";
myContentType.Value = FileUpload1.PostedFile.ContentType;
Here's the full code: Enjoy.
`private const string accessKeyId = "REMOVED";
private const string secretAccessKey = "REMOVED";
private static DateTime GetTimeStamp(DateTime myTime)
{
DateTime myUniversalTime = myTime.ToUniversalTime();
DateTime myNewTime = new DateTime(myUniversalTime.Year,
myUniversalTime.Month, myUniversalTime.Day,
myUniversalTime.Hour, myUniversalTime.Minute,
myUniversalTime.Second, myUniversalTime.Millisecond);
return myNewTime;
}
private static string GetSignature(string secretAccessKey, string strOperation, DateTime myTime)
{
Encoding myEncoding = new UTF8Encoding();
// Create the source string which is used to create the digest
string mySource = "AmazonS3" + strOperation + FormatTimeStamp(myTime);
// Create a new Cryptography class using the
// Secret Access Key as the key
HMACSHA1 myCrypto = new HMACSHA1(myEncoding.GetBytes(secretAccessKey));
// Convert the source string to an array of bytes
char[] mySourceArray = mySource.ToCharArray();
// Convert the source to a UTF8 encoded array of bytes
byte[] myUTF8Bytes = myEncoding.GetBytes(mySourceArray);
// Calculate the digest
byte[] strDigest = myCrypto.ComputeHash(myUTF8Bytes);
return Convert.ToBase64String(strDigest);
}
private static string FormatTimeStamp(DateTime myTime)
{
DateTime myUniversalTime = myTime.ToUniversalTime();
return myUniversalTime.ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z", System.Globalization.CultureInfo.InvariantCulture);
}
/// <summary>
/// Upload Images.
/// </summary>
/// <param name="a">Ex. FileUpload1.PostedFile.ContentType</param>
/// <param name="b">Ex. FileUpload1.PostedFile.FileName</param>
/// <param name="c">Ex. FileUpload1.FileBytes</param>
/// <param name="d">Ex. FileUpload1.FileBytes.Length</param>
/// <param name="id">The ID for this Product Group</param>
public void UploadImage_ProductGroup(string a, string b, byte[] c, long d, int id)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
// Create a signature for this operation
string strMySignature = GetSignature(
secretAccessKey,
"PutObjectInline",
myTime);
// Create a new Access grant for anonymous users.
Grant myGrant = new Grant();
Grant[] myGrants = new Grant[1];
// Setup Access control, allow Read access to all
Group myGroup = new Group();
myGroup.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
myGrant.Grantee = myGroup;
myGrant.Permission = Permission.READ;
myGrants[0] = myGrant;
// Setup some metadata to indicate the content type
MetadataEntry myContentType = new MetadataEntry();
myContentType.Name = "ContentType";
myContentType.Value = a;
MetadataEntry[] myMetaData = new MetadataEntry[1];
myMetaData[0] = myContentType;
//Format the file name to prepend thumbnail before the file extension.
/* int lastIndex = b.LastIndexOf('.');
string fileName = b.Remove(lastIndex);
string ext = b.Remove(0, lastIndex);
string thumbPath = string.Format("images/public/{0}thumb{1}",fileName,ext);
//Resize the thumbnail
*/
// Finally upload the object
PutObjectResult myResult = myS3.PutObjectInline(
"mywebsite",
"images/public/" + b,
myMetaData,
c,
d,
myGrants,
StorageClass.STANDARD,
true,
accessKeyId,
GetTimeStamp(myTime),
true,
strMySignature, null
);`
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 will add a given string to this field e.g "This is a photo".
Does some kind soul out there know how to do this?
Many thanks.
Based on other answers I wrote the following class which allows various metadata manipulations. You use it like this:
var jpeg = new JpegMetadataAdapter(pathToJpeg);
jpeg.Metadata.Comment = "Some comments";
jpeg.Metadata.Title = "A title";
jpeg.Save(); // Saves the jpeg in-place
jpeg.SaveAs(someNewPath); // Saves with a new path
The differences between my solution and the others are not large. Principally I have refactored this to be cleaner. I also use the higher level properties of BitmapMetadata, rather than the SetQuery method.
Here is the full code, which is licensed under the MIT licence. You will need to add references to PresentationCore, WindowsBase, and System.Xaml.
public class JpegMetadataAdapter
{
private readonly string path;
private BitmapFrame frame;
public readonly BitmapMetadata Metadata;
public JpegMetadataAdapter(string path)
{
this.path = path;
frame = getBitmapFrame(path);
Metadata = (BitmapMetadata)frame.Metadata.Clone();
}
public void Save()
{
SaveAs(path);
}
public void SaveAs(string path)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(frame, frame.Thumbnail, Metadata, frame.ColorContexts));
using (Stream stream = File.Open(path, FileMode.Create, FileAccess.ReadWrite))
{
encoder.Save(stream);
}
}
private BitmapFrame getBitmapFrame(string path)
{
BitmapDecoder decoder = null;
using (Stream stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
return decoder.Frames[0];
}
}
The following code solves my problem and adds comments to a given JPEG image:
public void addImageComment(string imageFlePath, string comments)
{
string jpegDirectory = Path.GetDirectoryName(imageFlePath);
string jpegFileName = Path.GetFileNameWithoutExtension(imageFlePath);
BitmapDecoder decoder = null;
BitmapFrame bitmapFrame = null;
BitmapMetadata metadata = null;
FileInfo originalImage = new FileInfo(imageFlePath);
if (File.Exists(imageFlePath))
{
// load the jpg file with a JpegBitmapDecoder
using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
bitmapFrame = decoder.Frames[0];
metadata = (BitmapMetadata)bitmapFrame.Metadata;
if (bitmapFrame != null)
{
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
if (metaData != null)
{
// modify the metadata
metaData.SetQuery("/app1/ifd/exif:{uint=40092}", comments);
// get an encoder to create a new jpg file with the new metadata.
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
//string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg");
// Delete the original
originalImage.Delete();
// Save the new image
using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
}
}
}
This is essentially a lightly modified version of the code found under the link which Konamiman kindly supplied.
Please be aware that to make this work you will need to add .NET references to PresentationCore and WindowsBase. If using Visual Studio 2008, this can be achieved via the following:
Right click on your project in the Solution Explorer
From the drop down list, select Add 'Reference...'
From the new box which opens, select the '.NET' tab
Scroll to the two references mentioned above and on each, click ok
Many thanks to both danbystrom and Konamiman for your help in this matter. I really appreciate the quick response.
The easy part:
Add this property item:
var data = System.Text.Encoding.UTF8.GetBytes( "Some comments" );
PropertyItem pi;
*** create an empty PropertyItem here
pi.Type = 2;
pi.Id = 37510;
pi.Len = data.Length;
pi.Value = data;
To the Image's PropertItems collection.
The somewhat more cumbersome part:
How do you create a new PropertyItem, since it has no public constructor?
The common "trick" is to have an empty image lying around from which you can steal a PropertyItem. sigh
Thanks to the answers here, I've coded a solution to set a comment using memory only:
public static Image SetImageComment(Image image, string comment) {
using (var memStream = new MemoryStream()) {
image.Save(memStream, ImageFormat.Jpeg);
memStream.Position = 0;
var decoder = new JpegBitmapDecoder(memStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapMetadata metadata;
if (decoder.Metadata == null) {
metadata = new BitmapMetadata("jpg");
} else {
metadata = decoder.Metadata;
}
metadata.Comment = comment;
var bitmapFrame = decoder.Frames[0];
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts));
var imageStream = new MemoryStream();
encoder.Save(imageStream);
imageStream.Position = 0;
image.Dispose();
image = null;
return Image.FromStream(imageStream);
}
}
Don't forget to dispose the image that is returned by this method. (For example after saving the image to a file)
Thanks to the previous tips I was able to put the following together. I've tested it and it seems to work. One of the biggest stumbling blocks was determining the Id needed for the field you want to assign.
string fileName = "c:/SomeImage.jpg";
// Retrieve the Image
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fileName);
// Get the list of existing PropertyItems. i.e. the metadata
PropertyItem[] properties = originalImage.PropertyItems;
// 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
// This will assign "Joe Doe" to the "Authors" metadata field
string sTmp = "Joe DoeX"; // The X will be replaced with a null. String must be null terminated.
var itemData = System.Text.Encoding.UTF8.GetBytes(sTmp);
itemData[itemData.Length - 1] = 0;// Strings must be null terminated or they will run together
item.Type = 2; //String (ASCII)
item.Id = 315; // Author(s), 315 is mapped to the "Authors" field
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
// This will assign "MyApplication" to the "Program Name" field
sTmp = "MyApplicationX";
itemData = System.Text.Encoding.UTF8.GetBytes(sTmp);
itemData[itemData.Length - 1] = 0; // Strings must be null terminated or they will run together
item.Type = 2; //String (ASCII)
item.Id = 305; // Program Name, 305 is mapped to the "Program Name" field
item.Len = itemData.Length;
item.Value = itemData;
bmpImage.SetPropertyItem(item);
// Save the image
bmpImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//Clean up
bmpImage.Dispose();
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)
{
}
}