如何返回 Bitmap 类型?

发布于 2024-12-04 07:34:08 字数 1202 浏览 0 评论 0原文

我有这个方法,应该截取屏幕截图并将图像返回给调用方法。

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

然后我在保存方法中收到错误。

ArgumentException 未处理。 参数无效。

如果我尝试将其保存在这样的方法中,它会很好地工作:

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

我在这里缺少什么?

I have this method that is supposed to take a screenshot and return the image to the calling method.

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

The problem is that when I try to save the picture:

Bitmap bitmap = MyClass.TakeScreenshot(0, 0, 200, 200);
bitmap.Save(@"C:\test.jpg", ImageFormat.Jpeg);

Then I get an error at the save-method.

ArgumentException was unhandled.
Parameter is not valid.

It works fine if I try to save it inside the method like this:

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

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

银河中√捞星星 2024-12-11 07:34:08

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

在第二个示例中,您在处置之前进行保存。

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

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

In your first example, the Bitmap has been disposed via the using statement, you are then saving afterwards.

In the second example, you are saving before the disposal.

All you should need to do is not wrap the bitmap in a using statement, Instead, either leave it for the garbage collector, or call .Dispose() after you've saved it.

Personally, for items that implement the IDisposable interface, I tend to make sure Dispose is called, unless my usage dictates keeping it alive.

×纯※雪 2024-12-11 07:34:08

Bitmap 在返回之前已被释放 - 也就是说,您返回的 IDisposable 对象已经调用了 Dispose

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;

如果您想在方法范围之外使用位图,那么您必须“释放所有权”(创建它,但不负责在该范围内销毁)并允许调用者管理它,并处置相应地。

The Bitmap is being disposed prior to being returned - that is to say, you're returning an IDisposable object that has already had Dispose called:

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;

If you want to use the bitmap outside of scope of the method, then you'll have to "release ownership" (create it, but don't be responsible for destroying within that scope) and allow the caller to manage it, and dispose it accordingly.

画骨成沙 2024-12-11 07:34:08

如果您在 using 块之外声明和访问 Bitmap 对象,则不要使用 using 块:) 我会像这样编码:

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

If you're declaring and accessing the Bitmap object outside the using block, then don't use a using block :) I would have coded it like this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文