如何在用户的特定坐标处截图';s屏幕并将其另存为example.jpeg c#?

如何在用户的特定坐标处截图';s屏幕并将其另存为example.jpeg c#?,c#,image,graphics,bitmap,save,C#,Image,Graphics,Bitmap,Save,我想用左上角的p、宽度w和高度h截图,然后保存它 我试过这个。不起作用,或者我不知道如何让它起作用 然后我试着保存剪贴板,但也不起作用 SendKeys.SendWait("%{PRTSC}"); Bitmap bp = new Bitmap(Clipboard.GetImage()); bp.Save(@"C:\Users\hasht\spamImages\work.png", ImageFormat.Jpeg); 我也试过了 private Bitmap bitmap; private

我想用左上角的p、宽度w和高度h截图,然后保存它

我试过这个。不起作用,或者我不知道如何让它起作用

然后我试着保存剪贴板,但也不起作用

SendKeys.SendWait("%{PRTSC}"); 
Bitmap bp = new Bitmap(Clipboard.GetImage());
bp.Save(@"C:\Users\hasht\spamImages\work.png", ImageFormat.Jpeg);
我也试过了

private Bitmap bitmap;
private Graphics graph;
... 
graph.CopyFromScreen(new Point(1600, 0), new Point(1920, 0), new Size(320, 300));
Bitmap bp = new Bitmap(320, 300, graph);
bp.Save(@"C:\Users\hasht\spamImages\work.png", ImageFormat.Jpeg);
但是图形是空的,因为我从来没有创建过它。事实证明,您无法创建图形对象。我如何拍摄左上角P、宽度w和高度h的屏幕截图,然后保存它?

试试:

    Bitmap printscreen = new Bitmap(150, 150);

    Graphics graphics = Graphics.FromImage(printscreen as Image);

    graphics.CopyFromScreen(Point1, Point2, 0, 0, printscreen.Size);
    printscreen.Save(PATH_FILENAME, ImageFormat.Jpeg);
适用于

尝试:

    Bitmap printscreen = new Bitmap(150, 150);

    Graphics graphics = Graphics.FromImage(printscreen as Image);

    graphics.CopyFromScreen(Point1, Point2, 0, 0, printscreen.Size);
    printscreen.Save(PATH_FILENAME, ImageFormat.Jpeg);

上运行良好我将所有内容都放入一个函数中

/// <summary>
/// Takes a screenshot of the specified section of the screen.
/// </summary>
public void Screenshot(string filename, ImageFormat imageFormat, int width, int height, int sourceX, int sourceY, int destX, int destY)
{
    using (var bm = new Bitmap(width, height))
    {
        using (var gr = Graphics.FromImage(bm))
        {
            gr.CopyFromScreen(sourceX, sourceY, destX, destY, bm.Size);
            bm.Save(filename, imageFormat);
        }
    }
}
它可能需要在您的c#类的顶部导入以下内容:

代码中的图形对象为空,因为它从未初始化。在我的示例中,“var gr=Graphics.FromImage(bm);”行是对其进行初始化的(提供一个位图,然后它将写入其中)


我在位图和图形对象中使用“using”语句,因为在完成它们时需要调用Dispose方法(即使出现异常,using语句也会自动执行该操作)。

我将所有内容都放在一个函数中

/// <summary>
/// Takes a screenshot of the specified section of the screen.
/// </summary>
public void Screenshot(string filename, ImageFormat imageFormat, int width, int height, int sourceX, int sourceY, int destX, int destY)
{
    using (var bm = new Bitmap(width, height))
    {
        using (var gr = Graphics.FromImage(bm))
        {
            gr.CopyFromScreen(sourceX, sourceY, destX, destY, bm.Size);
            bm.Save(filename, imageFormat);
        }
    }
}
它可能需要在您的c#类的顶部导入以下内容:

代码中的图形对象为空,因为它从未初始化。在我的示例中,“var gr=Graphics.FromImage(bm);”行是对其进行初始化的(提供一个位图,然后它将写入其中)


我在位图和图形对象中使用“using”语句,因为在它们完成时需要调用Dispose方法(即使出现异常,using语句也会自动执行该操作)。

它是目标矩形左上角点的x和y坐标。以下是一些文档的链接:发生错误************************异常文本***************System.Runtime.InteropServices.ExternalException(0x80004005):GDI+中发生一般错误。在System.Drawing.Image.Save(字符串文件名,ImageCodeInfo编码器,EncoderParameters encoderParams)在System.Drawing.Image.Save(字符串文件名,ImageFormat格式)在WindowsFormsP1.Form1.Screenshot(字符串文件名,ImageFormat ImageFormat,Int32宽度,Int32高度,Int32 sourceX,Int32 sourceY,Int32 destX,Int32 destY)在C:\Users\hasht\source\repos\windowsformsap1\windowsformsap1\Form1.cs中:第118行“保存”时发生此错误可能是由权限引起的,如果文件保存两次但未释放,仍被锁定,则可能会导致此错误。如果放置文件的目录位置不存在,则可能导致此问题;如果路径包含非法字符,则可能导致此问题。它是目标矩形左上角点的x和y坐标。以下是一些文档的链接:发生错误************************异常文本***************System.Runtime.InteropServices.ExternalException(0x80004005):GDI+中发生一般错误。在System.Drawing.Image.Save(字符串文件名,ImageCodeInfo编码器,EncoderParameters encoderParams)在System.Drawing.Image.Save(字符串文件名,ImageFormat格式)在WindowsFormsP1.Form1.Screenshot(字符串文件名,ImageFormat ImageFormat,Int32宽度,Int32高度,Int32 sourceX,Int32 sourceY,Int32 destX,Int32 destY)在C:\Users\hasht\source\repos\windowsformsap1\windowsformsap1\Form1.cs中:第118行“保存”时发生此错误可能是由权限引起的,如果文件保存两次但未释放,仍被锁定,则可能会导致此错误。如果放置文件的目录位置不存在,则可能导致此问题;如果路径包含非法字符,则可能导致此问题。