gpt4 book ai didi

c# - Directory.Exists 导致编译错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:38 25 4
gpt4 key购买 nike

我正在做一个 Asp.Net 网络应用程序。我需要录制屏幕并保存录制的文件。我搜索了很多并从 here 得到了最适合我的代码

这是代码

   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();
}
}

我在 asp.net 中创建了一个新类并添加了上面的代码,但它抛出了类似“名称路径在当前上下文中不存在”这样的错误 “名称目录在当前上下文中不存在”

这是截图 code如何解决此错误,我希望在单击按钮后记录屏幕。但是如何在单击按钮时调用一个类。请帮忙。!!提前致谢 !!

最佳答案

因为要使用 Directory 的类,您必须引用 System.IO;

将这一行 using System.IO; 添加到后面的代码之上,或者您可以单击该类并按 Ctrl+。 它会建议您在弹出菜单。

关于c# - Directory.Exists 导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28848790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com