【问题标题】:Argument exception when creating new bitmap [closed]创建新位图时出现参数异常[关闭]
【发布时间】:2012-11-10 11:23:43
【问题描述】:

我有一个小问题,当我尝试以下操作时,我的代码无法正常工作:

        Dim filename As String = "C:\Users\NahNah\Desktop\HeightMap.png"
        IO.File.Create(filename)
        Dim h As New Structures.HeightMap(1, 10, 512)
        Dim graph As Graphics = CreateGraphics()
        Dim png As New Bitmap(filename)

        graph.DrawImage(png, Width, Height)
        h.Generate()
        graph = h.Draw(graph)
        graph.DrawImage(png, 1024, 1024)
        png.Save(filename)

我在 line 处得到一个 Argument 异常

        Dim png As New Bitmap(filename)

如您所见,我真的不知道自己在做什么,我需要帮助。谢谢

【问题讨论】:

  • 您需要说出问题所在,而不仅仅是您有问题。
  • 你的主题说argument exception - 这是你得到的错误吗?在哪一行?
  • 我编辑了,它在昏暗的 png 作为新的位图(文件名)。我试图将 *.png 更改为 .bmp 但没有。

标签: vb.net bitmap


【解决方案1】:

当然失败了……

    IO.File.Create(filename)
    ......
    Dim png As New Bitmap(filename)

当您尝试打开一个像有效位图一样的空文件时会发生什么?

虽然我错过了很多细节(结构及其参数是个谜,你从哪里得到宽度和高度......),我认为你应该遵循这种方法

Dim h As New Structures.HeightMap(1, 10, 512)
Using png As New Bitmap(Width, Height)   
    Using graph = Graphics.FromImage(png)
        h.Generate()
        graph.DrawImage(png, 1024, 1024)
        png.Save(filename)
    End Using
End Using

【讨论】:

  • 好的,非常感谢!我需要了解什么是“使用”关键字?
  • Using 语句在使用位图等系统资源时非常重要。它保证当您完成使用这些资源时,它们将被释放(释放到系统)See docs on MSDN
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2013-11-14
  • 1970-01-01
相关资源
最近更新 更多