How do I rate an image?
In my project, I have to set the image rating value in any format (* .png, * .jpg, * .bmp, etc.) and return the value. I am trying to use PropertyItem
. he does not work.
Image im = Image.FromFile("D:\\2.jpg");
int intValue = 3;
byte[] intBytes = BitConverter.GetBytes(intValue);
if (BitConverter.IsLittleEndian)Array.Reverse(intBytes);
byte[] result = intBytes;
PropertyItem prop = im.GetPropertyItem(18246);
prop.Value = result;
im.SetPropertyItem(prop);
Does anyone do this, if yes then thanks?
source to share
To adjust the rating, you must set two values. Rating & Rating
- Exif.Image.Rating (18246)
- Exif.Image.RatingPercent (18249)
These two values ββcorrespond to each other. Therefore, the rating setting does not reflect the ratings in Windows. (This is actually very difficult because you are setting the value, you can read it, but nothing changes in Windows Explorer).
class Program
{
static void Main(string[] args)
{
//0,1,2,3,4,5
SetRating(0);
SetRating(1);
SetRating(2);
SetRating(3);
SetRating(4);
SetRating(5);
}
private static void SetRating(short ratingValue)
{
short ratingPercentageValue = 0;
switch (ratingValue)
{
case 0: ratingPercentageValue = ratingValue; break;
case 1: ratingPercentageValue = ratingValue; break;
default: ratingPercentageValue = (short)((ratingValue - 1) * 25); break;
}
string SelectedImage = @"d:\Trash\phototemp\IMG_1200.JPG";
using (var imageTemp = System.Drawing.Image.FromFile(SelectedImage))
{
var rating = imageTemp.PropertyItems.FirstOrDefault(x => x.Id == 18246);
var ratingPercentage = imageTemp.PropertyItems.FirstOrDefault(x => x.Id == 18249);
rating.Value = BitConverter.GetBytes(ratingValue);
rating.Len= rating.Value.Length;
ratingPercentage.Value = BitConverter.GetBytes(ratingPercentageValue);
ratingPercentage.Len = ratingPercentage.Value.Length;
imageTemp.SetPropertyItem(rating);
imageTemp.SetPropertyItem(ratingPercentage);
imageTemp.Save(SelectedImage + "new" + ratingValue +".jpg");
}
}
}
source to share
The solution is not optimal (and hacky), but the simplest way is:
1) Use an image on your hard drive. Any image. Just evaluate it manually for the image to have this property.
2) This image will act as your "dummy image", so you can load it to call getPropertyItem () on it to get the property.
3) When you have a PropertyItem, just change its value and use SetPropertyItem on your actual image.
string dummyFileName = @"C:\Users\<youruser>\Pictures\dummy.jpg";
string realFileName = @"C:\Users\<youruser>\Pictures\real.jpg";
string realFileNameOutput = @"C:\Users\<youruser\Pictures\real_rated.jpg";
Image dummyFile = Image.FromFile(dummyFileName);
var propertyItem = dummyFile.GetPropertyItem(18246);
Image realImage = Image.FromFile(realFileName);
realImage.SetPropertyItem(propertyItem);
realImage.Save(realFileNameOutput);
source to share