提问人:user20402884 提问时间:1/2/2023 更新时间:1/3/2023 访问量:71
VB.Net 未处理的异常:System.Runtime.InteropServices.ExternalException:“GDI+ 中发生一般错误
VB.Net Exception Unhandled: System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+
问:
我正在尝试将我在 Vb.Net 6.0 (Visual Studio 2022) 上编写的绘图保存到 .png 文件中。
bmp.Save("C:\graph.png", System.Drawing.Imaging.ImageFormat.Png)
但是,在上面的代码段中,我收到以下异常未处理消息:
VB.Net 异常未处理:
System.Runtime.InteropServices.ExternalException:“GDI+ 中发生一般错误
这是代码段所属的脚本的其余部分
Imports System.Security
Imports System.Security.Permissions
Imports System.Drawing.Bitmap
Imports System.Drawing.Imaging
Public Class Form1
Dim m, i As Integer
Dim h, Pi, Pi2, u, v, x, y As Single
Dim left As Integer = Me.left
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Dim top As Integer = Me.top
Dim centerX As Integer = Me.left + (Me.Width / 2)
Dim centerY As Integer = Me.top + (Me.Height / 2)
Dim bmp As New Bitmap(Me.Width, Me.Height)
Dim path As String = "C:\graph.png"
Dim perm As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, path)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim g As Graphics = Graphics.FromImage(bmp)
m = 1000
h = 1 / 100
Pi = 3.14
Pi2 = 3.14 * 2
x = 150
y = 150
For i = 1 To m
u = x * Cos(h * Pi2) + y * Sin(h * Pi2)
v = y * Cos(h * Pi2) - x * Sin(h * Pi2)
x = u
y = v
g.FillRectangle(New SolidBrush(Color.FromArgb(255, 0, 0)), x, y, 1, 1)
Next i
Try
perm.Demand()
' The application has permission to write to the file
bmp.Save("C:\graph.png", System.Drawing.Imaging.ImageFormat.Png)
Catch ex As SecurityException
' The application does not have permission to write to the file
End Try
End Sub
End Class
答:
0赞
Jiachen Li-MSFT
1/3/2023
#1
您是否尝试过使用管理员权限运行您的应用程序?
从 Windows 10 开始,Windows 不希望您将文件保存到 C 驱动器的根目录。因此,您应该选择其他目录来保存文件。
评论
"C:\graph.png"
"C:\SomeFolderWhereI HaveWriteAccessForSure\graph.png"
CodeAccessPermission.Demand()