public void screenShot(string path)
{
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
bmpScreenshot.Save(path, ImageFormat.Png);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码捕获我的计算机屏幕.
但今天我发现有一个名为Bitmap.Dispose()的方法.
什么时候调用Dispose()有什么区别?代码运行至关重要吗?
Gyö*_*zeg 13
如果类型实现了IDisposable接口,则应该明确地调用该Dispose方法(显式或通过using块).
如果我不调用dispose()会发生什么?
如果你不这样做,析构函数(终结器)负责释放资源; 但是,它有一些缺点:
必须打电话Dispose。如果不这样做,就会有一些非托管资源(例如 GDI 对象)不会被清除。这意味着你会出现内存泄漏。
所以是的,请致电Dispose(或者更好,使用using (...) { ... })。
| 归档时间: |
|
| 查看次数: |
4400 次 |
| 最近记录: |