ASP.NET error in Bitmap.Save "Exception (0x80004005): A general error occurred in GDI +."

I have a function that first reads an image from a disk, resizes it and then saves it to another directory.

when I use Bitmap.Save (directory + theimagename), it returns an error, as I stated in the title of the question.

I checked that the directory is right and the given image name does not exist in this directory.

What is strange is that the same code works fine on the local machine. but when I upload it to my shared hosting, it just doesn't work.

code below.

bmpOut = new Bitmap(Size, Size); Graphics g = Graphics.FromImage(bmpOut); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.White, 0, 0, Size, Size); int topBottomPadding = 0; int leftRightPadding = 0; if (Size > lnNewWidth + 1) leftRightPadding = Convert.ToInt32((Size - lnNewWidth) / 2); else if (Size > lnNewHeight + 1) topBottomPadding = Convert.ToInt32((Size - lnNewHeight) / 2); g.DrawImage(loBMP, leftRightPadding, topBottomPadding, lnNewWidth, lnNewHeight); Bitmap bmp = new Bitmap(bmpOut); if (bmp != null) bmp.Save(ResizedOutput); // C:\Inetpub\vhosts\DomainName\httpdocs\ProductImages\500px\gigabyte_ga_ep45_ds4_profilelarge[1].jpg bmp.Dispose(); bmpOut.Dispose(); g.Dispose(); loBMP.Dispose(); 

stack trace:

 [ExternalException (0x80004005): A generic error occurred in GDI+.] System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630 System.Drawing.Image.Save(String filename, ImageFormat format) +69 System.Drawing.Image.Save(String filename) +25 Utilities.ResizeImage(String fileName, String mode) in c:\inetpub\vhosts\batuhanakcay.com\httpdocs\App_Code\Utilities.cs:181 Link.ToProductImage(String fileName) in c:\inetpub\vhosts\batuhanakcay.com\httpdocs\App_Code\Link.cs:79 Product.PopulateControls(ProductDetails pd) in c:\inetpub\vhosts\batuhanakcay.com\httpdocs\Product.aspx.cs:37 Product.Page_Load(Object sender, EventArgs e) in c:\inetpub\vhosts\batuhanakcay.com\httpdocs\Product.aspx.cs:20 
+6
c # bitmap gdi + drawing
source share
2 answers

From ASP Net - GDI + and SAVE JPG or BMP on the server

99.9% of the time when using GDI, a โ€œgeneral error occurredโ€ means that the directory you are trying to save does not give the correct permissions. Typically, you need to make sure that the directory allows asp.net to modify files.

Have you checked permissions?

+12
source share

This error also occurs when trying to save a network share. The only solution for this is to move the bitmap to the memory stream and save it with the file stream to a network share.

It uses an extension method that is easy to fix:

 public static void SaveOnNetworkShare(this System.Drawing.Image aImage, string aFilename, System.Drawing.Imaging.ImageFormat aImageFormat) { using (System.IO.MemoryStream lMemoryStream = new System.IO.MemoryStream()) { aImage.Save(lMemoryStream, aImageFormat); using (System.IO.FileStream lFileStream = new System.IO.FileStream(aFilename, System.IO.FileMode.Create)) { lMemoryStream.Position = 0; lMemoryStream.CopyTo(lFileStream); } } } 
+2
source share

All Articles