Ich habe versucht, SVG-Bilder mit C# in PNG zu konvertieren, ohne zu viel Code zu schreiben. Kann jemand eine Bibliothek oder einen Beispielcode dafür empfehlen?Konvertieren von SVG in PNG mit C#
Antwort
können Sie rufen die Befehlszeilenversion von inkscape, dies zu tun:
http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx
es auch ein C# SVG-Rendering-Engine, in erster Linie entwickelt, SVG-Dateien zu ermöglichen, die im Internet auf Codeplex verwendet werden sollten das könnte Ihre Bedürfnisse anzupassen, wenn das Ihr Problem ist:
Original-Projekt
http://www.codeplex.com/svg
Gabel mit Fixes und mehr Aktivität: (hinzugefügt 7/2013)
https://github.com/vvvv/SVG
Ich Batik für diese verwenden. Der komplette Delphi-Code:
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;
Sie Altsoft XML2PDF lib für dieses
verwenden können, wenn ich hatte SVGs auf dem Server rastern, landete ich P/Invoke mit Funktionen aufrufen librsvg (Sie können das bekommen dlls aus einer Windows-Version des GIMP-Bildbearbeitungsprogramms).
[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());
}
}
Es gibt eine viel einfachere Weise, die Bibliothek http://svg.codeplex.com/ (Neuere Version @GIT, @NuGet) verwenden. Hier ist mein Code
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);
}
Ich musste die Github-Version verwenden, weil sie aktueller ist und sogar das Image-Element nicht unterstützt. – fireydude
Ich bin von diesem Code verwendet, es wirft 'Objekt nicht auf eine Instanz eines Objekts festgelegt, wenn Sie' var bitmap = svgDocument.Draw(); 'ausführen möchten. was ist das Problem? –
@RasoolGhafari stellen Sie sicher, dass Ihr svgDocument nicht null ist. – Anish
auf die Antwort von @Anish hinzuzufügen, wenn Sie Probleme haben mit dem Text nicht sehen, wenn die SVG zu einem Bild exportieren, können Sie eine rekursive Funktion einer Schleife durch die Kinder schaffen von das SVGDocument, versuche es wenn möglich in einen SvgText umzuwandeln (füge deine eigene Fehlerprüfung hinzu) und setze die Schriftfamilie und den Stil.
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
{
}
}
Lassen Sie mich wissen, wenn es Fragen gibt.
- 1. Abap: Konvertieren von Svg zu PNG
- 2. SVG-Stream in PNG oder JPG konvertieren
- 3. SVG in png oder andere konvertieren?
- 4. Konvertieren von SVG erstellt mit D3js zu PNG
- 5. Highchart.getSVG konvertieren in png
- 6. Hinzufügen von CSS-Stil beim Konvertieren von SVG nach PNG
- 7. SVG JPG/PNG
- 8. SVG-Code in PNG-Daten konvertieren URI in node.js
- 9. PNG zu SVG Python
- 10. Inkscape zum Umwandeln von SVG in PNG
- 11. Konvertieren von PNG in JPEG
- 12. Konvertieren von PDF in PNG mit ImageMagick
- 13. Convert SVG in PNG in Web Worker
- 14. Konvertieren Raphael SVG Bild (png etc.) Client-Seite
- 15. Konvertieren von Bitonal TIFF in Bitonal PNG in C#
- 16. Konvertieren von Bitmap zu PNG im Speicher in C++ (Win32)
- 17. Konvertieren von ppm nach PNG
- 18. Erstellen von PNG aus SVG XML ohne ImageMagick
- 19. readimageblob: Schwerwiegender Fehler beim Umwandeln von SVG in PNG
- 20. SVG Server-Seite PNG - mit node.js
- 21. Konvertieren von EPS/PDF in JPEG/PNG?
- 22. Konvertieren von PNG in Bitmap mit Transparenz mit opencv
- 23. SVG als PNG-Bild herunterladen
- 24. Konvertieren von Byte-Array in PNG-Bild
- 25. Konvertieren von PSD zu PNG in asp.net?
- 26. SVG in XAML konvertieren
- 27. Png-Datei in Ico mit PHP konvertieren
- 28. Verwenden von SVG mit referenzierten PNG als Hintergrundbild
- 29. SVG mit Muster und Bildelementkonvertierung nach PNG Bild fehlgeschlagen
- 30. Konvertieren von PDF in PNG mit gleicher Auflösung - Imagemagick
Danke Espo. Ich habe diesen Inkscape Blogpost geschrieben! Obwohl es "funktioniert", ist es keine besonders robuste Lösung. Ich mag das Codeplex-Projekt - ich werde es mir ansehen. Vielen Dank. – harriyott
Wie peinlich :) Gut, vielleicht könnte dir die SVG-Rendering-Engine helfen. – Espo
Ich nehme es als Kompliment. Ich wurde mir vorher nicht zitiert! – harriyott