如何返回 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))
{
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的第一个示例中,
Bitmap
已通过using
语句进行处理,然后您将进行保存。在第二个示例中,您在处置之前进行保存。
您需要做的就是不要将位图包装在
using
语句中,而是将其留给垃圾收集器,或者在完成后调用.Dispose()
保存了它。就我个人而言,对于实现 IDisposable 接口的项目,我倾向于确保调用 Dispose,除非我的用途要求使其保持活动状态。
In your first example, the
Bitmap
has been disposed via theusing
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 sureDispose
is called, unless my usage dictates keeping it alive.Bitmap
在返回之前已被释放 - 也就是说,您返回的IDisposable
对象已经调用了Dispose
:如果您想在方法范围之外使用位图,那么您必须“释放所有权”(创建它,但不负责在该范围内销毁)并允许调用者管理它,并处置相应地。
The
Bitmap
is being disposed prior to being returned - that is to say, you're returning anIDisposable
object that has already hadDispose
called: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.
如果您在 using 块之外声明和访问 Bitmap 对象,则不要使用 using 块:) 我会像这样编码:
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: