Does the API support adding an annotation to an image?
Load image
Add annotation (such as Text)
Save Image with annotation
Thanks
Hi Parakash,
First of all, please accept our apologies for a bit delayed response.
Unfortunately, Aspose.Imaging APIs do not offer direct means to annotate an image, although you can use Graphics class to draw shapes such as ellipse & rectangles, and text onto an image. For better elaboration, please visit the following links.
Please let us know if we can be of further assistance achieving your goals.
Thanks Babar,
Your links helped me to use Graphics to write Text onto an image that I loaded from a file.
I am looking for some help on the Aspose.Imaging.Font class.
I need to construct a Font "Courier New" Font Size is 12
Both font name and size are being read from a table and can vary for each line that I am writing.
I am a bit lost is the em-size parameter and the graphics.unit parameter.
Thanks
Parkoos
Hi Parkoos,
Thank you for writing back.
Please note,
GraphicsUnit is an enumeration whereas a few overload versions of
Font class constructor accepts a value of
GraphicsUnit therefore you may specify the
GraphicsUnit while creating an instance of
Font class. Possible values for
GraphicsUnit are outlined
here along with their details.
Regarding your original inquiry, you may store the font names and their corresponding values in any storage such as DataTable, ArrayList, and create a Font instance using these values on run time. Please check the below provided code snippet using Hashtable to store the font names and sizes.
C#
// Create a new Hashtable & fill it
Hashtable hashtable =
new Hashtable();
hashtable.Add(“Courier New”, 10);
hashtable.Add(“Times New Roman”, 20);
hashtable.Add(“Calibri”, 30);
hashtable.Add(“Book Antiqua”, 40);
hashtable.Add(“Arial”, 50);
hashtable.Add(“Arial Black”, 60);
//Load an exiting image
using (Image image = Image.Load(myDir + “Sample.jpg”))
{
int y = 100;
//Create an instance of Graphics and load the image graphics
Graphics graphics =
new Graphics(image);
//Optional: Clear the canvas
graphics.Clear(Color.Wheat);
//Iterate over the Hashtable entries
//foreach may retrieve the entries irrelevant of the sequence
foreach (DictionaryEntry entry
in hashtable)
{
Console.WriteLine(“{0}, {1}”, entry.Key, entry.Value);
//Create an object of the Font class based on Hashtable entry’s Key & Value
//Where Key is the font name and Value is the font size
Font font =
new Font(entry.Key.ToString(),
float.Parse(entry.Value.ToString()), GraphicsUnit.Pixel);
//Create a Brush of Black color
Brush brush =
new Aspose.Imaging.Brushes.SolidBrush(Color.Black);
//Draw text on image canvas with a difference of 200 points on Y axis
graphics.DrawString(“Hello World”, font, brush,
new PointF(500, y = y + 200));
}
//Save results
image.Save(myDir + “output.jpg”);
}
Hope this helps a bit. Please feel free to write back in case you have further questions or concerns.
Babar,
I am trying to get Courier New, 12 point font working. I am attaching 2 files, FormTestBasic.tif and FormTestBasic.docx.
The DOCX file is Courier New, 12 point. Note the difference from the TIF file.
My question - the TIF does no seem to be in 12 point font. Thanks for your help.
Here is my code
//Create an instance of Image
Aspose.Imaging.ImageOptions.TiffOptions tiffOptions = new Aspose.Imaging.ImageOptions.TiffOptions();
tiffOptions.Source = new Aspose.Imaging.Sources.StreamSource(fs);
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(tiffOptions, 1710, 2190))
{
Aspose.Imaging.License license = new Aspose.Imaging.License();
license.SetLicense(“Aspose.Imaging.lic”);
//Create and initialize an instance of Graphics class
Aspose.Imaging.Graphics graphic = new Aspose.Imaging.Graphics(image);
Aspose.Imaging.Brushes.SolidBrush brush;
graphic.Clear(Aspose.Imaging.Color.White);
// write text
FormElement formElement = new FormElement();
Aspose.Imaging.Font aspFont = new Aspose.Imaging.Font(“Courier New”, 12, Aspose.Imaging.FontStyle.Bold, Aspose.Imaging.GraphicsUnit.Point);
brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Black);
Aspose.Imaging.Point aspPoint = new Aspose.Imaging.Point(200, 400);
string sWriteThis = “LAU,JOSEPH 58763258 03/27/2014 03/27/2014 21210950”;
graphic.DrawString(sWriteThis, aspFont, brush, aspPoint);
// save all changes.
image.Save(fs);
fs.Dispose();
}
Please ignore the FormElement line just below // write text - its a left over from my real code and is not used here.
Also note that there are spaces in the sWriteThis and it has been removed by the html in this post.
Parkoos
Hi Parkoos,
Thank you for writing back.
I am afraid, we will require some time to investigate your recently presented scenario as there is no hard & fast rule to determine the font size on an image. Although we can perform OCR operation to determine the font name and its em-size. I have done some tests on the attached image, and have got different results with different OCR tools available. Moreover, I have used a word processing utility such as MS Words and did a little bit of trial and error to pinpoint the size of the font. I assume the font size in the attached image could be between 10~12 pts (not more).
Babar,
I did some testing with the fonts specified in your Hello World attachment above.
I create a WORD doc file with each font - same as your Hello World. it is attached.
If you notice, it is quite different from the Hello World image in the above post.
The fonts get increasingly larger as one would expect from an increasing font size.
Could there be an issue within Aspose Imaging?
Regards
Parkoos
Hi Parkoos,
Thank you for writing back.
Unfortunately we do not consider your provided comparison to be valid because MS Word is a document processing engine and therefore its results cannot be compared to an image. Moreover, MS Word has zoom levels that depends upon the screen resolution where the document is displayed, meaning you may get visually different font sizes when displayed on different screens.
It would be more appropriate if you compare the results of Aspose.Imaging API with another imaging library such as GDI+. Please check the below provided code snippet that uses the same input image and writes text on it using Aspose.Imaging API and GDI+. From the attached results, you will notice that the text font style and size is exactly the same for both generated images. Please note, I have performed this test with several font families & sizes, and I am unable to find any discrepancy so far.
C#
File.Copy(myDir + “sample1.tiff”, myDir + “sample10.tiff”);
using (var myBitmap = new System.Drawing.Bitmap(myDir + “sample1.tiff”))
{
var GdiGraphics = System.Drawing.Graphics.FromImage(myBitmap);
GdiGraphics.Clear(System.Drawing.Color.White);
var GdifontFamily = new System.Drawing.FontFamily(“Times New Roman”);
var Gdifont = new System.Drawing.Font(GdifontFamily, 20, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
var GdisolidBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 0, 0));
GdiGraphics.DrawString(“Hello World”, Gdifont, GdisolidBrush, new System.Drawing.PointF(100, 100));
GdiGraphics.Save();
myBitmap.Save(myDir + “GdiOutput.tif”, System.Drawing.Imaging.ImageFormat.Tiff);
}
using (var image = Aspose.Imaging.Image.Load(myDir + “sample10.tiff”))
{
var AsposeGraphics = new Aspose.Imaging.Graphics(image);
AsposeGraphics.Clear(Aspose.Imaging.Color.White);
var AsposeFont = new Aspose.Imaging.Font(“Times New Roman”, 20f, FontStyle.Regular, Aspose.Imaging.GraphicsUnit.Pixel);
var AsposeSolidBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
AsposeGraphics.DrawString(“Hello World”, AsposeFont, AsposeSolidBrush, new Aspose.Imaging.PointF(100, 100));
image.Save(myDir + “AsposeOutput.tif”);
}
Thanks Babar.
Looks like using a Graphics brush to write strings on an image is not the way to solve the issue of overlaying a form image with data.
I will be looking into other imaging tools that support Annotations.
Regards
Parkoos