2011-05-17 20 views
1

你好,我有我的调整大小和上传img到服务器的问题。一切都很好,但今天告诉我的朋友,当他想要添加img到服务器时,他得到了“在GDI +中发生了一个通用错误”。但是在我的电脑中一切正常。那么可以用IIS的问题? (两天前他有一些问题,所以管理员在服务器上改变了一些东西)。GDI +发生一般性错误。 (只有当我尝试在服务器上)

Bitmap image = KTEditImage.ResizeImage(new Bitmap(file.PostedFile.InputStream), 360, 360); 
image.Save(Server.MapPath("~") + "/Static/Img/Zbozi/" + urlName, ImageFormat.Jpeg); 
image.Dispose(); 
Bitmap smallImage = KTEditImage.ResizeImage(new Bitmap(file.PostedFile.InputStream), 230, 230);       
smallImage.Save(Server.MapPath("~") + "/Static/Img/Zbozi/Small/" + urlName, ImageFormat.Jpeg); 
smallImage.Dispose(); 

和调整方法是

public static Bitmap ResizeImage(Bitmap image, int maxWidth, int maxHeight) 
{ 
    return new Bitmap(image, maxWidth, maxHeight); 
} 
+0

我想这是一种特权相关问题。你能检查目标文件夹的所有者是否与IIS正在运行的用户相同吗? – alexn 2011-05-17 18:33:07

+0

我可以问我该如何检查? – John 2011-05-17 18:36:20

+0

我目前没有Windows机器可用,但是如果我没有弄错,您可以右键单击文件夹,属性,安全性,高级,然后选择所有者选项卡。然后你可以看到所有者。 – alexn 2011-05-17 18:37:16

回答

5

授予写权限在目标目录ASPNET帐户(Windows XP)或NETWORK SERVICE帐户(在Windows Server 2003/2008/Vista/7),

+3

如果网站在IIS 7上运行,授予权限的帐户取决于分配给运行该网站的应用程序池的帐户。它并不总是网络服务,并且在大多数情况下,网络服务不是默认设置。 – esteuart 2011-05-18 18:23:29

0

这可能是一个权限问题与文件保存到该路径。您需要确保“/ Static/Img/Zbozi”和“/ Static/Img/Zbozi/Small”目录允许匿名用户保存文件。

+0

允许匿名用户在文件夹上写入必须避免,并且会带来安全问题。 – 2011-05-18 07:11:48

0

此问题是由于Net框架来处理图像资源,请使用GC.Collect();代码后

1

我有类似于这个问题的地方,我调整了一个图像的大小,并用调整大小的版本替换原来的。我发现GDI + Exception是因为导致问题的图像是只读,并且无法被覆盖。

我在下面的代码中的目标是调整超过最大文件大小的图像大小。

for (int i = 0; i < LoadedImgs.Length; i++) 
{ 
    info = new FileInfo(LoadedImgs[i].origImgFullPath); 
    double sizeMB = Math.Round(((double)info.Length/1048576.0), MidpointRounding.AwayFromZero); 
    if (sizeMB > (double)numImgMaxSize.Value) 
    { 
     Bitmap bmpOrigImg = new Bitmap(LoadedImgs[i].origImgFullPath); 
     Bitmap bmpResizeImg = null; 

     bmpResizeImg = ImageUtilities.Resize(bmpOrigImg, sizeMB); 

     #region Save the resized image over the original 
     bmpOrigImg.Dispose();       
     bmpResizeImg.Save(LoadedImgs[i].origImgFullPath); 
     bmpResizeImg.Dispose(); 
     #endregion 
    } 
} 

而且,我调整算法到一个特定的文件大小需要调整(不包括位压缩等),但在共享的名称:

Bitmap origBmp = new Bitmap(image.Width, image.Height); 
double ratio = (double)image.Width/(double)image.Height; 

double bitDepth = 32.0;//Output BMP default 
double newHeight = Math.Sqrt((1024.0 * 1024.0 * 8.0)/(ratio * bitDepth)); 

int height = (int)Math.Round(newHeight, MidpointRounding.AwayFromZero); 
int width = (int)Math.Round(newHeight * ratio); 
相关问题