繁体   English   中英

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

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

我需要在从OFD打开后保存图像。 这是我的代码atm:

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

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

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

我检查两件事:

  1. 您要保存的目录存在
  2. 您拥有此目录的写权限

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

我真的不了解你的代码,但你必须这样做:

    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

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

图像放锁。

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

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);
    }

这样做的一个原因是您已经处理了从主图像加载的流(MemoryStream或任何其他流)!

这样的情况:

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

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

暂无
暂无

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

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