/
Stamp and Endorsements - ImageMagick


Link:

https://imagemagick.org/index.php

ImageManipulator.cs V 1.0


1using System; 2using System.Drawing; 3using Amazon.S3.Model; 4using ImageMagick; 5 6namespace Coral.API 7{ 8 public class ImageManipulator 9 { 10 // Method to add text annotation to an image 11 public void AddTextAnnotation(string inputFilePath, string outputFilePath, string annotationText, int x, int y) 12 { 13 using (MagickImage image = new MagickImage(inputFilePath)) 14 { 15 // Add text annotation at the specified position 16 image.Annotate(annotationText, new MagickGeometry(x, y), Gravity.Center);// Save the modified image 17 image.Write(outputFilePath); 18 } 19 } 20 21 // Method to remove annotations from an image 22 public void RemoveAnnotations(string inputFilePath, string outputFilePath) 23 { 24 using (MagickImage image = new MagickImage(inputFilePath)) 25 { 26 // Clear all annotations 27 image.RemoveAttribute("annotate"); 28 29 // Save the modified image 30 image.Write(outputFilePath); 31 } 32 } 33 34 public void AddAnnotationAtTop(string inputFilePath, string outputFilePath, string annotationText) 35 { 36 using (MagickImage image = new MagickImage(inputFilePath)) 37 { 38 // Calculate the size of the annotation 39 int width = image.Width; 40 int height = 100; // Height of the blank space for the annotation 41 42 // Create a new image with the calculated dimensions 43 using (MagickImage annotatedImage = new MagickImage(MagickColors.White, width, height)) 44 { 45 // Add the annotation text to the new image 46 annotatedImage.Annotate(annotationText, Gravity.Center); 47 48 // Composite the original image on top of the new image 49 image.Composite(annotatedImage, Gravity.North, CompositeOperator.Over); 50 51 // Save the modified image 52 image.Write(outputFilePath); 53 } 54 } 55 } 56 57 // Method to add annotation with tags to an image 58 public void AddAnnotationWithTags(string inputFilePath, string outputFilePath, string annotationText, int x, int y, string[] tags) 59 { 60 using (MagickImage image = new MagickImage(inputFilePath)) 61 { 62 // Create annotation text with tags 63 string annotationWithTags = $"{annotationText}\\nTags: {string.Join(", ", tags)}"; 64 65 // Annotate the image with the text at the specified position 66 image.Annotate(annotationWithTags, new MagickGeometry(x, y), Gravity.Northwest); 67 68 // Save the modified image 69 image.Write(outputFilePath); 70 } 71 } 72 73 //// Method to render HTML content onto an image using ImageMagick and HtmlRenderer.Core 74 //public void RenderHtmlToImage(string htmlContent, string outputFilePath, int width, int height) 75 //{ 76 // // Render HTML content onto a bitmap 77 // Bitmap bitmap = new Bitmap(width, height); 78 // using (Graphics graphics = Graphics.FromImage(bitmap)) 79 // { 80 // HtmlRender.Render(graphics, htmlContent, new Rectangle(0, 0, width, height)); 81 // } 82 83 // // Convert bitmap to MagickImage 84 // using (MagickImage image = new MagickImage(bitmap)) 85 // { 86 // // Optionally, you can further process the image (resize, annotate, etc.) before saving 87 88 // // Save the image to the output file path 89 // image.Write(outputFilePath); 90 // } 91 //} 92 93 94 public void AddStamp() 95 { 96 var imageBackgroundColor = new MagickColor("White"); 97 98 using (MagickImageCollection images = new MagickImageCollection()) 99 { 100 // Read the existing image file 101 images.Read(@"C:\\test.tiff"); 102 103 // Iterate through each image in the collection 104 foreach (var image in images) 105 { 106 // Create a drawable for the annotation 107 var drawable = new DrawableText(0, 10, "Line One\\nLine Two\\nLine Three"); 108 var gravity = new DrawableGravity(Gravity.North); 109 var font = new DrawableFont("Arial"); 110 var size = new DrawableFontPointSize(50); 111 var color = new DrawableFillColor(MagickColors.Black); 112 113 // Annotate the image 114 image.Annotate("Some annotation", Gravity.Center); 115 116 // Draw the annotation on the image 117 image.Draw(drawable, gravity, font, size, color); 118 119 // Optionally, save the modified image to a new file 120 image.Write(@"c:\\test2.tiff"); 121 } 122 } 123 124 125 126 MagickImage img = new MagickImage(MagickColors.White, 80, 96); 127 img.Settings.AntiAlias = false; 128 img.Settings.StrokeAntiAlias = false; 129 img.Settings.TextAntiAlias = false; 130 img.SetCompression(CompressionMethod.Group4); 131 img.Format = MagickFormat.Tiff; 132 133 var drawables = new Drawables() 134 .FillColor(MagickColors.Black) 135 .Font("Arial") 136 .FontPointSize(14); 137 138 drawables.Text(0, 25, "dddDDDddDDWwwWW"); 139 img.Draw(drawables); 140 141 img.Write(@"c:\\test.tiff"); 142 143 } 144 145 // Method to modify TIFF tags 146 public void ModifyTiffTags(string imagePath, TiffTag[] tags) 147 { 148 // Load the image 149 using (MagickImage image = new MagickImage(imagePath)) 150 { 151 // Get or create the EXIF profile 152 IptcProfile iptcProfile = (IptcProfile)(image.GetIptcProfile() ?? new IptcProfile()); 153 154 // Add or update the specified tags 155 foreach (var tag in tags) 156 { 157 iptcProfile.SetValue(tag.Tag , tag.Value); 158 } 159 160 // Update the image with the modified profile 161 //image.AddProfile(iptcProfile); 162 163 // Save the modified image 164 image.Write(imagePath); 165 } 166 } 167} 168// Model for TIFF tag 169public class TiffTag 170{ 171 public ImageMagick.IptcTag Tag { get; set; } 172 public string Value { get; set; } 173} 174} 175