Creating a Bitmap in c# .net 4 Framework - c#

I am currently working on a small program, that loads an image file into the Console and print it out as an asci image. I am using the ".Net Console application framework 4". My Problem is, that I do not know how to create a Bitmap. After browsing for a while, I found many answers to my question, that suggested the "System.Drawing.Bitmap" class. When I try to refer to this class, visual studio does not even know System.Drawing.Bitmap. I have a class "AsciArt" which should convert the picture in the method ConvertToAsci. Does anybody know what my mistake is?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Bitmap;
namespace consoleGrafix
{
class AsciArt
{
private string ImgLink;
public AsciArt(string imageLocation)
{
this.ImgLink = imageLocation;
}
public void ConvertToAsci(string saveLocation)
{
var bm = Bitmap(saveLocation);
}
}
}

The solution was to set up my class by inheriting from image class. Also, in the ConvertTo Asci method, there was a "new". https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=netframework-4.0 . This really helped. Thank you!

Related

How to embed a R plot in a Winform in c#?

I am using R.NET to perform computation in my C# application and
now I'd like to display the results in a Winform.
Anyone could advise on how to embed a R plot in a winform using R.NET ?
I found the below post which seems outdated as I can't find any reference nor Nuget package for the RNETGraph namespace that they use. The link referenced in the post have also been archived.
display multiple R Embedded Graph in multiple panel winform c#
And I would like to avoid the ugly solution of saving the image and then loading it in a PictureBox as I need to change the plot dynamically according to user input.
Thanks
You can use Dieter Menne's RGraphHooks to display R's plot output in a graphical WinForms element (e.g. a Windows.Forms.Panel). RGraphHooks has a dependency to Dino Esposito's Win32 hooks library.
Usage of RGraphHooks is quite simple. See this blog post by Peter Dai Dinh for a small demo program.
What you basically do is, you attach an RGrapHook to a specific control in your WinForms GUI and then wrap your engine.Evaluate("plot(...)") in this hook:
RGraphAppHook cbt = new RGraphAppHook { GraphControl = panelForPlot };
cbt.Install();
engine.Evaluate("plot(rnorm(100))");
cbt.Uninstall();
I never got hold of R.NET - documentation just wasn't very clear.
There is another option, however. You can use the command line to transfer arguments from your C# application to your R.Script.
For example:
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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strCmdText;
strCmdText = "Rscript.exe [directory here]\\script.R 10 arg2"; //what comes after script.R are the arguments you are passing.
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
}
}
Then, it is very easy to retrieve the arguments in the R Script. Just use:
args <- commandArgs(trailingOnly = TRUE)
var1 <- args[1] #Argument 1
var2 <- args[2] #Argument 2
Addendum: Please note, you should have your RScript.exe in the environment variables in order to get the above to work.

How to use referenced c# dll code in a VS C# console application

So I have two dlls, Algorithms.dll and Data_Structures.dll (I made these from projects I found on GitHub). Using the browse feature I have managed to add both of the DLL files as references to my Visual Studio 2017 console project. The problem is I can't do anything else with them. Whenever I try to reference something within either file, it simply cannot be found. The only thing that is recognized is the namespace, but nothing inside of that.
What do I need to do to get VS to find the classes these DLLs contain so I can use them? I am aware I need to use Algorithms.Sorting for the example but I can't call anything so I used this as an example.
P.S. If you need more info, please ask. I'm not sure what's relevant to this issue.
EDIT: Ok, it was misleading to have that kind of example. Corrected but please read the question.
EDIT: I tried this on Monodevelop and get the same issue. Maybe it's not the IDE that's the problem?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Algorithms.Sorting; // Error, Sorting cannot be found, and neither can the file container Sorting
using Data_Structures; //Perfectly ok, can find the namespace
namespace CS_HW2_Testing_App
{
class Program
{
static void Main(string[] args)
{
// I'd like to call MergeSort and so forth here. What am I missing?!
}
}
}
Here's the top piece of the file containing MergeSort if it helps
using System;
using System.Collections.Generic;
using Algorithms.Common;
namespace Algorithms.Sorting
{
public static class MergeSorter
{
//
// Public merge-sort API
public static List<T> MergeSort<T>(this List<T> collection, Comparer<T> comparer = null)
{
comparer = comparer ?? Comparer<T>.Default;
return InternalMergeSort(collection, 0, collection.Count - 1, comparer);
}
...
In the first code block, you're importing the wrong namespace: using Algorithms.MergeSort should be using Algorithms.Sorting. Then you can use MergeSorter.MergeSort<T>(...) in your code!
You need to reference the namespace not the class.
using Algorithms.Sorting; //instead of using Algorithms.MergeSort;
Plus make sure the classes are public

Affdex videodetector

I'm currently doing a project where I have to use the Affectiva SDK to analyse some videos that I have recorded. I have downloaded the files, which they have given me and started writing the code for the SDK to work, however when calling the callback functions in my code, Visual Studio doesn't seem to accept the arguments that are put in. So I figured that the interfaces for the callback functions must be done. I'm not really clear on how to this though, since I thought this was all done in their assembly code. My code so far looks like this:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Affdex;
namespace ConsoleApplication2
{
class Program
{
public interface FaceListener { }
public interface ImageListener { }
public interface ProcessStatusListener { }
static void Main(string[] args)
{
VideoDetector detector = new VideoDetector(15);
String licensePath = "C:/Users/hamud/Desktop/sdk_ahmedmudi1992#gmail.com.license";
detector.setLicensePath(licensePath);
String classifierPath = "C:/Programmer/Affectiva/Affdex SDK/data";
detector.setClassifierPath(classifierPath);
detector.setFaceListener(this);
detector.setImageListener(this);
detector.setProcessStatusListener(this);
detector.setDetectSmile(true);
detector.setDetectSurprise(false);
detector.setDetectBrowRaise(false);
detector.setDetectAnger(false);
detector.setDetectDisgust(false);
detector.setDetectAllExpressions(false);
detector.start();
detector.stop();
}
}
}
As far as I know, I have to write code for the interfaces if I'm not mistaken... Or do I? Please help.
Here is a tutorial on getting started to analyze the video files.
As far as I know, I have to write code for the interfaces if I'm not mistaken... Or do I?
No you don't. You just have to implement the methods in the interfaces if you were to use them.
Here is the link to the sample app that uses Camera Detector which you can relate to since both the Camera Detector and the Video Detector implement the FaceListener, ProcessListener and ImageListener Interfaces.
EDIT: You have to implement the Listeners. For example in the code sample you are using the FaceListener so you need to write the implementation for the callbacks viz onFaceFound() and onFaceLost().
You may also want to create an object of processStatusListener and wait for the process to end for a video file something like this:
AffdexProcessStatusListener processStatusListener = new AffdexProcessStatusListener();
processStatusListener.wait();
Here is a link to our C# app AffdexMe which uses CameraDetector. You may find examples of CameraDetector, PhotoDetector, VideoDetector and FrameDetector in our getting started guide.

How to read image from file .NetPortable 4.5

I'm trying to simply read an image from file into a byte array. The problem is that I am using Xamarin and I want to decouple my Data Access code completely from Xamarin Forms. The problem is that my version of .Net (.NetPortable 4.5) does not contain System.IO.File so I don't know what to do now.
Can somebody point me in the right direction or recommend a lightweight package from Nuget? I want to achieve the follow without the use of Xamarin Forms and ideally load an image into a byte array with .NetPortable 4.5 or another light-weight mobile framework
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing; //Doesn't Exist in this .Net Framework!
using System.IO.File; //Doesn't Exist in this .Net Framework!
//using Xamarin.Forms; //I don't want to use Xamarin in this class
namespace EmployeeApp.DAT
{
public class EmployeeImageDAT
{
private ImageSource _employeeImageSource = ImageSource.FromFile("placeholder_image.png");
public EmployeeImageDAT() {
}
public ImageSource EmployeeImageSource
{
get
{
return _employeeImageSource;
}
set
{
_employeeImageSource = (value == null ? ImageSource.FromFile("placeholder_image.png") : value);
}
}
}
}

"Name 'Process' does not exist in current context" error in console app

I'm trying to use the Process class but it gives me this compiler error every time I do
The name 'Process' does not exist in the current context
I've already added using System.Diagnostics; at the top of my code but I still get this error. Autocomplete wont even recognize the class.
Hear is the code. I've only included the Main method because the rest of the code has nothing to do with the error.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
public class MainClass
{
static void Main()
{
AIRuntime adam = new AIRuntime("Adam", false);
adam.AIUpdate();
Process.Start(adam.directory); //adam.directory is just a string
}
}
My IDE is Xamarin Studio if it matters.
After web searching and attempts, I found the problem.
To fix this, I manually add a reference to System in my project options instead of just typing using System;. I also need to keep using System.Diagnostics; at the top. It's all in the picture below
Thanks guys for trying to help

Categories

Resources