I am trying to make a transparent GIF, however, I am observing that text is looking distorted with Anti-aliasing when image is rendered. In my case, I am rendering image on the browser.
I am using latest binary (Magick.NET-Q8-AnyCPU.7.0.4.400), which I downloaded from the Nuget.Org. I am coding in C# .NET. Here is my source code.
Below are the core methods to generate image frames and converting them to byte array.
Code: Select all
public static byte[] GenerateCountdown(DateTime processingDateTime, int numberOfFrames)
{
byte[] result = null;
using (MagickImageCollection collection = new MagickImageCollection())
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
collection.AddRange(GenerateFrames(DateTime.Now, numberOfFrames));
stopwatch.Stop();
stopwatch.Restart();
result = collection.ToByteArray(MagickFormat.Gif);
stopwatch.Stop();
}
return result;
}
private static IEnumerable<MagickImage> GenerateFrames(DateTime processingDateTime, int numberOfFrames)
{
List<MagickImage> magickImageList = new List<MagickImage>();
for (int i = 0; i < numberOfFrames; ++i)
{
DateTime dt = processingDateTime.AddSeconds(i);
string text = "Countdown " + i;
MagickImage frame = null;
if (!string.IsNullOrEmpty(text))
{
try
{
frame = new MagickImage(MagickColors.Transparent, 200, 40);
frame.WriteText(text, MagickColors.Black, MagickColors.Red, "Arial", 20);
frame.GifDisposeMethod = GifDisposeMethod.Background;
frame.Transparent(MagickColors.Black);
frame.AnimationDelay = 100;
magickImageList.Add(frame);
}
catch(Exception ex)
{
if (frame != null)
{
frame.Dispose();
frame = null;
}
}
}
}
return magickImageList;
}
Code: Select all
[Route("api/preview")]
[HttpGet]
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public HttpResponseMessage GeneratePreview()
{
HttpResponseMessage response = null;
byte[] resultBytes = GenerateCountdown(DateTime.Now, 60);
if (resultBytes != null)
{
response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(resultBytes);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/gif");
}
else
{
response = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return response;
}