Creating Device in C# (Device not recognized as keyword) - c#

I decided to try working with C# over c++ but ran into a problem.
The following code snippet shows the problem.
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 Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Device;
namespace Project_AREA
{
public partial class Form1 : Form
{
private Device device;
public Form1()
{
InitializeComponent();
}
}
}
That's as much code as I have right now.
In private Device device;, the keyword Device is not getting recognized although all the libraries have been referenced and the using directives are included.
Any ideas?

Managed to Solve the problem.. for your reference and also if other run in to the same problem. You must add all the following references to the project: (note all files can be found on your had disk at c:\Windows\Microsoft.NET\DirectX for Managed Code) 1: Microsoft.DirectX.dll from folder 1.0.2902.0 2: Microsoft.DirectX.Direct3D.dll from folder 1.0.2902.0 3: MicrosoftMicrosoft.DirectX.Direct3DX.dll from folder 1.0.2911.0 Thanks for the help tho guys :)

Related

Bitmap can't be returned?

I'm new to C#. However, this seems like a trivial problem that yet I cannot find an answer to.
I'm trying to simply return a Bitmap object (defined via an interface) yet it cannot "find" the Bitmap class. I've imported the appropriate namespace System.Drawing; and yet I'm still getting the same error,
Type or namespace reference cannot be found (are you missing an assembly reference?)
Here is the code presently:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace ConsoleApplication1
{
public interface PlanetInterface
{
bool isColonized();
bool isDestroyed();
Bitmap getImage(); //Why does Bitmap not 'exist'.
}
}
Any help is much appreciated. Thanks.
You need to add Reference to System.Drawing it is not referenced by default in Console application:

Working with embedded resources in external dll, without visual studios

Okay, so I've ran into a problem that I am having trouble finding a solution. There are plenty of solutions using visual studios properties. The issue is that I'm not using visual studios, I'm using csc.exe to compile my code.
Here's what I have so far.
exe.cmd
dll.cmd
Resource.dll
Main.csx
Rsc.csx
a.png
This is all of the code for the .cmd files and .csx files
exe.cmd
#echo off
csc.exe /target:winexe /reference:Resource.dll /out:Main.exe Main.csx
pause
dll.cmd
#echo off
csc.exe /target:library /resource:a.png /out:Resource.dll Rsc.csx
pause
Rsc.csx
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace EmIm
{
public static class Bck
{
public static Image GetBck()
{
Bitmap bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("a.png"));
Image rtn = bmp;
return rtn;
}
}
}
Main.csx
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using EmIm;
namespace prg
{
class class_m
{
public static void Main()
{
Form f1 = new Form();
try
{
f1.BackgroundImage = Bck.GetBck();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
f1.ShowDialog();
}
}
}
When I run this, I get a messagebox that displays value of 'null' is not valid for 'stream'
What steps do I need to take to be able to access a.png with reflection, I have checked to make sure a.png is the correct name in the assembly. Any help would be greatly appreciated. Thanks :)
A large thanks to Hans Passant for providing the simple solution to this problem. I had no idea even where to look to find out how to answer this problem, but he summed it up in a single comment.
only one file had to be altered in order for the program to work
new Rsc.csx file
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
namespace EmIm
{
public static class Bck
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static Image GetBck()
{
Bitmap bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("a.png"));
Image rtn = bmp;
return rtn;
}
}
}
Once again, thanks Hans Passant for that quick fix to my problem, hopefully any other programmers stubborn enough to avoid Visual Studios could possibly look to this to find a solution that works without it. Have fun programming :)

I cant get SevenZipLib library to work (p/invoke)

Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
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.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.

Not able To find A Class in A namespace In c#

I am extracting images from pdf in c#.
Someone has suggested that use Aspose namespace which is a third party namespace.
I have downloaded the aspose and included in my project as a reference.But the problem is i am not able to find the class PdfExtractor which is used to extract images.
I am sharing the link in which some one has suggested to use aspose and also sharing my code.
This is a link
and my code in which i just include aspose
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspose;
using Aspose.Pdf;
using Aspose.Pdf.Devices;
using Aspose.Pdf.DOM;
using Aspose.Pdf.Generator;
using Aspose.Pdf.InteractiveFeatures;
using Aspose.Pdf.Structure;
using Aspose.Pdf.Text;
namespace Imageget
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
And The class which is suggested in the link is PdfExtractor i am also sharing its code below so one can not waste his time to go to link.
PdfExtractor objExtractor = new PdfExtractor();
//bind input pdf file
objExtractor.BindPdf("input.pdf");
//extract image with specific mode
objExtractor.ExtractImage(ExtractImageMode.Default);
//check if images extracted and save them one by one
while (objExtractor.HasNextImage()) {
objExtractor.GetNextImage(DateTime.Now.Ticks.ToString() + ".jpg");
}
A first sight at google told me that you might need to add namespace:
Aspose.Pdf.Facades
Check out here and see the tree at the left.
and don;t forget to look at my comment at the top, it always help to find the namespace of class if i am unable to find it.
hope it helps!

Cant type System.IO.Ports shorthand

I have the following code but still have to type full path to use the System.IO.Ports namespace even though I have the using clause in place. Am I missing something in my reference list?
The = new SerialPort returns a Error 5 'SerialPort' is a 'namespace' but is used like a 'type'
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 System.IO.Ports;
namespace SerialPort
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort counter = new SerialPort("COM5");
public Form1()
{
InitializeComponent();
}
Thanks
You're declaring a namespace of SerialPort. Don't do that. That's what's causing the problem.
All you've got to do is change the namespace, and you'll be fine. You could use an alias as per Honza's request, but I think the code would be clearer to everyone if you just renamed the namespace.
It's because your namespace carries the same name. Either rename your namespace or use an alias for the serial port, like this:
using SP = System.IO.Ports.SerialPort
And then you can use
SP counter = new SP("COM5");
But as Jon suggested, renaming your namespace is a clearer solution to anyone who'll read your code.

Categories