/
Stamp and Endorsements - ImageMagick

Stamp and Endorsements - ImageMagick

 

Link:

ImageMagick – Mastering Digital Image Alchemy

ImageManipulator.cs V 1.0

 

using System; using System.Drawing; using Amazon.S3.Model; using ImageMagick; namespace Coral.API { public class ImageManipulator { // Method to add text annotation to an image public void AddTextAnnotation(string inputFilePath, string outputFilePath, string annotationText, int x, int y) { using (MagickImage image = new MagickImage(inputFilePath)) { // Add text annotation at the specified position image.Annotate(annotationText, new MagickGeometry(x, y), Gravity.Center);// Save the modified image image.Write(outputFilePath); } } // Method to remove annotations from an image public void RemoveAnnotations(string inputFilePath, string outputFilePath) { using (MagickImage image = new MagickImage(inputFilePath)) { // Clear all annotations image.RemoveAttribute("annotate"); // Save the modified image image.Write(outputFilePath); } } public void AddAnnotationAtTop(string inputFilePath, string outputFilePath, string annotationText) { using (MagickImage image = new MagickImage(inputFilePath)) { // Calculate the size of the annotation int width = image.Width; int height = 100; // Height of the blank space for the annotation // Create a new image with the calculated dimensions using (MagickImage annotatedImage = new MagickImage(MagickColors.White, width, height)) { // Add the annotation text to the new image annotatedImage.Annotate(annotationText, Gravity.Center); // Composite the original image on top of the new image image.Composite(annotatedImage, Gravity.North, CompositeOperator.Over); // Save the modified image image.Write(outputFilePath); } } } // Method to add annotation with tags to an image public void AddAnnotationWithTags(string inputFilePath, string outputFilePath, string annotationText, int x, int y, string[] tags) { using (MagickImage image = new MagickImage(inputFilePath)) { // Create annotation text with tags string annotationWithTags = $"{annotationText}\\nTags: {string.Join(", ", tags)}"; // Annotate the image with the text at the specified position image.Annotate(annotationWithTags, new MagickGeometry(x, y), Gravity.Northwest); // Save the modified image image.Write(outputFilePath); } } //// Method to render HTML content onto an image using ImageMagick and HtmlRenderer.Core //public void RenderHtmlToImage(string htmlContent, string outputFilePath, int width, int height) //{ // // Render HTML content onto a bitmap // Bitmap bitmap = new Bitmap(width, height); // using (Graphics graphics = Graphics.FromImage(bitmap)) // { // HtmlRender.Render(graphics, htmlContent, new Rectangle(0, 0, width, height)); // } // // Convert bitmap to MagickImage // using (MagickImage image = new MagickImage(bitmap)) // { // // Optionally, you can further process the image (resize, annotate, etc.) before saving // // Save the image to the output file path // image.Write(outputFilePath); // } //} public void AddStamp() { var imageBackgroundColor = new MagickColor("White"); using (MagickImageCollection images = new MagickImageCollection()) { // Read the existing image file images.Read(@"C:\\test.tiff"); // Iterate through each image in the collection foreach (var image in images) { // Create a drawable for the annotation var drawable = new DrawableText(0, 10, "Line One\\nLine Two\\nLine Three"); var gravity = new DrawableGravity(Gravity.North); var font = new DrawableFont("Arial"); var size = new DrawableFontPointSize(50); var color = new DrawableFillColor(MagickColors.Black); // Annotate the image image.Annotate("Some annotation", Gravity.Center); // Draw the annotation on the image image.Draw(drawable, gravity, font, size, color); // Optionally, save the modified image to a new file image.Write(@"c:\\test2.tiff"); } } MagickImage img = new MagickImage(MagickColors.White, 80, 96); img.Settings.AntiAlias = false; img.Settings.StrokeAntiAlias = false; img.Settings.TextAntiAlias = false; img.SetCompression(CompressionMethod.Group4); img.Format = MagickFormat.Tiff; var drawables = new Drawables() .FillColor(MagickColors.Black) .Font("Arial") .FontPointSize(14); drawables.Text(0, 25, "dddDDDddDDWwwWW"); img.Draw(drawables); img.Write(@"c:\\test.tiff"); } // Method to modify TIFF tags public void ModifyTiffTags(string imagePath, TiffTag[] tags) { // Load the image using (MagickImage image = new MagickImage(imagePath)) { // Get or create the EXIF profile IptcProfile iptcProfile = (IptcProfile)(image.GetIptcProfile() ?? new IptcProfile()); // Add or update the specified tags foreach (var tag in tags) { iptcProfile.SetValue(tag.Tag , tag.Value); } // Update the image with the modified profile //image.AddProfile(iptcProfile); // Save the modified image image.Write(imagePath); } } } // Model for TIFF tag public class TiffTag { public ImageMagick.IptcTag Tag { get; set; } public string Value { get; set; } } }

 

Add label

Related content