【发布时间】:2016-06-14 11:32:38
【问题描述】:
我有一个应用程序,它创建了一个包含所有 1600 万种颜色的 Bitmap 对象。最终的位图将测量 4096 × 4096 像素。
当我尝试调用 Bitmap 的 Save() 方法时,会导致错误。
这是出现的错误消息:
GDI+ 中出现一般错误。
请在这件事上帮助我。 提前致谢!
注意:请看下面源代码的最后几行注释。我已经解释了那里的疑问。
源代码:
Public Sub CreateAllColorImage()
Dim BMP As New Bitmap(4096, 4096) 'This BMP variable is where the image will be created.'
Dim CurrX = 0
Dim CurrY = 0
Dim ExitFors As Boolean = False
For R = 0 To 255
For G = 0 To 255
For B = 0 To 255
BMP.SetPixel(CurrX, CurrY, Color.FromArgb(R, G, B))
CurrY += 1 'Increment the Y axis to move to next pixel.'
If CurrY > 4095 Then CurrX += 1 : CurrY = 0 'Move to next row, or increment X axis, if the last pixel on the Y axis is reached'
If CurrX > 4095 Then ExitFors = True 'Set the variable to exit the FOR loops if the very last pixel on the whole image is reached.'
If ExitFors Then Exit For 'Exit the FOR loop if the variable is true.'
Next
If ExitFors Then Exit For 'Exit the FOR loop if the variable is true.'
Next
If ExitFors Then Exit For 'Exit the FOR loop if the variable is true.'
Next
'So therefore, the final image is the BMP variable.'
'Here, I try to save the Bitmap as a file by calling this:'
BMP.Save("C:\TEST.BMP")
'This is when the error occurs. I think so because of the image is too large. If so, is there any way to do anything?'
'And by the way, I already have the rights to access the C:\ drive because I am working from an Administrator account...'
BMP.Dispose()
BMP = Nothing
End Sub
【问题讨论】:
-
为了检查您是否有权限这样做,只需在同一位置保存一个非常小的BMP文件,如果不能,则表示权限问题...跨度>
-
您的代码还有另一个隐藏的问题:您泄漏了 Bitmap 对象。要么将其构造包装在 Using 语句中,要么在 sub 结束时调用它的 Dispose 方法。更喜欢前者。
-
超级花生,你是对的。我尝试了我的桌面,它就像一个魅力!
-
@CodyGray 嗯,我会改变它