Domanda

La mia applicazione ASP.NET ha un ritaglio delle immagini e funzioni di ridimensionamento. Ciò richiede che l'immagine caricata temporanea essere cancellata. Tutto funziona bene, ma quando provo a cancellare un'immagine più grande di 80px da 80px ottengo un "file è bloccato da un altro processo ..." l'errore, anche se ho rilasciato tutte le risorse.

Ecco un frammento:

System.Drawing.Image tempimg = System.Drawing.Image.FromFile(temppath);
System.Drawing.Image img = (System.Drawing.Image) tempimg.Clone(); //advice from another forum
tempimg.Dispose();

img = resizeImage(img, 200, 200); //delete only works if it's 80, 80
img.Save(newpath);
img.Dispose();

File.Delete(temppath);
È stato utile?

Soluzione

Credo che non si sta disponendo l'immagine prima istanza assegnato alla variabile img.

Considerate questo, invece:

System.Drawing.Image tempimg = System.Drawing.Image.FromFile(temppath);
System.Drawing.Image img = (System.Drawing.Image) tempimg.Clone();
tempimg.Dispose();

System.Drawing.Image img2 = resizeImage(img, 200, 200);
img2.Save(newpath);
img2.Dispose();
img.Dispose();

File.Delete(temppath);

Altri suggerimenti

Se si crea l'immagine in questo modo, non sarà bloccato:

using (FileStream fs = new FileStream(info.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    byte[] data = new byte[fs.Length];
                    int read = fs.Read(data, 0, (int)fs.Length);
                    MemoryStream ms = new MemoryStream(data, false);
                    return Image.FromStream(ms, false, false); // prevent GDI from holding image file open
                }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top