简体   繁体   English

C#:将屏幕截图保存到应用程序基目录中的子目录中?

[英]C# : saving screenshot into a sub directory within the app base directory?

I am attempting to save a screenshot into the img sub-directory located inside of the apps base directory;我试图将屏幕截图保存到位于应用程序基目录内的img子目录中; however, the image file is saved within the apps base directory.但是,图像文件保存在应用程序基本目录中。 What should I do to save the image into the img sub-directory?我应该怎么做才能将图像保存到img子目录中?

Code:代码:

private void screenCapture()
{
    try
    {
        string appDir = AppDomain.CurrentDomain.BaseDirectory;
        string pathString = System.IO.Path.Combine(appDir, "img");

        Bitmap memoryImage;
        memoryImage = new Bitmap(1000, 900);
        Size s = new Size(memoryImage.Width, memoryImage.Height);

        Graphics memoryGraphics = Graphics.FromImage(memoryImage);

        memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);

        string fileName =
            string.Format(pathString
            + DateTime.Now.ToString("yyyyMMdd_hhmmss")
            + ".png");

        // save it  
        memoryImage.Save(fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

You're just not combining the path components and the filename properly.您只是没有正确组合路径组件和文件名。 Try this:尝试这个:

string appDir = AppDomain.CurrentDomain.BaseDirectory;
string pathString = System.IO.Path.Combine(appDir, "img");
string fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".png";
string fullPath = System.IO.Path.Combine(pathString, fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM