C# 如何返回位图类型?

C# 如何返回位图类型?,c#,bitmap,C#,Bitmap,我有一个方法,它应该截图并将图像返回给调用方法 public static Bitmap TakeScreenshot(int x, int y, int height, int width) { Rectangle bounds = new Rectangle(0, 0, height, width); Bitmap bitmap; using (bitmap = new Bitmap(bounds.Width, bounds.Height)) {

我有一个方法,它应该截图并将图像返回给调用方法

public static Bitmap TakeScreenshot(int x, int y, int height, int width)
{
    Rectangle bounds = new Rectangle(0, 0, height, width);
    Bitmap bitmap;

    using (bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);
        }
    }

    return bitmap;
}
问题是,当我尝试保存图片时:

Bitmap bitmap = MyClass.TakeScreenshot(0, 0, 200, 200);
bitmap.Save(@"C:\test.jpg", ImageFormat.Jpeg);
然后我在save方法中得到一个错误

异常未处理。 参数无效

如果我尝试将其保存在以下方法中,效果很好:

public static Bitmap TakeScreenshot(int x, int y, int height, int width)
{
    Rectangle bounds = new Rectangle(0, 0, height, width);

    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);
        }
        bitmap.Save(@"c:\begin.tiff", ImageFormat.Tiff);
    }
}
public static Bitmap TakeScreenshot(int x, int y, int height, int width)
{
    Rectangle bounds = new Rectangle(0, 0, height, width);
    Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    Graphics g = Graphics.FromImage(bitmap);
    g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);

    return bitmap;
}

这里缺少什么?

在您的第一个示例中,
位图已通过
using
语句进行了处理,然后保存

在第二个示例中,您在处理之前保存

您需要做的只是不要使用
语句将位图包装在
中,而是将其留给垃圾收集器,或者在保存后调用
.Dispose()


就个人而言,对于实现
IDisposable
接口的项目,我倾向于确保调用
Dispose
,除非我的用法要求它保持活动状态。

如果您在using块之外声明和访问位图对象,那么不要使用using块:)我会这样编码它:

public static Bitmap TakeScreenshot(int x, int y, int height, int width)
{
    Rectangle bounds = new Rectangle(0, 0, height, width);

    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);
        }
        bitmap.Save(@"c:\begin.tiff", ImageFormat.Tiff);
    }
}
public static Bitmap TakeScreenshot(int x, int y, int height, int width)
{
    Rectangle bounds = new Rectangle(0, 0, height, width);
    Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    Graphics g = Graphics.FromImage(bitmap);
    g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);

    return bitmap;
}

Bitmap
在返回之前已被释放-也就是说,您正在返回一个已调用
Dispose
IDisposable
对象:

using (bitmap = new Bitmap(bounds.Width, bounds.Height))
{
}
//using block makes sure Dispose is called when
//out of scope so this bitmap is no more
return bitmap;

如果要在该方法的作用域之外使用位图,则必须“释放所有权”(创建它,但不负责在该作用域内销毁),并允许调用方管理它,并相应地处理它。

是否尝试在第二个方法中另存为Jpeg?现在,您通过和失败的代码正试图以两种不同的格式保存。我知道,并且可能已经纠正了这一点,以避免混淆。但它将来会以相同的格式保存。谢谢。在实现IDisposable接口时,我会考虑这个问题。当你看到有人在一个句子中使用,你就知道你是一个程序员,你知道他们的意思。