Using Magick.NET with C# - c#

I am trying to implement a functionality using Magick.NET in C#.
Previously I was using:-
// Convert to a png.
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\ImageMagick-6.2.8-Q16\convert.exe";
p.StartInfo.Arguments = "-scale 60% \"" + svg + "\" \"" + png + "\"";
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
TransmitFile(context, png);
I want to move away from having to store convert.exe on the server.....Now I want to use something that will be in code and doesn't need to reference an executable file on the server:-
// Pseudo-code:-
MagickImage img = new MagicImage();
Image.Add(svg);
Image.Format = MagickFormat.png;
Image.Scale = 60%;
But I cannot find enough documentation to implement the same functionality that I was using before. Is there a place with appropriate documentations? I have googled quite a bit, without success.

There are some examples of how to use Magick.NET available here.
An example of how to convert one image to another image can be found here. But there is no example for -scale 60%.
Most options from the command line have the same name in the MagickImage class. Your command convert input.svg -scale 60% output.png translates to this:
using (MagickImage image = new MagickImage("input.svg"))
{
image.Scale(new Percentage(60));
image.Write("output.png");
}

Related

c# extract mp3 file from mp4 file

is there any easy way of extracting a mp3 file from a mp4 file?
I've already tried to change the file extension, but that won't let me to edit the mp3 description.
thank you!
Use Xabe.FFmpeg. It's free (non-commercial use), has public repository on GitHub, cross-platform (written in .NET Standard).
Extracting mp3 from mp4 just by 3 lines:
string output = Path.ChangeExtension(Path.GetTempFileName(), FileExtensions.Mp3);
IConversionResult result = await Conversion.ExtractAudio(Resources.Mp4WithAudio, output)
.Start();
It requires FFmpeg executables like in other answer but you can download it by
FFmpeg.GetLatestVersion();
Full documentation available here - Xabe.FFmpeg Documentation
using FFMPEG you could do something like this:
using System;
using System.Diagnostics;
namespace ConvertMP4ToMP3
{
internal class Program
{
static void Main(string[] args)
{
var inputFile = args[0] + ".mp4";
var outputFile = args[1] + ".mp3";
var mp3out = "";
var ffmpegProcess = new Process();
ffmpegProcess.StartInfo.UseShellExecute = false;
ffmpegProcess.StartInfo.RedirectStandardInput = true;
ffmpegProcess.StartInfo.RedirectStandardOutput = true;
ffmpegProcess.StartInfo.RedirectStandardError = true;
ffmpegProcess.StartInfo.CreateNoWindow = true;
ffmpegProcess.StartInfo.FileName = "//path/to/ffmpeg";
ffmpegProcess.StartInfo.Arguments = " -i " + inputFile + " -vn -f mp3 -ab 320k output " + outputFile;
ffmpegProcess.Start();
ffmpegProcess.StandardOutput.ReadToEnd();
mp3out = ffmpegProcess.StandardError.ReadToEnd();
ffmpegProcess.WaitForExit();
if (!ffmpegProcess.HasExited)
{
ffmpegProcess.Kill();
}
Console.WriteLine(mp3out);
}
}
}
Perhaps? Here your would call the .exe from the command line with the path to the input and output files minus the extensions, a la:
C:\>ConvertMP3ToMP4.exe C:\Videos\Aqua\BarbieGirl C:\Music\Aqua\BarbiGirl
You will, of course, need to provide the full path to the FFMPEG executable on line 19. A small caveat here, I just back of the enveloped this so the code may have problems, I did not compile or test it, this is just a starting point. Good luck.

C# run a vbscript encoded file without the .vbe extension using process start

I can use process start and run a standard vbscript without its .vbs extension (see my code below) but how do you run a vbscript file that has been encoded using the VBScript Encoder object without the .vbe extension?
Process process = new Process();
process.StartInfo.FileName = #"wscript.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = "//e:vbscript noExtensionFile";
process.Start();
If you're trying to run an encoded VBScript file, you want the VBScript.Encode engine rather than the VBScript engine. Try using //e:VBScript.Encode as your first argument to wscript.
Assuming your problem is with the file not having an extension and you can rename it you could do something like:
string filename = ....
if (!Path.HasExtension(filename))
{
string t = Path.ChangeExtension(filename, ".vbe");
File.Move(filename, t);
filename = t;
}
// rest of your code

Get apk infromation using c#

I want to get internal names of apk files and I've tried a lot of ways and I've searched a lot. finally this is what I've got :
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
proc.Start();
using (StreamWriter sw = proc.StandardInput)
{
foreach (string path in Directories.GetAllFiles(#"E:\apk", "apk"))
{
sw.WriteLine("aapt d badging " + "\"" + path + "\"");
//following line doesn't work because sStandardInput is open
Debug.WriteLine(proc.StandardOutput.ReadToEnd());
}
}
// There will be a really huge amount of text (at least 20 line per sw.Writeline) and
//it impacts performance so following line is not really what I'm looking for
Debug.WriteLine(proc.StandardOutput.ReadToEnd());
GetAllFiles is a function that I've created to fetch all apk file full path. This is not final code.
As I've commented it out first Debug.WriteLine doesn't work and second one is not a good idea. how should I get output from proc?
I have tried running proccess by argument and its awful :)

Text content in pdf is not converted using pdf2swf

I am running c# application in service mode. And i am using pdf2swf tool to convert odf to swf format. Images saved in pdf is converting. But if any test adding to pdf is not getting converted in service mode.
But when run as UI mode(Consoleapplication.exe) then everything is getting converted.
string inputFileName = this.Filename;
string outputFileName = inputFileName.Replace("pdf", "swf");
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} -o {1}", inputFileName, outputFileName);
string executingDirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string dataDirectoryPath = Path.Combine(executingDirPath, "pdf2swf.exe");
ProcessStartInfo psi = new ProcessStartInfo(dataDirectoryPath, sb.ToString());
psi.UseShellExecute = false;
System.Diagnostics.Process pdf2swf = new System.Diagnostics.Process();
pdf2swf.StartInfo = psi;
pdf2swf.Start();
pdf2swf.WaitForExit();
pdf2swf.Close();
pdf2swf.Dispose();
Regards
Sangeetha
Direct using process to start pdf2swf.ext maybe had some privilege problems.I used another way to solve this problem,write a batch file,then running the batch file by process.
Batch file sample:
c:
cd C:\Program Files (x86)\SWFTools\
pdf2swf.exe -f -T 9 -t "%1" -o "%2"
Code in program:
Process p = new Process();
string path = basePath + "/plugin/ConvertToSwf.bat";//batch file path
ProcessStartInfo pi = new ProcessStartInfo(path, filePath + " " + swfPath);//passing the file path and converted file path to batch file
pi.UseShellExecute = false;
pi.RedirectStandardOutput = true;
p.StartInfo = pi;
p.Start();
p.WaitForExit();
I faced a similar problem recently. I solved the issue by adding a separate console application(Consoleapplication.exe) with administrative-rights that runs on my server without shell.
Also, try to upgrade to the newest version of pdf2swf.
FYI. I recently had this problem (thought it was fonts not being embedded but actually was missing all text in converted swf). What fixed it for me was to set:
pi.UseShellExecute = false;
AND set the working directory;
pi.WorkingDirectory = "C:\windows\temp"; // path where read & write is

Read GnuPlot output into image in C#

I am using GnuPlot from a C# application. I'd like to read the GnuPlot PNG output directly from Standard Output rather than saving to a file and then reading it. My code looks like this right now:
string Path = #"C:\Program Files\gnuplot\bin\gnuplot.exe";
Process GnuplotProcess = new Process();
GnuplotProcess.StartInfo.FileName = Path;
GnuplotProcess.StartInfo.UseShellExecute = false;
GnuplotProcess.StartInfo.RedirectStandardInput = true;
GnuplotProcess.StartInfo.RedirectStandardOutput = true;
GnuplotProcess.Start();
StreamWriter SW = GnuplotProcess.StandardInput;
StreamReader SR = GnuplotProcess.StandardOutput;
SW.WriteLine("set terminal pngcairo size 300,200");
foreach (LoadCaseOutput LCO in LoadCases)
{
foreach (LoadCaseOutput.MemberOutput MO in LCO.Members)
{
SW.WriteLine("plot " + MO.GenerateAFEquation(P));
MO.AFImage = Image.FromStream(SR.BaseStream);
}
}
SW.WriteLine("exit");
GnuplotProcess.Close();
Right now this seems to stall at the Image.FromStream() line. What's going wrong?
update (I have updated my code to reflect my comment below)
It appears the problem is when the "exit" command is sent to gnuPlot. Without the exit command sent to gnuPlot, the program waits.
I took your example and was able to get the program to complete by moving the gnuPlot exit command up in the execution tree.
string Path = #"z:\tools\gnuplot\bin\gnuplot.exe";
Process GnuplotProcess = new Process();
GnuplotProcess.StartInfo.FileName = Path;
GnuplotProcess.StartInfo.UseShellExecute = false;
GnuplotProcess.StartInfo.RedirectStandardInput = true;
GnuplotProcess.StartInfo.RedirectStandardOutput = true;
GnuplotProcess.Start();
StreamWriter SW = GnuplotProcess.StandardInput;
StreamReader SR = GnuplotProcess.StandardOutput;
SW.WriteLine("set terminal pngcairo size 300,200");
SW.WriteLine("plot f(x) = sin(x*a), a = .2, f(x), a = .4, f(x)");
SW.WriteLine("exit");
Image png = Image.FromStream(SR.BaseStream);
png.Save(#"z:\tools\try3a.png");
GnuplotProcess.Close();
This did correctly generate a PNG file. For testing, I did try reading from the stream before sending the exit command. The program waits on the FromStream call.
Matt
The problem is not with the reading otherwise you would be getting an exception, replace the reading bit with this:
Image.FromStream(oFileStream, false, true)
It will validate the image as soon as it receives the first bytes and you go from there.

Categories

Resources