获取" GDI+"出现一般性错误;在c#

本文关键字:quot 错误 一般性 GDI+ 获取 | 更新日期: 2023-09-27 18:05:26

我编写了一个非常简单的代码,当用户按CTRL+V时从剪贴板获取图像。然后将图像保存到静态位置:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public System.Drawing.Image GetClipboardImage()
    {
        System.Drawing.Image returnImage = null;
        if (Clipboard.ContainsImage())
        {
            returnImage = Clipboard.GetImage();
        }
        return returnImage;
    }
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
        {
            Image img = GetClipboardImage();
            // ******* IT THROWS THE ERROR HERE*****************
            img.Save(@"C:'LOGO_ARTWORK_TEMP_IMAGE.jpeg", ImageFormat.Jpeg);
            this.Close();
        }
    }
}

当我试图保存图像时,我得到错误。这个程序在我的系统上工作得很好,但在客户的服务器上不起作用。是关于权限的吗?

这里有一个例外:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at LOGO_IMAGE_COPY_PASTE.Form1.Form1_KeyDown(Object sender, KeyEventArgs e) in D:'PROJELER'LOGO_IMAGE_COPY_PASTE'LOGO_IMAGE_COPY_PASTE'Form1.cs:line 36
   at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

获取" GDI+"出现一般性错误;在c#

您似乎不太可能拥有对客户服务器上的C:'的写入权限。除了运行程序,你还能在里面创建文件吗?

检查所粘贴的图像数据是否有效。然后检查权限并确认"C"驱动器确实存在。还要检查防火墙是否阻止程序创建该文件,或者是否已经存在同名的其他文件,从而阻止写入。