Saving image. A common error occurred in GDI +. (Vb.net)

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

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 Image.FromFile(File).Save("C:\Users\Jonathan\Desktop\e\tmp.png", Imaging.ImageFormat.png)an error appears on the line .

(note: the application will be built in such a way that only my first code will need to be saved and not copied)

+3
source share
4 answers

I would check two things:

  • So that the directory you save exists
  • You have write permissions for this directory.
+16
source

Opening or saving the image places the lock in the file. To overwrite this file, you must first call Dispose () on the Image object that contains the lock.

, :

    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 , .

+6

.

, memystream.

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

, (MemoryStream ), , !

:

, , , :

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

Source: https://habr.com/ru/post/1728201/


All Articles