C# を使用した SVG から PNG への変換 [終了]
質問
あまり多くのコードを記述することなく、C# を使用して SVG 画像を PNG に変換しようとしました。これを行うためのライブラリまたはコード例を推奨できる人はいますか?
解決
これを行うには、コマンドライン バージョンの inkscape を呼び出します。
http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx
また、C# SVG レンダリング エンジンもあります。これは主に、Codeplex 上の Web 上で SVG ファイルを使用できるように設計されており、それが問題である場合のニーズに合う可能性があります。
オリジナルプロジェクト
http://www.codeplex.com/svg
修正とさらなるアクティビティを含むフォーク: (2013 年 7 月追加)
https://github.com/vvvv/SVG
他のヒント
ライブラリを使用するより簡単な方法があります http://svg.codeplex.com/ (新しいバージョン@ギット, @NuGet)。これが私のコードです
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);
}
サーバー上で svgs をラスタライズする必要があるとき、最終的に P/Invoke を使用して librsvg 関数を呼び出すことになりました (Windows バージョンの GIMP 画像編集プログラムから DLL を取得できます)。
[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());
}
}
使っています バティック このために。完全な Delphi コード:
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;
@Anish からの応答に追加すると、SVG を画像にエクスポートするときにテキストが表示されないという問題が発生した場合は、SVGDocument の子をループする再帰関数を作成できます。次の場合は、それを SvgText にキャストしてみてください。 (独自のエラーチェックを追加) 可能で、フォントファミリーとスタイルを設定します。
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
{
}
}
ご質問がございましたらお知らせください。
これには altsoft xml2pdf lib を使用できます