保存图像:GDI+ 中出现一般错误。(vb.net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2050996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:47:18  来源:igfitidea点击:

Saving image: A generic error occurred in GDI+. (vb.net)

.netvb.netimagegdi+

提问by Jonathan.

I need to save an image after opening it in from an OFD. This is my code atm:

从 OFD 打开图像后,我需要保存图像。这是我的代码自动取款机:

Dim ofd As New OpenFileDialog
ofd.Multiselect = True
ofd.ShowDialog()


For Each File In ofd.FileNames
   Image.FromFile(File).Save("C:\Users\Jonathan\Desktop\e\tmp.png", Imaging.ImageFormat.png)
Next

And on the line Image.FromFile(File).Save("C:\Users\Jonathan\Desktop\e\tmp.png", Imaging.ImageFormat.png)it comes up with the error.

在线上Image.FromFile(File).Save("C:\Users\Jonathan\Desktop\e\tmp.png", Imaging.ImageFormat.png)它出现了错误。

(note: the application will be built on so that's just my first code and it will need to be saved not copied)

(注意:应用程序将被构建,所以这只是我的第一个代码,需要保存而不是复制)

回答by Jay Riggs

I'd check two things:

我会检查两件事:

  1. That the directory you're saving to exists
  2. That you have write permissions to this directory
  1. 您要保存的目录存在
  2. 您对此目录有写权限

回答by Hans Passant

Opening or saving an Image puts a lock on the file. Overwriting this file requires you to first call Dispose() on the Image object that holds the lock.

打开或保存图像会锁定文件。覆盖此文件需要您首先对持有锁的 Image 对象调用 Dispose()。

I don't really understand your code but you'd have to do it this way:

我不太明白你的代码,但你必须这样做:

    For Each File In ofd.FileNames
        Using img As Image = Image.FromFile(File)
            img.Save("C:\Users\Jonathan\Desktop\e\tmp.png", Imaging.ImageFormat.Png)
        End Using
    Next

The Using statement ensures that the img object is disposed and the file lock is released.

Using 语句确保释放 img 对象并释放文件锁。

回答by Partha

The Image puts a lock.

图像锁定。

For example, i used this buffer images to save in to a memorystream.

例如,我使用此缓冲区图像保存到内存流中。

byte[] ImageData = new Byte[0];
if (BackGroundImage != null)
    {
        Bitmap BufferImage = new Bitmap(BackGroundImage);
        MemoryStream ImageStream = new MemoryStream();
        BufferImage.Save(ImageStream, ImageFormat.Jpeg);
        BufferImage.Dispose();
        ImageData = ImageStream.ToArray();
        ImageStream.Dispose();


        //write the length of the image data...if zero, the deserialier won't load any image
        DataStream.Write(ImageData.Length);
        DataStream.Write(ImageData, 0, ImageData.Length);
    }
    else
    {
        DataStream.Write(ImageData.Length);
    }

回答by Sawan

One reason of this is that the stream (MemoryStream or any other stream) that you have loaded the main image from has been disposed!

原因之一是您从中加载主图像的流(MemoryStream 或任何其他流)已被释放!

Such this case:

这种情况:

This is an extension method that converts a byte array to a bitmap, but using statement will dispose the memory stream, this will cause this error always:

这是一个将字节数组转换为位图的扩展方法,但是 using 语句会处理内存流,这将始终导致此错误:

    public static Bitmap ToBitmap(this byte[] bytes)
    {
        if (bytes == null)
        {
            return null;
        }
        else
        {
            using(MemoryStream ms = new MemoryStream(bytes))
            {
                return new Bitmap(ms);
            }
        }
    }