![](/img/trans.png)
[英]ExternalException (0x80004005) A generic error occurred in GDI+
[英]ASP.NET error on Bitmap.Save “Exception (0x80004005): A generic error occurred in GDI+.”
我有一個函數,它首先從磁盤讀取圖像,調整大小,然后保存到另一個目錄。
當我使用Bitmap.Save(目錄+ theimagename)時,它返回錯誤,如我在問題標題中所述。
我檢查了目錄是否正確,並且該目錄中不存在給定的圖像名稱。
奇怪的是,相同的代碼在本地機器上運行良好。 但是當我將它上傳到我的共享主機空間時,它只是不起作用。
代碼如下。
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();
堆棧跟蹤:
[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
來自ASP Net - 服務器上的GDI +和SAVE JPG或BMP
99.9%的情況下,當使用GDI時,“發生一般錯誤”意味着您嘗試保存的目錄沒有適當的權限。 通常,您需要確保該目錄允許ASP.NET修改文件。
你檢查了權限嗎?
當您嘗試保存網絡共享時也會發生此錯誤。 解決此問題的唯一解決方案是將位圖移動到內存流並將其與文件流一起保存到網絡共享。
這是一個可以輕松解決此問題的擴展方法:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.