Cannot see the Image type in System.Drawing namespace in .NET - c#

I'm trying to write a program that sorts images in specific folder by ther dimensions and moves little images to another folder via simple .NET console application. I decided to use System.Drawing.Image class to get the image dimentions from an image file. But I face following error:
The type or namespace name 'Image' could not be found (are you missing
a using directive or an assembly referrence?)
What exactly did I do wrong and why it doesn't see this class?
Here are the complete code of my program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
namespace ImageSort
{
class Program
{
static void Main(string[] args)
{
string targetPath = #"d:\SmallImages";
string[] files = Directory.GetFiles(#"d:\Images");
foreach (string path in files)
{
if (File.Exists(path))
{
Image newImage = Image.FromFile(path);
var Width = (int)(newImage.Width);
var Height = (int)(newImage.Height);
if (Width * Height < 660000) {
System.IO.File.Move(path, targetPath);
}
}
}
}
}
}

You need to add a reference : System.Drawing.dll.
In Solution Explorer, right-click on the References node and choose Add Reference and find System.Drawing.dll.

The answer to this issue for .NET Core 3.1 is to simply install System.Drawing.Common from NuGet.

Related

C# find the length of a video file

I am using the following code to find the length of a video file.
WindowsMediaPlayer windowsMediaPlayer = new WindowsMediaPlayer();
WindowsMediaPlayer player = windowsMediaPlayer;
var clip = player.newMedia(strPath);
WMPLength = $"{TimeSpan.FromSeconds(clip.duration)} ";
player.close();
The code returns what I expect but there are two problems.
One it crashes randomly. It crashes like its out of memory but while I do see memory usage go up it doesn't appear to be enough to crash the program
Two it is very slow
Am I missing something in cleaning up the code, a memory leak
or is there a better way to do this?
Thank you
First thing first: your method requires WMP installed on the computer, there are many editions without WMP installed oob.
This is an approach i am using and no problems so far (not sure why you need the media player) is it a specific requirement, extension ?
1-Install the following NuGet package, Microsoft.WindowsAPICodePack-Shell
https://www.nuget.org/packages/Microsoft.WindowsAPICodePack-Shell
2-Use the code snipet
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VideoLenght
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetVideoDuration(#"C:\videos\20190531_005611.mp4"));
Console.ReadLine();
}
public static TimeSpan GetVideoDuration(string filePath)
{
using (var shell = ShellObject.FromParsingName(filePath))
{
IShellProperty prop = shell.Properties.System.Media.Duration;
var t = (ulong)prop.ValueAsObject;
return TimeSpan.FromTicks((long)t);
}
}
}
}

Moving all unknown and future folders to specific folder

I am trying to create script for my terminal server, which will move all folders with their contents, created in the wrong place. I don't know which names these folders will have. As I noticed, moving folders in C# is a problem for some reason. Can someone help me with my code? It just deleting my test folders and nothing moves.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.VisualBasic.FileIO;
namespace ConsoleApp1
{
public class Programm
{
public static void Main()
{
string root = #"C:\Users\user1\Desktop";
string[] subdirectoryEntries = Directory.GetDirectories(root);
string destDirname = #"D:\confiscated";
foreach (string path in subdirectoryEntries)
{
FileSystem.MoveDirectory(path, destDirname, true);
}
}
}
}
This is how to do it:
string startPath = #"YOURSTARTPATH";
string endPath = #"YOURENDPATH";
foreach (string directory in Directory.GetDirectories(startPath))
{
Directory.Move(directory, Path.Combine(endPath, Path.GetFileName(directory)));
}
This way, you will use the already provided by the framework classes to work with directories.
No need to include Microsoft.VisualBasic.FileIO

C# connecting (referencing) couple *.cs files

I have couple questions about referencing methods / variables between two or more *.cs files. I know that there are similar topics, but I still don't quite understand what is going on.
I'm using Visual Studio Community 2015.
So here is the problem. I have 2 files, those files are First.cs and Second.cs. They are saved in completely different, known locations on hard disc.
Inside First.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class First
{
static void Main(string[] args)
{
}
public int GiveMeNumber()
{
return 5;
}
}
}
Inside Second.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class Second
{
int number = // method from file First.cs
}
}
How do I access method GiveMeNumber() from First.cs in Second.cs as assignment for int number? How do I tell my compiler where are those files?
Thanks for any help :)
Like Alex said in his comment.
You can create a solution/project, add existing item, and browse to your first.cs and second.cs.
Mark both files with the public-keyword
for example:
namespace Forum
{
public class Second
{
int number = // method from file First.cs
}
}
Then both class can be used within each other.
So you could do
var first = new First();
var number = first.GiveMeNumber();
you probably want to do it the other way around, because I think you have a console app where your First class has a main-method.
does that help>?

Roslyn CTP - How to Write Changes To File System

I created an empty Visual Studio Solution called Solution.sln which I load into the workspace int the first line. Then I add a project to the solution, and update the workspace to the latest solution which should now contain a project. How do I write out the files for the new stuff I added to the empty solution?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
namespace RoslynMainApp
{
class Program
{
static void Main(string[] args)
{
IWorkspace workspace = Workspace.LoadSolution(#"C:\RoslynSolutions\Solution.sln");
ProjectId projectId;
ISolution solution = Solution.Create(SolutionId.CreateNewId("Solution"));
solution.AddCSharpProject("Project1.dll", "Project1", out projectId);
var success = workspace.ApplyChanges(workspace.CurrentSolution, solution);
if(success)
{
//How do I write out all the stuff I just added to Solution.sln to the directory RoslynSolutions?
}
}
}
}
Thanks in advance,
Bob
The act of calling ApplyChanges should write the changes to disk. However, note that in CTP1, only a small set of the changes you can apply to solutions are actually implemented.

Why this message: does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument

Hi,
What I'm doing is a method to display images from a specific folder but when I debug I'm getting this error on the last line of code and I have no idea why.**
Error 3 'MBKiosk.classTools' does not contain a definition for 'Controls' and no extension
method 'Controls' accepting a first argument of type 'MBKiosk.classTools' could be found (are you
missing a using directive or an assembly reference?)
Thanks for any help.
Here is the code:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MBKiosk;
namespace MBKiosk
{
class classTools
{
public void ShowImages(string path)
{
FlowLayoutPanel imagePanel = new FlowLayoutPanel();
imagPanel.FlowDirection = FlowDirection.LeftToRight;
imagePanel.Size = new Size(1240, 630);
imagePanel.Location = new Point(12, 344);
imagePanel.WrapContents = true;
imagePanel.AutoScroll = false;
DirectoryInfo dInfo = new DirectoryInfo(path);
foreach (FileInfo file in dInfo.GetFiles())
{
System.Diagnostics.Debug.Print(file.Extension);
if ((file.Extension == ".jpg") || (file.Extension == ".gif") || (file.Extension ==
".png"))
{
PictureBox image = new PictureBox();
image.Image = Image.FromFile(file.FullName);
image.SizeMode = PictureBoxSizeMode.Normal;
image.Size = new Size(180, 108);
imagePanel.Controls.Add(image);
imagePanel.Refresh();
}
}
this.Controls.Add(imagePanel);
}
}
}
This can happen when you copy and paste code. this.Controls expects your class 'classTools' to have a member Controls. Either add the intended member variable to 'classTools' or derive it from another class.
You imported System.Windows.Forms but are not actually using it. Change you class definition to class classTools : Forms, and then you will be able to use the Controls class.
And seems like there is no Add method in the Controls class, as long as you don't add the Add extension method to Controls, it is going to give you an error.
In my case, I was copy/pasting the user controls for an other page, but I havn't checked the src at the Register tag on top of the page.
It appears it didn't started at the root (~/), so it could only find files that were on the same map:
<%# Register Src="MyPopUp.ascx" TagName="MyPopup" TagPrefix="uc3" %>
Due to this, it saw it as an UserControl rather than it's own class.
The page I copied the control to was on a different map, so correcting that path, and starting from the root have fixed the issue.

Categories