c# - 在表达式中创建的图像是否会立即处理?

标签 c# dispose using

这足够了吗:

    using (Graphics g = Graphics.FromImage(image))
    {
        g.DrawImage(newImage.GetThumbnailImage(10, 10, null, new IntPtr()), 3, 3, 10, 10);
    }

或者我应该使用:

  using (Graphics g = Graphics.FromImage(image))
        {
            using (Image i = newImage.GetThumbnailImage(10, 10, null, new IntPtr()))
            {
                g.DrawImage(i, 3, 3, 10, 10);
            }
        }

编辑: 有人可以添加一些 MS 引用,即使没有创建变量 - 资源也不会立即释放吗?

最佳答案

除非您专门调用它的 Dispose() 方法(或者它留下 using block ),否则它不会被释放。因此,就您的情况而言,使用第二个 using block 将是确保释放非托管资源的更安全选择。

关于c# - 在表达式中创建的图像是否会立即处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8141934/

相关文章:

c# - 如何以及何时处置 c# 静态成员?

c# - ZeroMQ Create Context 和 Create Socket 有多贵?

xslt - 我想使用 xslt 在 xml 文件中添加 "xmlns"属性

c# - 将代码重构为 using 语句

c# - 使用 xamarin 的移动跨平台本地化和仅 iOS 问题

c# - 替换c#中的字符串字符

c# - 如何在不等待其他方法完成的情况下从服务结构参与者获取状态?

c# - 使用引用的 ItemsSource 绑定(bind)到 SelectedItem

wcf - 我是否需要关闭和/或处置通过OperationContext.Current.GetCallbackChannel 获取的回调 channel ?

sharepoint - SharePoint 中的跨方法处置模式