Reputation: 969
I am doing an Asp.Net web application. I need the screen to be recorded and save the recorded file . I searched a lot and got a code which suits best for me from here
This is the code
public class ScreenRecorder
{
private static string tempDir = Path.GetTempPath() + "/snapshot/";
private static System.Threading.Thread snap = new System.Threading.Thread(Snapshot);
private static System.Drawing.Rectangle _Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
public static System.Drawing.Rectangle Bounds
{
get { return _Bounds; }
set { _Bounds = value; }
}
private static void Snapshot()
{
if (!Directory.Exists(tempDir))
Directory.CreateDirectory(tempDir);
int Co = 0;
do
{
Co += 1;
System.Threading.Thread.Sleep(50);
System.Drawing.Bitmap X = new System.Drawing.Bitmap(_Bounds.Width, _Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using(System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(X)) {
G.CopyFromScreen(_Bounds.Location, new System.Drawing.Point(), _Bounds.Size);
System.Drawing.Rectangle CurBounds = new System.Drawing.Rectangle(System.Drawing.Point.Subtract(System.Windows.Forms.Cursor.Position,Bounds.Size), System.Windows.Forms.Cursor.Current.Size);
System.Windows.Forms.Cursors.Default.Draw(G, CurBounds);
}
System.IO.FileStream FS = new System.IO.FileStream(tempDir + FormatString(Co.ToString(), 5, '0') + ".png", System.IO.FileMode.OpenOrCreate);
X.Save(FS, System.Drawing.Imaging.ImageFormat.Png);
X.Dispose();
FS.Close();
} while (true);
}
public static void ClearRecording()
{
if (Directory.Exists(tempDir))
Directory.Delete(tempDir, true);
Directory.CreateDirectory(tempDir);
}
public static void Save(string Output)
{
System.Windows.Media.Imaging.GifBitmapEncoder G = new System.Windows.Media.Imaging.GifBitmapEncoder();
List<System.IO.FileStream> X = new List<System.IO.FileStream>();
foreach (string Fi in Directory.GetFiles(tempDir, "*.png", SearchOption.TopDirectoryOnly))
{
System.IO.FileStream TempStream = new System.IO.FileStream(Fi, System.IO.FileMode.Open);
System.Windows.Media.Imaging.BitmapFrame Frame = System.Windows.Media.Imaging.BitmapFrame.Create(TempStream);
X.Add(TempStream);
G.Frames.Add(Frame);
}
System.IO.FileStream FS = new System.IO.FileStream(Output, System.IO.FileMode.OpenOrCreate);
G.Save(FS);
FS.Close();
foreach (System.IO.FileStream St in X)
{
St.Close();
}
}
public static void Start()
{
snap = new System.Threading.Thread(Snapshot);
snap.Start();
}
public static void Stop()
{
snap.Abort();
}
private static string FormatString(string S, int places, char character)
{
if (S.Length >= places)
return S;
for (int X = S.Length; X <= places; X++)
{
S = character + S;
}
return S;
}
}
class Program
{
static void Main(string[] args)
{
ScreenRecorder.Start();
System.Threading.Thread.Sleep(5000);
ScreenRecorder.Stop();
ScreenRecorder.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\video.gif");
ScreenRecorder.ClearRecording();
}
}
I created a new class in asp.net and added the above code but it throws me error like "The name path doesn't exist in the current context " "The name directory doesn't exist in the current context"
Here is the screenshot
How to resolve this error and I want the screen to be recorded after clicking the button. But how to call a class on button click. Please help.!! Thanks in advance !!
Upvotes: 1
Views: 416
Reputation: 5650
You're trying to use a class from a different namespace, one that is not referenced.
There are two ways to go about this.
One way is to write the namespace along with the class that you want to use. For example:
if (!System.IO.Directory.Exists(tempDir))
System.IO.Directory.CreateDirectory(tempDir);
This is useful if you only need to use the class once, but if you want to use it more often...
using System.IO;
, as Aria answered.There's also a neat little trick in Visual Studio:
If the class you're trying to use actually exists within the linked libraries (in this case, System.IO
is part of the mscorlib.dll
, so it should be linked by default), then you can right-click on the unknown class and a menu option called "Resolve" should be available, which will add the required namespace to the using
section or add the full namespace to the class name.
Upvotes: 4
Reputation: 3844
Because to use Directory's classes you must reference to System.IO;
add this line using System.IO;
on top of the code behind or you can click on that class and press Ctrl+.
it will suggest you related namespaces in a popup menu.
Upvotes: 5