大众计算机学习网

专门提供各种电脑教程

修改上传图片的尺寸大小之NET2.0代码

Admin | 2009-3-16 13:39:38 | 被阅次数 | 6441

  以下代码为网络高手(浩辉)提供,感谢其人对本站的支持:

  代码实现的功能是:使用ASP.NET2.0上传图片,同时修改图片的尺寸大小;

/// <summary> 
/// 修改指定图片的分辨率的方法 
/// </summary> 
/// <param name="fileFoldUrl">文件夹url </param> 
/// <param name="fileName">文件名 </param> 
/// <param name="filePath">文件路径,带文件名 </param> 
/// <param name="_width">分辨率的宽 </param> 
/// <param name="_height">分辨率的高 </param> 
public void update_picture(string fileFoldUrl, string fileName, string filePath, int _width, int _height) 

    byte[] zp = this.load_pictMemory(filePath); 
 
    MemoryStream ms = new MemoryStream(zp); 
 
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 
 
    Bitmap btp = new Bitmap(img, _width, _height); 
 
    DirectoryInfo dti = new DirectoryInfo(fileFoldUrl); 
 
    FileInfo[] fis = dti.GetFiles(); 
 
    string fileUrl = fileFoldUrl + fileName; 
 
    btp.Save(fileUrl); 

 
/// <summary> 
/// 获取指定文件流的字节大小 
/// </summary> 
/// <param name="filePath">文件路径 </param> 
/// <returns>byte[] </returns> 
public byte[] load_pictMemory(string filePath) 

    byte[] pictData = null; 
 
    FileInfo fi = new FileInfo (filePath); 
 
    if (fi.Exists) 
    { 
        pictData = new byte[fi.Length]; 
 
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite); 
 
        BinaryReader br = new BinaryReader(fs); 
 
        br.Read(pictData, 0, Convert.ToInt32(fi.Length)); 
 
        fs.Dispose(); 
    } 
    else 
    { 
        HttpContext.Current.Response.Write(" <script language='javascript'>alert('没有找到你所指定的图片') </script>"); 
    } 
    return pictData; 
}
//上面的方法直接调用即可!


-----
//文件上传部分的代码:在按钮事件中:
            try
            {
if (Fupload.PostedFile.FileName == "")
                    return "";
                string dir = DateTime.Now.ToString("yyyyMM");
                if (!Directory.Exists(Server.MapPath("~/uploadpic/") + dir))
                {
                    Directory.CreateDirectory(Server.MapPath("~/uploadpic/") + dir);
                    if (!Directory.Exists(Server.MapPath("~/uploadpic/") + dir))
                        return "";
                }
                Random rd = new System.Random();
               string fileFoldUrl;
             string filepath;
             string filename;
             string extname;

             if (Fupload.PostedFile.FileName != "")
                {
                    extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
                    if ("JPG|GIF|BMP|PNG".IndexOf(extname) == -1)
                    {
                        return "";
                    }

                    filename = dir + "/" + DateTime.Now.ToString("yyyyMM") + rd.Next(1000).ToString() + "." + extname;
                    fileFoldUrl = Server.MapPath("~/uploadpic/") ;
//这个地方的路径设置很重要,由于这里上传的是服务器端代码,因此只能用"~/uploadpic/",而非:"..\\uploadpic\\"
                    filepath = Server.MapPath("~/uploadpic/") + filename;
                    Fupload.PostedFile.SaveAs(filepath);
                    update_picture(filefoldurl, filename,filepath, 400, 300); //调用修改图片的方法。                         
                    return filename;
                }
                return "";

            }
            catch { return ""; }