I've made a simple C# WinForms app, which makes a screen-capture
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX.Direct3D9;
using SlimDX;
namespace KMPP
{
public class DxScreenCapture
{
Device d;
public DxScreenCapture()
{
PresentParameters present_params = new PresentParameters();
present_params.Windowed = true;
present_params.SwapEffect = SwapEffect.Discard;
d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, present_params);
}
public Surface CaptureScreen()
{
Surface s = Surface.CreateOffscreenPlain(d, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.Scratch);
d.GetFrontBufferData(0, s);
return s;
}
}
}
now to call it:
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SlimDX.Direct3D9;
using SlimDX;
using KMPP;
using System.Diagnostics;
using System.Threading;
namespace dxcapture
{
public partial class Form1 : Form
{
DxScreenCapture sc = new DxScreenCapture();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
DateTime current = DateTime.Now;
string n = string.Format(#"text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bmp",DateTime.Now);
string directory = (#"C:\temp\");
string name = (".bmp");
string filename = String.Format("{0:hh-mm-ss}{1}", DateTime.Now, name);
string path = Path.Combine(directory, filename);
stopwatch.Start();
Thread.Sleep(1000);
Surface s = sc.CaptureScreen();
Surface.ToFile(s, path, ImageFileFormat.Bmp);
stopwatch.Stop();
s.Dispose();
textBox1.Text = ("Elapsed:" + stopwatch.Elapsed.TotalMilliseconds);
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
Everything works fine when I run this app on Windows 7 x64 (it was compiled here)
Unfortunately, when I try to run this app on Windows XP x86 machine - I'm getting following error:
How I tried to fix it?
installed latest DX on WinXP
installed latest SlimDX on WinXP (btw this step solved my previous problem)
installed latest .Net Framework v.4 on WinXP
compiled this app as x86 and used SlimDX.dll x86 for the same reason
I also put slimdx.dll into the same folder where dxcapture.exe (app name) is located
What might be the problem? Does WinXP support Directx9 screen capture?
edit: I've tried to comment-out different code-lines and it seems like "device creation" is the problem.. I mean this line:
d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, present_params);
WinXP machine has integrated ATI graphics, so, I don't know.. maybe that's the problem, maybe not, but I can't check my program on some other pc.
As mentioned in the bug report you filed, the problem appears to be with your system, whether it be missing DirectX components or your graphics adapter not supporting Direct3D 9. If your card doesn't at least support D3D9, you won't be able to use SlimDX (or any of the other DirectX wrappers) for any kind of rendering.
Related
What did I miss?
VS could not be found Capture and HaarCascade.
I added all opencv .dll and "Copy always".
And added References of Emgu.
My Emgu is emgucv-windesktop 3.2.0.2682-сuda. Visual Studio 2017
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 Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Windows;
namespace Emgu.FaceDetection
{
public partial class Form1 : Form
{
private Capture cap;
private HaarCascade haar;
public Form1()
{
InitializeComponent();
}
}
}
You can't use HaarCascade when using EmguCV3.X, it has been deprecated and replaced with CascadeClassifier
See here for an explination, the summary being it has changed to something akin to this:
CascadeClassifier _cascadeClassifier = new CascadeClassifier(#"C:\OPENCV_3.0.0\opencv\build\etc\haarcascades\" + "haarcascade_frontalface_alt2.xml");
A more complete example, and tutorial, using 3.X can be found here, a snippet from that blog:
private CascadeClassifier _cascadeClassifier;
_cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_alt_tree.xml");
using (var imageFrame = _capture.QueryFrame().ToImage<Bgr, Byte>())
{
if (imageFrame != null)
{
var grayframe = imageFrame.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here
foreach (var face in faces)
{
imageFrame.Draw(face, new Bgr(Color.BurlyWood), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them
}
}
imgCamUser.Image = imageFrame;
}
I am very new to C#, visual studio, and related Microsoft work. So it is very likely that I messed something up and I hope some one can help me a bit on this.
I create an Console application and I am trying to run some tutorial C# code. There is a red line under when I try to load Windos.Storage.Pickers and all namespaces after it. I attempt to add the references, but I can't find what I need there.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.Graphics.Imaging;
using Windows.Media.FaceAnalysis;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
FaceDetector faceDetector;
IList<DetectedFace> detectedFaces;
FileOpenPicker photoPicker = new FileOpenPicker();
photoPicker.ViewMode = PickerViewMode.Thumbnail;
photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
photoPicker.FileTypeFilter.Add(".jpg");
photoPicker.FileTypeFilter.Add(".jpeg");
photoPicker.FileTypeFilter.Add(".png");
photoPicker.FileTypeFilter.Add(".bmp");
StorageFile photoFile = await photoPicker.PickSingleFileAsync();
if (photoFile == null)
{
return;
}
}
}
}
the namespace you are tying to use is use for UWP development.
The problem is that you are trying to use it in Console Development.
Read about it here
I have got a problem when using EmguCV. Everytime I try to create SIFT or SURF detector I get exception "The type initializer for 'Emgu.CV.CvInvoke' threw an exception".
I've already: installed MSVCRT, copied OpenCV and Emgu dlls to the execution directory, added to solution OpenCV and Emgu dlls, added to solution Emgu.CV, Emgu.CV.GPU, Emgu.CV.ML, Emgu.CV.UI, Emgu.Util references.
Unfortunately I still got exception. I've tried to build SURFFeature example and it worked perfectly. It actually means that I make some mistake, but I can't find it... Please help...
Here's the code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Util;
using Emgu.CV.GPU;
namespace blablabla
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Bitmap waveform_to_file = new Bitmap(2, 2);
//Emgu.CV.Features2D.SIFTDetector detector = new Emgu.CV.Features2D.SIFTDetector();//(0, 3, 0.04, 10, 1.6);//(400, true, 3, 4);//
SURFDetector surfCPU = new SURFDetector(500, false);//I got exception on these lines
Emgu.CV.Util.VectorOfKeyPoint keypoints = new Emgu.CV.Util.VectorOfKeyPoint();
Emgu.CV.Image<Emgu.CV.Structure.Gray, Byte> waveform_to_file_gray = new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(waveform_to_file);
Emgu.CV.Features2D.ImageFeature<float>[] modKeyPointsArray = surfCPU/*detector*/.DetectFeatures(waveform_to_file_gray, null);
List<Emgu.CV.Features2D.ImageFeature<float>> modKeyPointsList = new List<Emgu.CV.Features2D.ImageFeature<float>>();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
}
}
}
Solution explorer:
http://ifotos.pl/zobacz/4JPG_wwaerwh.jpg
bin/debug:
http://ifotos.pl/zobacz/3JPG_wwaereh.jpg
I want to run the Skeinforge slicer program written in Python inside my Windows Phone 8 C# application. I have determined that I should probably use IronPython to do this, I have already determined that I can run Skeinforge inside the ipy.exe terminal I got when I installed IronPython. My problem though is that I am struggling to figure out how to host and run a Python script with IronPython inside Windows Phone 8. I have also already managed to get a simple hello world script running inside a Desktop Windows Forms application that transfers the applications console output to the Debug console with the following 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 System.Diagnostics;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DebugWriter debugW = new DebugWriter();
Console.SetOut(debugW);
}
private void button1_Click(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("Test.py");
tw.Write(scriptBox.Text);
tw.Close();
try
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
}
}
}
And this is the DebugWriter:
class DebugWriter : TextWriter
{
private StringBuilder content = new StringBuilder();
public DebugWriter()
{
Debug.WriteLine("Writing console to debug");
}
public override void Write(char value)
{
base.Write(value);
if (value == '\n')
{
Debug.WriteLine(content.ToString());
content = new StringBuilder();
}
else
{
content.Append(value);
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
I have no idea how to even add the IronPython libraries to my Windows Phone 8 application though as the standard libraries won't import. I have though tried compiling the apparently now defunct Windows Phone 7 libraries with the master source code and I can import these libraries, but I get absolutely no response on the debug terminal when I try to run my hello world script.
Do any of you have any idea how to get this woring in Windows Phone 8, if you know how to do this in Windows 8/Metro/RT then that would also probably work for WP8.
UPDATE:
I have looked at the debug output again and I seem to get this error when trying to use the WP7 libraries to run a hello world script:
A first chance exception of type 'System.NotImplementedException' occurred in Microsoft.Scripting.DLL
Error: The method or operation is not implemented.
I managed to get Skeinforge running on a modified version of IPY. You can get the source for my application here: http://skeinforgewp8.codeplex.com/
I am a total beginner at C#, but I have used Java a lot. I am trying to use the following code in my app to get location data. I am making a Windows 8 desktop app to use the GPS sensor in my device:
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 Windows.Devices.Sensors;
using Windows.Devices.Geolocation;
using Windows.Devices.Geolocation.Geoposition;
using Windows.Foundation;
namespace Hello_Location
{
public partial class Form1 :
{
public Form1()
{
InitializeComponent();
}
async private void Form1_Load(object sender, EventArgs e)
{
Geolocator loc = new Geolocator();
try
{
loc.DesiredAccuracy = PositionAccuracy.High;
Geoposition pos = await loc.GetGeopositionAsync();
var lat = pos.Coordinate.Latitude;
var lang = pos.Coordinate.Longitude;
Console.WriteLine(lat+ " " +lang);
}
catch (System.UnauthorizedAccessException)
{
// handle error
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I get this error:
'await' requires that the type
'Windows.Foundation.IAsyncOperation'
have a suitable GetAwaiter method. Are you missing a using directive
for 'System'? C:\Users\clidy\documents\visual studio
2012\Projects\Hello-Location\Hello-Location\Form1.cs
How can I fix this?
Also it will be very useful if you can point me to some resources for C# location and the sensor API for windows desktop apps. Upon googling, I am only getting Windows RT APIs.
To fix your error you have to reference to the link that Bart gave in one of the question's comments.
You might need to add a reference to System.Runtime.WindowsRuntime.dll
as well if you are using mapped types like Windows Runtime event
handlers:
...
That assembly resides in C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETCore\v4.5
I recently found a "solution" for a similar question: C# desktop application doesn't share my physical location. Maybe you might be interested by my approach: https://stackoverflow.com/a/14645837/674700.
It's more like a workaround and it's not targeting Windows 8, but it works in the end.
alex's solution works!
add that reference and geolocation api starts working like a charm! so do async methods for other sensors!
here is a function i just got working using it.
async public void UseGeoLocation()
{
Geolocator _GeoLocator = new Geolocator();
Geoposition _GeoPosition =
await _GeoLocator.GetGeopositionAsync();
Clipboard.Clear();
Clipboard.SetText("latitude," +
_GeoPosition.Coordinate.Latitude.ToString() +
"," + "longitude," + _GeoPosition.Coordinate.Longitude.ToString() +
"," + "heading," + _GeoPosition.Coordinate.Heading.ToString() +
"," + "speed," + _GeoPosition.Coordinate.Speed.ToString());
Application.Exit();
}