Extracting frames from image in C# - c#

I`ve seen some stackover flow on how to do this but i cant get it to work for myself in visual studio.
What is wrong with code? I have downloaded FFMpeg and im using it as reference. yet, I get the error
"Could not load file or assembly Aforge.Video.FFMPEG. dll or one of
its dependencies"
Here is the code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.FFMPEG;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
{
Bitmap videoFrame = reader.ReadVideoFrame( );
videoFrame.Save(i + ".bmp");
// dispose the frame when it is no longer required
videoFrame.Dispose( );
}
reader.Close( );
}
}
}
The program stop when i click the button and comes with an error.

It looks like the dll might have been moved / deleted (or) probably not checked-in. Usually the best practice is to add them in a library folder (say lib or bin) within your project. So when you check-in your stuff, all the library files get checked in too, and your references are intact.
Since the dll seems to be external, I'm hoping you browsed to it and added the reference.
Can you copy the dll file (preferably within a lib folder) within your project and then add the reference again?
It looks like Visual Studio added the reference but it's not able to find the file.
I'm guessing doing this will solve your problem.

You may need to check the way you build your project as well. Some DLLs may only work under x86 or x64 build configuration. Maybe not the parent dll directly but the references it uses internally.

You need to add to your bin folder the dlls from AForge.NET\Framework\Externals\ffmpeg and make sure you're running 32 bit (project properties -> debug -> platform -> x86

Related

System.IO.FileNotFoundException In multi-project solution

So I've been stuck on this error for a good week now, and it has been very frustrating
(Impossible to load the file or assembly 'Audio.Default.Switcher.Wrapper.dll' or one of its dependencies. The specified module wasn't found)
I'm working with Visual Studio 2017 and I've downloaded the SoundSwitch project, and everything is working perfectly fine. I'm able to run "SoundSwitch" without any errors.
The way the solution works (as I'm able to understand) is that the "SoundSwitch" C# project is the "master" and it has references to "Audio.Default.Switcher.Wrapper" as well as "SoundSwitch.UI.UserControls"
"Audio.Default.Switcher.Wrapper" as a reference to "AudioDefaultSwitcher"
Both "Audio.Default.Switcher.Wrapper" and "AudioDefaultSwitcher" seem to compile as .dll files
"SoundSwitch.UI.UserControls" cannot be started and is used for display stuff I guess
"SoundSwitch" is the only startable project (not counting mine of course)
The K005_test is MY project, I've added it and it has references (only) to "SoundSwitch" and I'm trying to use the functions from the SoundSwitch project inside K005_test. And so far my K005_test project consists only of
Program.cs (where I get the error by the way)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Exception thrown here
}
}
}
Form1.cs (where I put my code for the test)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This is a test");
var test = SoundSwitch.Model.AppModel.Instance.AvailablePlaybackDevices; //Error this line exists
}
}
}
And when I run my K005_test project, it compiles and runs just fine, except when I click the button Then I get the error I posted above. Also the message box doesn't open.
What I've tried so far :
Flipping everything to "x86" or "Win32", it seems that it might be related a problem with 64bit something
Using references to both "Audio.Default.Switcher.Wrapper.dll" and "SoundSwitch" in K005_test
copy "Audio.Default.Switcher.Wrapper.dll" pretty much everywhere and praying the gods
Trying to understand where it looks for "Audio.Default.Switcher.Wrapper.dll" but I wasn't able to find anything when looking in the exception details
Nothing has helped in any way.
For anybody wondering, I've uploaded my entire project folder here
https://drive.google.com/open?id=1OABjDiZyF0B-1-b15_9lzMrPZ4UpyKqU

Class library loses references (dlls) when being used

Sorry for the title. I don't know how to describe this problem shortly.
My problem is that I have a class-library which has references to other (third party) DLLs.
I need to use this class-library in another project, so I obviously added the .dll of my class-library to my main-project.
When I start my main-project, there's alway an error which says, that a reference (dll) in my class-library cannot be found.
If I add the whole class-library as a project to my projectmap in visual studio and then reference the whole project, this error doesn't occur.
I really don't want to add the whole class-library as a project to every "host"-project I make.
Has anyone an idea why this error occurs when the .dll of the class-library is added, but not when the whole project of the class-library is added as reference?
There must be a solution to get this working even if I don't add the whole library-project as reference. Otherwise it wouldn't make any sense to make a class library, right?
By the way: My class-library contains third-party dlls and the local copy property of the third-party dll is set to true.
Thanks in advance!
Edit:
My goal is to really make the class-library portable, even though it contains third-party libraries. I want to give only the .dll to another pc and use it without adding the whole class-library project every time.
The error is because you're not copying the dll's on the second project, you added a reference to your dll so it get's copied, but not the dll's referenced by your dll, so there are missing libraries.
Or you redistribute the dependencys with your dll or you can embedd the dll's inside your dll as resources and then intercept the assembly load and provide it through a resource: http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
EDIT: IN order to do it inside a dll you need to use an static class and call an static initializer BEFORE using any of the classes which are dependant on other libraries.
Here is an example setup:
-A library called LibraryB which supplies a simple class like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryB
{
public class ReferencedClass
{
public int GetIt()
{
return 5;
}
}
}
-A library called LibraryA which references LibraryB and supplies two classes, the initializer and the real class:
Initializer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace LibraryA
{
public static class Initializer
{
public static void Init()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
if (!args.Name.StartsWith("LibraryB"))
return null;
return Assembly.Load(LibraryA.Properties.Resources.LibraryB);
};
}
}
}
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryA
{
public class RealClass
{
public int DoIt()
{
LibraryB.ReferencedClass cl = new LibraryB.ReferencedClass();
return cl.GetIt();
}
}
}
The LibraryA also has the LibraryB.dll compiled library embedded as a resource.
-A project called Test which only references LibraryA:
using LibraryA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Initializer.Init();
RealClass c = new RealClass();
Console.WriteLine("From LibraryA: " + c.DoIt());
Console.ReadKey();
}
}
}
If you set-up everithing right and you execute it it will work, remember that if you are doing through visual studio, vs will copy the dll's so to do a real test after compiling all copy the exe and LibraryA and execute, it will work without LibraryB and LibraryB is being used from LibraryA.

LEADTOOLS uncompressing example getting an exception

I'm following the next example from the Leadtools page
https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.codecspngoptions.html
The version is the 19
But I'm getting this error in visual studio, {featured not supported}, I don't know what I'm doing wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
namespace DicomTest3
{
public class Program
{
public static void Main(string[] args)
{
const string LEAD_VARS = #"C:\Users\Public\Documents\LEADTOOLSImages";
RasterSupport.SetLicense(
#"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC",
File.ReadAllText(#"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC.KEY")
);
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS, "IMAGE1.CMP");
RasterImage image = codecs.Load(srcFileName);
// save with maximum quality
codecs.Options.Png.Save.QualityFactor = 1;
codecs.Save(image, Path.Combine(LEAD_VARS, "quality.png"), RasterImageFormat.Png, image.BitsPerPixel);
// save with maximum compression
codecs.Options.Png.Save.QualityFactor = 9;
codecs.Save(image, Path.Combine(LEAD_VARS, "compression.png"), RasterImageFormat.Png, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
}
}
Error during debugging
The most likely cause for this error is failing to load the assembly (codec DLL) for the PNG file format, which is Leadtools.Codecs.Png.dll.
You can either add it as a reference in the .NET project, or copy it to the same folder that has your EXE and other LEADTOOLS assemblies such as Leadtools.Codecs.dll
Our demos avoid such problems because they're all built in the BIN sub-folder for the project's platform. For example, if you build a 32-bit .NET 4 demo, its EXE will be placed in this folder:
LEADTOOLS 19\Bin\Dotnet4\Win32
This folder contains all the LEADTOOLS Dotnet4 assemblies for Win32, including the codecs.
The help topic Files to be Included with Your Application details which assemblies are required for different toolkit features.
If this does not solve the problem for you, please provide more details here or open a support case by emailing LEADTOOLS support.

C# play embedded mp3 file

I know there are a dozen questions like this and I looked at the more popular ones, but I have had no luck in figuring out anything.
I want to make a C# *.exe that would play a sound when opened, I decided it would be OK to start with a Console Application project, but I have no idea if that is the best choice.
I have added the "file.mp3" file to the resource designer under "Other" files and set its build action to "Embedded Resource".
From there I saw the suggestion about WMPLib, which I cannot access in my project - it is not present neither in my Reference Manager, neither does "using WMPLib;" work (it raises a "could not be found" error) or the one about the "System.Media.SoundPlayer.Play();" method but I am having trouble implementing it, because my complier either says an object reference is required for it or it has no overload, because it only takes 1 arguments if I make it like "System.Media.SoundPlayer.Play("file.mp3");"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Mum();
}
void Mum()
{
System.Media.SoundPlayer.Play();
}
}
}
To play an mp3 file, You will need to add the windows media player Library.
1. Add a reference to the WMP library - in your solution explorer, under your project, go to References, right click and add a reference to WMP. It will be under the COM libraries, I think.
2. Add "using WMPLib;" to your code at the top,
3. Add this code to play the file:
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = "mysound.mp3";
myplayer.controls.play();
mind you the URL is the path to the file you want to play. If it has backslashes in it, you will have to "escape" them. Like this
myplayer.URL = "C:\\Users\\Public\\Music\\Sample Music\\Kalimba.mp3";

Combining two WinForms projects

I have to projects in my solution: StoreHelper and DefaultsSwitcher. Both of them have to create a Windows Executable (their output is application).
Now we are located in StoreHelper. This is my code:
At the top of the file:
using DefaultsSwitcher;
And in the file:
private void defaultsSwitcherToolStripMenuItem_Click( object sender, EventArgs e ) {
var defaultsSwitcher = new DefaultsSwitcher.dialog();
defaultsSwitcher.ShowDialog();
}
The code editor does not show any error by underlining, but when I try to build, the compiler say the following error:
Error 1 The type or namespace name 'dialog' does not exist in the namespace 'DefaultsSwitcher' (are you missing an assembly reference?) E:\Apps\StoreHelper\StoreHelper\mainWindow.cs 84 25 StoreHelper
Which is the problem? P.S. I want these two projects to pe .exe files after building.
The code in DefaultsSwitcher:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreHelperLib.Options;
namespace DefaultsSwitcher {
public class dialog : Form {
public dialog() {
var options = new iniHelper();
if (!File.Exists( "settings.ini" )) {
StreamWriter settings = new StreamWriter( "settings.ini" );
settings.Close();
}
options.Load( "settings.ini" );
options.AddSection( "Application" );
options.SetKeyValue( "Application", "firstRun", "true" );
options.SetKeyValue( "Application", "startGuide", "false" );
options.Save( "settings.ini" );
InitializeComponent();
}
}
}
Is there any communication between the apps, or are you just trying to start the one app from the other app? If the latter, use System.Diagnostics.Process and have Windows run the other EXE file seperately.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
This also means that you can have both apps be able to start the other because there would not be a circular dependency.
I think that you have miss to include the child Project to his parent.
To do this : Right clic on Reference folder in StoreHelper project and clic on Add Reference.
Finally choose DefaultsSwitcher project.
Now the DefaultsSwitcher namespace must be accesible from the StoreHelper project
I found a solution for my problem to keep both projects output to EXE. I have just to use:
public partial class dialog

Categories

Resources