将图片等比例缩小不影响图片的分辨率

#region 生成缩略图
    /// 
    /// 生成缩略图
    /// 
    /// 传入图片的路径
    private void SaveImage(string filePath)
    {
        string fileName = Path.GetExtension(File1.PostedFile.FileName).ToLower();
        string _filename = DateTime.Now.ToString("yyMMddHHmm");

        System.Drawing.Image image = null, aNewImage = null;

        //下面是生成缩略图 
        int newwidth = 0, newheight = 0;
        image = System.Drawing.Image.FromFile(filePath);
        System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

        //取高和宽   
        int phWidth = image.Width;
        int phHeight = image.Height;

        //设置缩略的宽度和高度
        if (phWidth > 110)
        {
            //指定宽度
            newwidth = 110;
            //根据宽度算出高度
            newheight = phHeight * newwidth / phWidth;
        }
        aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
        //将缩略图重新命名并保存
        aNewImage.Save(Server.MapPath("~/upload/") + "Avaster_" + DateTime.Now.ToString("yyMMddHHmm") + fileName);
        image.Dispose();
    }

    #endregion

你可能感兴趣的:(ASP.NET)