I have a function which first reads an image from disk, resizes it and then saves to another directory.
when i use the Bitmap.Save(directory + theimagename) it returns the error as i stated in the question title.
i checked the directory is right, and the given image name doesn’t exist in that dir.
what is weird, is that the same code works great on the local machine. but when i upload it to my shared hosting space, it just doesn’t work.
the code is 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:InetpubvhostsDomainNamehttpdocsProductImages500pxgigabyte_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:inetpubvhostsbatuhanakcay.comhttpdocsApp_CodeUtilities.cs:181 Link.ToProductImage(String fileName) in c:inetpubvhostsbatuhanakcay.comhttpdocsApp_CodeLink.cs:79 Product.PopulateControls(ProductDetails pd) in c:inetpubvhostsbatuhanakcay.comhttpdocsProduct.aspx.cs:37 Product.Page_Load(Object sender, EventArgs e) in c:inetpubvhostsbatuhanakcay.comhttpdocsProduct.aspx.cs:20
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
From ASP Net – GDI+ and SAVE JPG or BMP on the server
99.9% of the time, when using GDI, ‘a generic error occured’ means that the
directory you are trying to save to
doesn’t have the proper permissions.
Typically, you need to make sure that the
directory is allowing ASP.NET to
modify files.
Did you check the permissions?
Method 2
This error also occurs when you try to save on a network share. The only solution to fix this is to move your bitmap to a memory stream and save it with a file stream to the network share.
Here’s an extension method to easily fix this:
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);
}
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0