Visual Studio 未处理的VB.Net异常:System.Runtime.InteropServices.ExternalException:'GDI+中发生一般性错误

zzzyeukh  于 2023-01-05  发布在  .NET
关注(0)|答案(1)|浏览(389)

我正在尝试将我在VB.Net 6.0(Visual Studio 2022)上编写的绘图保存为.png文件。
第一个月
但是,在上面的代码段中,我得到了以下异常未处理消息:

  • 未处理的VB.Net异常:*
  • 系统。运行时。互操作服务。外部异常:'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
omqzjyyz

omqzjyyz1#

您是否尝试过以管理员权限运行应用?
从Windows 10开始,Windows不希望你将文件保存到C驱动器的根目录。因此,你应选择其他目录来保存文件。

相关问题