Conversione da SVG a PNG utilizzando C# [chiuso]
Domanda
Ho provato a convertire le immagini SVG in PNG utilizzando C#, senza dover scrivere troppo codice.Qualcuno può consigliare una libreria o un codice di esempio per farlo?
Soluzione
Puoi chiamare la versione da riga di comando di inkscape per fare ciò:
http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx
Inoltre esiste un motore di rendering SVG C#, progettato principalmente per consentire l'utilizzo di file SVG sul Web su codeplex che potrebbe soddisfare le tue esigenze se questo è il tuo problema:
Progetto originale
http://www.codeplex.com/svg
Fork con correzioni e più attività: (aggiunto il 7/2013)
https://github.com/vvvv/SVG
Altri suggerimenti
Esiste un modo molto più semplice per utilizzare la libreria http://svg.codeplex.com/ (Versione più nuova @IDIOTA, @NuGet).Ecco il mio codice
var byteArray = Encoding.ASCII.GetBytes(svgFileContents);
using (var stream = new MemoryStream(byteArray))
{
var svgDocument = SvgDocument.Open(stream);
var bitmap = svgDocument.Draw();
bitmap.Save(path, ImageFormat.Png);
}
Quando ho dovuto rasterizzare gli SVG sul server, ho finito per usare P/Invoke per chiamare le funzioni librsvg (puoi ottenere le DLL da una versione Windows del programma di modifica delle immagini GIMP).
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string pathname);
[DllImport("libgobject-2.0-0.dll", SetLastError = true)]
static extern void g_type_init();
[DllImport("librsvg-2-2.dll", SetLastError = true)]
static extern IntPtr rsvg_pixbuf_from_file_at_size(string file_name, int width, int height, out IntPtr error);
[DllImport("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static extern bool gdk_pixbuf_save(IntPtr pixbuf, string filename, string type, out IntPtr error, __arglist);
public static void RasterizeSvg(string inputFileName, string outputFileName)
{
bool callSuccessful = SetDllDirectory("C:\\Program Files\\GIMP-2.0\\bin");
if (!callSuccessful)
{
throw new Exception("Could not set DLL directory");
}
g_type_init();
IntPtr error;
IntPtr result = rsvg_pixbuf_from_file_at_size(inputFileName, -1, -1, out error);
if (error != IntPtr.Zero)
{
throw new Exception(Marshal.ReadInt32(error).ToString());
}
callSuccessful = gdk_pixbuf_save(result, outputFileName, "png", out error, __arglist(null));
if (!callSuccessful)
{
throw new Exception(error.ToInt32().ToString());
}
}
sto usando Batik per questo.Il codice Delphi completo:
procedure ExecNewProcess(ProgramName : String; Wait: Boolean);
var
StartInfo : TStartupInfo;
ProcInfo : TProcessInformation;
CreateOK : Boolean;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,
nil, nil, StartInfo, ProcInfo);
if CreateOK then begin
//may or may not be needed. Usually wait for child processes
if Wait then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end else
ShowMessage('Unable to run ' + ProgramName);
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
procedure ConvertSVGtoPNG(aFilename: String);
const
ExecLine = 'c:\windows\system32\java.exe -jar C:\Apps\batik-1.7\batik-rasterizer.jar ';
begin
ExecNewProcess(ExecLine + aFilename, True);
end;
Per aggiungere alla risposta di @Anish, se riscontri problemi nel non vedere il testo durante l'esportazione di SVG in un'immagine, puoi creare una funzione ricorsiva per scorrere i figli di SVGDocument, prova a trasmetterlo a SvgText se possibile (aggiungi il tuo controllo degli errori) e imposta la famiglia e lo stile del carattere.
foreach(var child in svgDocument.Children)
{
SetFont(child);
}
public void SetFont(SvgElement element)
{
foreach(var child in element.Children)
{
SetFont(child); //Call this function again with the child, this will loop
//until the element has no more children
}
try
{
var svgText = (SvgText)parent; //try to cast the element as a SvgText
//if it succeeds you can modify the font
svgText.Font = new Font("Arial", 12.0f);
svgText.FontSize = new SvgUnit(12.0f);
}
catch
{
}
}
Fammi sapere se ci sono domande.
puoi usare altsoft xml2pdf lib per questo