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

发布于 2024-08-18 03:36:30 字数 436 浏览 4 评论 0原文

我需要在从 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) 上出现错误。

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

I need to save an image after opening it in from an OFD.
This is my code 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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笨死的猪 2024-08-25 03:36:30

我会检查两件事:

  1. 您要保存到的目录
    存在
  2. 您有写入权限
    这个目录

I'd check two things:

  1. That the directory you're saving to
    exists
  2. That you have write permissions to
    this directory
懷念過去 2024-08-25 03:36:30

打开或保存图像会锁定文件。覆盖此文件需要您首先对持有锁的 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 对象被释放并释放文件锁。

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.

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.

っ左 2024-08-25 03:36:30

图像加了一把锁。

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

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

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);
    }
柏林苍穹下 2024-08-25 03:36:30

原因之一是您加载主图像的流(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);
            }
        }
    }

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

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:

    public static Bitmap ToBitmap(this byte[] bytes)
    {
        if (bytes == null)
        {
            return null;
        }
        else
        {
            using(MemoryStream ms = new MemoryStream(bytes))
            {
                return new Bitmap(ms);
            }
        }
    }
掐死时间 2024-08-25 03:36:30

出现此问题似乎是因为 Image.Drawing.FromFile() 方法锁定了打开的文件!

我遇到了同样的问题,并使用以下解决方法解决了此问题。

Public sFileName as string
Public img as Drawing.Image

Public Sub OpenFile(ByVal sFile As String)
    img = System.Drawing.Image.FromFile(sFile)
    sFileName = sFile
End Sub

Public Sub Update()
    Try
        img.Save("NEW.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
        img.Dispose
        System.IO.File.Delete(sFileName)
        System.IO.File.Move("NEW.jpeg", sFileName)
    Catch ex As Exception
    EndTry
End Sub

解决方法包括将

  1. 修改后的图像保存在另一个名为 NEW.jpeg 的文件中,
  2. 处置 img 对象以释放它
  3. ,将创建的文件重命名为原始文件名

。 code> 和 Update() 方法,我调用一个特定的方法来更改照片的拍摄日期。

Public Sub SetDateTime(ByVal sDate As String)

    Dim ascii As New System.Text.ASCIIEncoding
    Dim propItem As System.Drawing.Imaging.PropertyItem

    propItem = img.GetPropertyItem(&H132) 'PropertyTagDateTime

    propItem.Value = ascii.GetBytes(sDate)
    propItem.Value(19) = 0

    img.SetPropertyItem(propItem)
    propItem.Id = &H9003
    img.SetPropertyItem(propItem)
    propItem.Id = &H9004
    img.SetPropertyItem(propItem)
    propItem = Nothing

End Sub

这是当相机日期未正确初始化时重命名照片的拍摄日期的程序的一部分。

This problem seems occuring because Image.Drawing.FromFile() method lock opened file !

I have encountered same problem and I have resolved this issue using following workaround.

Public sFileName as string
Public img as Drawing.Image

Public Sub OpenFile(ByVal sFile As String)
    img = System.Drawing.Image.FromFile(sFile)
    sFileName = sFile
End Sub

Public Sub Update()
    Try
        img.Save("NEW.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
        img.Dispose
        System.IO.File.Delete(sFileName)
        System.IO.File.Move("NEW.jpeg", sFileName)
    Catch ex As Exception
    EndTry
End Sub

The workaround consist to

  1. save modified image in another file named here NEW.jpeg
  2. dispose img object to release it
  3. rename created file to original filename

Between OpenFile() and Update() methods, I call a specific method to change capture's date of a photo.

Public Sub SetDateTime(ByVal sDate As String)

    Dim ascii As New System.Text.ASCIIEncoding
    Dim propItem As System.Drawing.Imaging.PropertyItem

    propItem = img.GetPropertyItem(&H132) 'PropertyTagDateTime

    propItem.Value = ascii.GetBytes(sDate)
    propItem.Value(19) = 0

    img.SetPropertyItem(propItem)
    propItem.Id = &H9003
    img.SetPropertyItem(propItem)
    propItem.Id = &H9004
    img.SetPropertyItem(propItem)
    propItem = Nothing

End Sub

This is part of a program that rename capture's date of photos when camera's date has not been initalized correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文