HAVIT Knowledge Base

Vývoj webových aplikací, .NET, SQL, návrh
Welcome to HAVIT Knowledge Base Sign in | Join | Help
-
Home Články Forums Obrázky Soubory

.NET Framework

Microsoft .NET Framework, Base Class Library

ExternalException: A generic error occurred in GDI+. při Image.Save()

Tuto krásnou výjimku dostaneme, pokud chceme Image ukládat do stejného souboru, z kterého byl načten. GDI+ to prostě nedovoluje.

Dá se to ale obejít například zkopírováním obrázku do nové instance:

Bitmap bitmap;
using (Image image = Image.FromFile(sourceFilename))
{
   // načteme si obrázek do bitmapy, abychom mohli zavřít soubor
   bitmap = new Bitmap(image);
}
...
bitmap.Save(...); // tady už to nevadí, přetrhli jsme vazbu na soubor
Published 3. května 2006 15:59 by Robert Haken
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Rudolf Dvořáček said:

Stejná výjimka nastane i v okamžiku, kdy chceme ukládat ve web aplikaci obrázek ve formátu PNG do Response.OutputStreamu. Proměnná outputImage obsahuje

               HttpResponse currentResponse = _context.Response;

               const string CONTENT_TYPE_PNG = "image/png";

               currentResponse.ContentType = CONTENT_TYPE_PNG;

               Stream outputStream = currentResponse.OutputStream;

               outputImage.Save(outputStream, ImageFormat.Png); <--zde dojde k výjimce

               outputImage.Dispose();

Řešením je vytvoření MemoryStreamu, zápis obrázku do MemoryStreamu a až poté do OutputStreamu

               HttpResponse currentResponse = _context.Response;

               const string CONTENT_TYPE_PNG = "image/png";

               currentResponse.ContentType = CONTENT_TYPE_PNG;

               MemoryStream temporaryStream = new MemoryStream();

               outputImage.Save(temporaryStream, ImageFormat.Png);

               Stream outputStream = currentResponse.OutputStream;

               temporaryStream.WriteTo(outputStream);

               outputImage.Dispose();

               temporaryStream.Dispose();

května 9, 2011 16:03

What do you think?

(required) 
(optional)
(required) 
Enter the code you see below

Submit