Reputation: 11
I'm building an C# console-app that modifies image metadata (title, comments, tags) with AI-generated text to improve searchability in file folders. However, I recently ran into a problem. Is it not possible to change the metadata of a PNG with an alpha channel?
Whenever I try to edit the metadata of a transparent PNG, the background turns white. And if the transparency remains intact, the metadata doesn’t change at all. At this point, I’m not even sure if it’s possible.
Here's how I'm saving the Image:
var originalBytes = await File.ReadAllBytesAsync(imagePath);
using var originalMs = new MemoryStream(originalBytes);
using var originalImage = new Bitmap(originalMs);
using var resizedImage = ResizeImage(originalImage, 400, 400);
using var msResized = new MemoryStream();
resizedImage.Save(msResized, ImageFormat.Jpeg);
var b64 = Convert.ToBase64String(msResized.ToArray());
Before I send the data to the AI im resizing the image so it's smaller (ResizeImage):
var destRect = new Rectangle(0, 0, width, height);
var destImage = Image.IsAlphaPixelFormat(image.PixelFormat)
? new Bitmap(width, height, PixelFormat.Format32bppArgb)
: new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using var graphics = Graphics.FromImage(destImage);
if (Image.IsAlphaPixelFormat(image.PixelFormat))
{
graphics.Clear(Color.Transparent);
}
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
using var wrapMode = new ImageAttributes();
wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
return destImage;
I'm using System.Drawing.Imaging for the Images and this Logic for changing the meta data:
// Get data from AI response
var categories = responseContent["Category"]?.ToString() ?? "Unknown";
var description = responseContent["Description"]?.ToString() ?? "No description available";
var newTitleProperty = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
newTitleProperty.Id = 0x010E;
newTitleProperty.Type = 2;
newTitleProperty.Value = Encoding.UTF8.GetBytes(categories + "\0");
newTitleProperty.Len = newTitleProperty.Value.Length;
originalImage.SetPropertyItem(newTitleProperty);
// Create and set a new Comment property item.
var newCommentProperty = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
newCommentProperty.Id = 0x9286;
newCommentProperty.Type = 2;
newCommentProperty.Value = Encoding.UTF8.GetBytes(description + "\0");
newCommentProperty.Len = newCommentProperty.Value.Length;
originalImage.SetPropertyItem(newCommentProperty)
I searched almost everywhere on the internet and couldn't find an answer to my question.
Upvotes: 1
Views: 30