简体   繁体   English

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

[英]VB.Net Exception Unhandled: System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+

I'm trying to save a plot I write on Vb.Net 6.0 (Visual Studio 2022) to a.png file.我正在尝试将我在 Vb.Net 6.0 (Visual Studio 2022) 上编写的 plot 保存到 .png 文件中。

bmp.Save("C:\graph.png", System.Drawing.Imaging.ImageFormat.Png)

However, in the above code segment, I get the following exception unhandled message:但是,在上面的代码段中,我收到以下异常未处理消息:

VB.Net Exception Unhandled:未处理的 VB.Net 异常:

System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+ System.Runtime.InteropServices.ExternalException: 'GDI+ 中发生一般性错误

This is the rest of the script to which the code segment belongs to这是代码段所属脚本的rest

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

Have you tried running your app with administrator privileges?您是否尝试过以管理员权限运行您的应用程序?

Starting with Windows 10, Windows doesn't want you to save files to the root of your C drive.从 Windows 10 开始,Windows 不希望您将文件保存到 C 驱动器的根目录。 So you should choose a different directory to save the file.所以你应该选择一个不同的目录来保存文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM