I am looking to see if it is possible to automatically convert images to SVG and embedd them into HTML with the existing features in the GroupDocs.Convert package. I am trying to stay away from having to cache images. If anyone can point me in the right direction, or other forum posts of peopl trying to achieve the same thing that would be great, I could not find any.
This is a slimmed down version of what I am already working with.
class CustomContentConverter : IContentConverter
{
static CustomContentConverter()
{
License groupDocLicense = new License();
using (var stream = new MemoryStream(Resources.GroupDocs_Total_NET))
groupDocLicense.SetLicense(stream);
}
private PossibleConversions GetPossibleConversions(string fileName)
{
var sourceFileType = FileType.FromFilename(fileName);
return Converter.GetPossibleConversions(sourceFileType.Extension);
}
public bool CanConvert(string from, string to)
{
if (string.IsNullOrWhiteSpace(to))
throw new ArgumentException("Invalid target type", nameof(to));
if (string.IsNullOrWhiteSpace(from))
throw new ArgumentException("Invalid file name", nameof(from));
var possibleConversions = GetPossibleConversions(from);
return possibleConversions.All.Any(tc => tc.Format.Extension == to);
}
public byte[] Convert(Stream content, string from, string to)
{
if (content == null)
throw new ArgumentNullException(nameof(content));
if (string.IsNullOrWhiteSpace(to))
throw new ArgumentException("Invalid target type", nameof(to));
if (string.IsNullOrWhiteSpace(from))
throw new ArgumentException("Invalid file name", nameof(from));
var possibleConversions = GetPossibleConversions(from);
var resultCopyStream = new ResultStream();
using (Converter converter = new Converter(
() => content,
() => possibleConversions.LoadOptions))
{
var targetConversionInfo = possibleConversions[to];
var convertOptions = targetConversionInfo.ConvertOptions;
var result = new ResultStream();
converter.Convert(() =>
{
if (result.CanRead)
{
result.CopyTo(resultCopyStream);
return result;
}
else
{
return resultCopyStream;
}
},
targetConversionInfo.ConvertOptions);
return result.WrittenContent;
}
}
class ResultStream : MemoryStream
{
public byte[] WrittenContent { get; private set; }
public override void Close()
{
if (CanRead)
{
Position = 0;
WrittenContent = ToArray();
}
base.Close();
}
}
}