sourcegit/src/Commands/GetImageFileAsBitmap.cs
leo 111bf2966a refactor: rewrite external editor supports
* supported editors can be different on different platforms.
* display founded editors only
2024-04-06 13:14:22 +08:00

40 lines
1.1 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using Avalonia.Media.Imaging;
namespace SourceGit.Commands
{
public static class GetImageFileAsBitmap
{
public static Bitmap Run(string repo, string revision, string file)
{
var starter = new ProcessStartInfo();
starter.WorkingDirectory = repo;
starter.FileName = Native.OS.GitExecutable;
starter.Arguments = $"show {revision}:\"{file}\"";
starter.UseShellExecute = false;
starter.CreateNoWindow = true;
starter.WindowStyle = ProcessWindowStyle.Hidden;
starter.RedirectStandardOutput = true;
try
{
var stream = new MemoryStream();
var proc = new Process() { StartInfo = starter };
proc.Start();
proc.StandardOutput.BaseStream.CopyTo(stream);
proc.WaitForExit();
proc.Close();
stream.Position = 0;
return new Bitmap(stream);
}
catch
{
return null;
}
}
}
}