回 帖 发 新 帖 刷新版面

主题:.net生成縮略圖

/// <summary>
/// 添加遊戲信息
/// </summary>
private void AddGameInfo()
{
if(dp.CheckData("select * from GameInfo where GameName='"+txtGameName.Text+"'"))
            {
                Response.Write("<script language=javascript>alert('已經存在該遊戲的信息了!');</script>");
                return;
            }
            string fileFullName=GamePhoto.PostedFile.FileName;//文件名全名
            string fileName=fileFullName.Substring(fileFullName.LastIndexOf("\\")+1);//文件名
            string fileType=fileFullName.Substring(fileFullName.LastIndexOf(".")+1);//文件類型
            
            if(fileType=="jpg"||fileType=="bmp"||fileType=="gif")
            {
                string strFile=MapPath("../UpLoadPhoto")+"\\"+fileName;
                //Img.ImageUrl=strFile;
                GamePhoto.PostedFile.SaveAs(strFile);
                
                //生成縮略圖
                CreatePhoto(strFile);
                try
                {
                    dp.RunProcedure("usp_InsertGameInfo",new SqlParameter[]{new SqlParameter("@GameName",txtGameName.Text),new SqlParameter("@GamePhoto",strFile),new SqlParameter("@JieShaoContent",txtContent.Text)});
                    Response.Write("<script language=javascript>alert('操作成功!');</script>");
                }
                catch
                {
                    Response.Write("<script language=javascript>alert('操作失敗,請與管理員聯系!');</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('文件類型有誤!');</script>");
            }
            
        }

        

        /// <summary>
        /// 生成縮略圖
        /// </summary>
        /// <param name="file">圖片路徑</param>
        private void CreatePhoto(string file)
        {
            int width, height,newWidth,newHeight;
            System.Drawing.Image image,newImage;
            
            System.Drawing.Image.GetThumbnailImageAbort callb=null;
            image=System.Drawing.Image.FromFile(file);
            
            width=image.Width;
            height=image.Height;

            if(width>height)
            {
                newWidth=110;
                newHeight=image.Height/image.Width*newWidth;
            }
            else
            {
                newHeight=110;
                newWidth=image.Width/image.Height*newHeight;
            }
            
            
                newImage=image.GetThumbnailImage(newWidth,newHeight,callb,new System.IntPtr());
            
            
                file=file.Insert(file.LastIndexOf("\\")+1,"001");
              
                newImage.Save(file);
                Img.ImageUrl=file;
                image.Dispose();
                newImage.Dispose();
                
        
        }

上面會出現內存不足,請高手幫忙

回复列表 (共1个回复)

沙发

你说的问题我也遇到过,我个人觉得
   if(width>height)
            {
                newWidth=110;
                newHeight=image.Height/image.Width*newWidth;
            }
            else
            {
                newHeight=110;
                newWidth=image.Width/image.Height*newHeight;
            }
            
是这个地方出现了错误了,造成内存占用过大

我来回复

您尚未登录,请登录后再回复。点此登录或注册