当前位置:编程学习 > C#/ASP.NET >>

急求c#实现图片上传,然后可以截取图片大小,传到数据库,然后显示的效果怎么做呀?有源码可参照吗??

急求c#实现图片上传,然后可以截取图片大小,传到数据库,然后显示的效果怎么做呀?有源码可参照吗??
谢谢!! --------------------编程问答-------------------- http://www.cnblogs.com/zhongweiv/archive/2011/11/29/2266544.html

http://www.cnblogs.com/flyinghigher/archive/2012/06/21/2557706.html --------------------编程问答-------------------- www.51aspx.com --------------------编程问答-------------------- 人懒没得救啊............ --------------------编程问答-------------------- 不错,貌似楼主比我还要懒,搜索都懒得用 --------------------编程问答-------------------- http://www.helloweba.com/view-blog-51.html 
之前用过这个 
后台大概是这样
  //获取jquery pass 来的数据
        //根据数据模拟结果, 然后绘图
        //画一个模板
        //然后根据所有的数据 ,来复制原图
        //img, new Rectangle(imgx,imgy, width, height),
        // new Rectangle(0, 0, copywidth, copyheight), GraphicsUnit.Pixel);
        //以上是最关键的8组数据 画板(x,y,width,height) 简称1,2,3,4 , 这是paste的方位和大小
        //(x,y,width,height) 5,6,7,8 这是复制原图从方位和大小。 
        //这些如果有调角度的话都会被影响 , 必须用sin cos 等等来计算后才dropimage 
        try
        {
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            context.Response.ContentType = "text/plain";
            int ID = Convert.ToInt32(context.Request["ID"]);
            decimal imageH = decimal.Parse(context.Request["imageH"]); //image 高 ,
            decimal imageW = decimal.Parse(context.Request["imageW"]); //image 宽 , 
            decimal angle = decimal.Parse(context.Request["imageRotate"]); //角度
            string img_source = context.Request["imageSource"];
            decimal imageX = decimal.Parse(context.Request["imageX"]); //image角 和div x 的距离
            decimal imageY = decimal.Parse(context.Request["imageY"]); //image角 和div y 的距离
            decimal selectorH = decimal.Parse(context.Request["selectorH"]); //框框的高 ,内:画板的高
            decimal selectorW = decimal.Parse(context.Request["selectorW"]); //框框的宽 ,内:画板的宽
            decimal selectorX = decimal.Parse(context.Request["selectorX"]); //框框 和 div x的距离
            decimal selectorY = decimal.Parse(context.Request["selectorY"]); //框框 和 div y的距离 
            decimal viewPortH = decimal.Parse(context.Request["viewPortH"]); //div 的高
            decimal viewPortW = decimal.Parse(context.Request["viewPortW"]); //div 的宽
            decimal dwidth = decimal.Parse(context.Request["dwidth"]);  //原图的宽
            decimal dheight = decimal.Parse(context.Request["dheight"]); //原图的高

            int ipos = img_source.LastIndexOf(".");

            string nojpg = img_source.Substring(0, ipos - 6);

            string jpg = img_source.Substring(ipos);

            string newpath = nojpg + ".jpg";
            newpath = newpath.Replace("http://www.stooges.com.my", "~");//today
            newpath = newpath.Replace("/", "\\");

            img_source = img_source.Replace("http://www.stooges.com.my", "~");//today 

            /*改路径*/


            //画板的尺寸 
            string w = selectorW.ToString();
            int finalw = int.Parse(w);
            string h = selectorH.ToString();
            int finalh = int.Parse(h);
            //最终x 1
            decimal x = imageX - selectorX;
            x = Decimal.Round(x, 0);
            string imgx1 = x.ToString();
            int imgx = int.Parse(imgx1);
            //最终 y 2 
            decimal y = imageY - selectorY;
            y = Decimal.Round(y, 0);
            string imgy1 = y.ToString();
            int imgy = int.Parse(imgy1);
            //最终width 3
            imageW = Decimal.Round(imageW, 0);
            string width2 = imageW.ToString();
            int width = int.Parse(width2);
            //最终height 4
            imageH = Decimal.Round(imageH, 0);
            string height1 = imageH.ToString();
            int height = int.Parse(height1);
            //copy height 也等于原图的尺寸   7
            dheight = Decimal.Round(dheight, 0);
            string dheight1 = dheight.ToString();
            int copyheight = int.Parse(dheight1);
            //copy width 也等于原图的尺寸  8
            dwidth = Decimal.Round(dwidth, 0);
            string dwidth1 = dwidth.ToString();
            int copywidth = int.Parse(dwidth1);
            //角度处理
            angle = Decimal.Round(angle, 0);
            string angle1 = angle.ToString();
            int jiaodu = int.Parse(angle1);

            if (angle > 0 && angle < 360)  //如果有角度的话
            {

                double radian = jiaodu * Math.PI / 180.0;
                double cos = Math.Cos(radian);
                double sin = Math.Sin(radian);
                //原图的宽和高
                //取得新的 3,4 
                int finalwidth = (int)(Math.Max(Math.Abs(width * cos - height * sin), Math.Abs(width * cos + height * sin)));
                int finalheight = (int)(Math.Max(Math.Abs(width * sin - height * cos), Math.Abs(width * sin + height * cos)));

                //取得新的 7,8 
                int finalcopywidth = (int)(Math.Max(Math.Abs(copywidth * cos - copyheight * sin), Math.Abs(copywidth * cos + copyheight * sin)));
                int finalcopyheight = (int)(Math.Max(Math.Abs(copywidth * sin - copyheight * cos), Math.Abs(copywidth * sin + copyheight * cos)));
                //获取新的 1,2
                int finalimgx = imgx - ((finalwidth - width) / 2);
                int finalimgy = imgy - ((finalheight - height) / 2);

                imgx = finalimgx;
                imgy = finalimgy;
                width = finalwidth;
                height = finalheight;
                copywidth = finalcopywidth;
                copyheight = finalcopyheight;
            }


            jiaodu = -jiaodu; //弧度转换要-
            Bitmap img = (Bitmap)Bitmap.FromFile(context.Server.MapPath(img_source));
            //  img = Rotate(img, jiaodu); 
            //调整原图角度来复制
            jiaodu = jiaodu % 360; 
            double radian1 = jiaodu * Math.PI / 180.0;
            double cos1 = Math.Cos(radian1);
            double sin1 = Math.Sin(radian1);
            //原图的宽和高
            int w1 = img.Width;
            int h1 = img.Height;
            int W = (int)(Math.Max(Math.Abs(w1 * cos1 - h1 * sin1), Math.Abs(w1 * cos1 + h1 * sin1)));
            int H = (int)(Math.Max(Math.Abs(w1 * sin1 - h1 * cos1), Math.Abs(w1 * sin1 + h1 * cos1)));
            //目标位图
            Bitmap dsImage = new Bitmap(W, H);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //计算偏移量
            Point Offset = new Point((W - w1) / 2, (H - h1) / 2);
            //构造图像显示区域:让图像的中心与窗口的中心点一致
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w1, h1);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(360 - jiaodu);
            //恢复图像在水平和垂直方向的平移
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(img, rect);
            //重至绘图的所有变换
            g.ResetTransform();
            g.Save();
            g.Dispose();


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

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

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

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

            //清空画布并以透明背景色填充
            g2.Clear(Color.White);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g2.DrawImage(dsImage, new Rectangle(imgx, imgy, width, height),// x,y ,最终的宽,高
                new Rectangle(0, 0, copywidth, copyheight), GraphicsUnit.Pixel);


            System.Drawing.Imaging.ImageCodecInfo encoder = GetEncoderInfo("image/jpeg");//keatkeat

            System.Drawing.Imaging.EncoderParameters encoderParams =
                          new System.Drawing.Imaging.EncoderParameters(1);

            //System.Drawing.Imaging.Encoder 对象封装一个全局唯一标识符 (GUID),它标识图像编码器参数的类别。
            //设置 jpeg 质量为 60,注意应该为 long 类型    /*质量90*/
            encoderParams.Param[0] =
                new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

            bitmap.Save(context.Server.MapPath(newpath), encoder, encoderParams);
            dsImage.Dispose();
            g.Dispose();
            bitmap.Dispose();
            img.Dispose();
            g2.Dispose();
            newpath = newpath.Replace("\\", "/");
            newpath = newpath.Replace("~", "http://www.stooges.com.my");//today
            context.Response.Write(newpath);
        }
        catch (Exception ex)
        {
            string a = ex.Message;
        }


 private System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(string mimeType)
    /*用于压图片*/
    {
        try
        {
            System.Drawing.Imaging.ImageCodecInfo result = null;

            //检索已安装的图像编码解码器的相关信息。
            System.Drawing.Imaging.ImageCodecInfo[] encoders =
                System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
            for (int i = 0; i < encoders.Length; i++)
            {
                if (encoders[i].MimeType == mimeType)
                {
                    result = encoders[i];
                    break;
                }
            }
            return result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }



前台是个plug in 可以剪图 和 旋转 ,然后传值去后台(尺寸角度等等),后台根据前台传来的值来剪和旋转等等,然后在保存新的一张图 。 
这是我几个月前学习的时候做过的。不过现在是直接在前台剪了用 canvas 生产再直接上传,后台就保存图片即可,只是怕旧的游览器不兼容罢了。有需要才找我拿吧
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,