KimhillZhang  

这几天在做一个需求,要求上传两张一样的图片,但大小不一;之前用两个<input type="file" />控件来分别上传图片;一个同事(Zendic[http://www.z-endic.info/])说好像可以为一张大图片生成小图片;那时我就上网找了些资料,发现真的可以;把网上实现的类拷贝下来修改了下结果可以实现;

现在把整个实现过程写一下:

创建一个PublicMethods类,在类里写以下代码:

 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case "HW"://指定高宽缩放(可能变形)
                    break;
                case "W"://指定宽,高按比例
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut"://指定高宽裁减(不变形)
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);

            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

 在后台写一个上传的函数:

     public void uploadFile(HtmlInputFile imgfile)
        {
            string str = imgfile.PostedFile.FileName;
            if (str == string.Empty)
            {
                return;
            }
            else
            {
                string filename = System.IO.Path.GetFileName(str);
                string filename_s = DateTime.Now.ToString("yyyyMMddHHmmss") + filename;//缩图的文件名

                string webFilePath = Server.MapPath("..\\images\\" + filename); //上传大图片
                string webFilePath_s = Server.MapPath("..\\images\\" + filename_s); //上传小图片

                if (!File.Exists(webFilePath))
                {
                    imgfile.PostedFile.SaveAs(webFilePath);
                    PublicMethods.MakeThumbnail(webFilePath, webFilePath_s, 50, 50, "Cut"); //调用缩略图的方法
                }

               //下面的操作是为了写入数据库而操作的
                string path = "..\\images\\" + filename;
                string path_s = "..\\images\\" + filename_s;
                string imgurl = path.ToString().Replace("\\", "/");
                string imgurl_s = path_s.ToString().Replace("\\", "/");

               //两图片在项目里的相对路径分别保存到两个的Label控件里
                label.Text = imgurl;
                label_s.Text = imgurl_s;
            }
        }

在前台写上传控件时得把客户端的写成服务端的

<input name="imgfile" type="file" id="imgfile" runat="server" size="40" />

最后在后台调用:

uploadFile(imgfile);就OK了;

posted on 2010-05-08 13:20  KimhillZhang  阅读(360)  评论(0编辑  收藏  举报