예제 #1
0
        private void UploadImage()
        {
            System.Web.UI.WebControls.Image image  = null;
            System.Web.UI.WebControls.Image image2 = null;
            Bitmap       bitmap   = null;
            Graphics     graphics = null;
            MemoryStream stream   = null;

            try
            {
                HttpPostedFile file = base.Request.Files["Filedata"];
                string         str  = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo);
                string         str2 = "/Storage/master/reply/";
                string         str3 = str + Path.GetExtension(file.FileName);
                file.SaveAs(Globals.MapPath(str2 + str3));
                base.Response.StatusCode = 200;
                base.Response.Write(str2 + str3);
            }
            catch (Exception)
            {
                base.Response.StatusCode = 500;
                base.Response.Write("服务器错误");
                base.Response.End();
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
                if (graphics != null)
                {
                    graphics.Dispose();
                }
                if (image2 != null)
                {
                    image2.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                }
                base.Response.End();
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        protected override void CreateChildControls()
        {
            bool isInUpdatePanel = this.IsInUpdatePanel;


            //Page p = (Page)System.Web.HttpContext.Current.Handler;
            //ScriptManager m = ScriptManager.GetCurrent(p);



            ClientScriptManager cs = Page.ClientScript;

            if (this.IncludeJQuery)
            {
                if (string.IsNullOrEmpty(this.ScriptPath))
                {
                    this.ScriptPath = "/scripts/";
                }

                if (isInUpdatePanel)
                {
                    if (!cs.IsClientScriptIncludeRegistered(this.GetType(), "jquery"))
                    {
                        ScriptManager.RegisterClientScriptInclude(this, this.GetType()
                                                                  , "jquery", ResolveClientUrl(this.ScriptPath + "jquery.js"));
                    }

                    if (!cs.IsClientScriptIncludeRegistered(this.GetType(), "jqueryCrop"))
                    {
                        ScriptManager.RegisterClientScriptInclude(this, this.GetType()
                                                                  , "jqueryCrop", ResolveClientUrl(this.ScriptPath + "jquery.jcrop.js"));
                    }
                }
                else
                {
                    if (!cs.IsClientScriptIncludeRegistered(this.GetType(), "jquery"))
                    {
                        cs.RegisterClientScriptInclude(this.GetType()
                                                       , "jquery", ResolveClientUrl(this.ScriptPath + "jquery.js"));
                    }

                    if (!cs.IsClientScriptIncludeRegistered(this.GetType(), "jqueryCrop"))
                    {
                        cs.RegisterClientScriptInclude(this.GetType()
                                                       , "jqueryCrop", ResolveClientUrl(this.ScriptPath + "jquery.jcrop.js"));
                    }
                }
            }
            else
            {
                // load it from google ajax library. Faster way and less annoying!

                /*if(!cs.IsClientScriptIncludeRegistered("jquery"))
                 *  cs.RegisterClientScriptInclude("jquery"
                 *      , "http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js");
                 */

                string jquery = "\r\n<script src=\"" + cs.GetWebResourceUrl(this.GetType(),
                                                                            "CS.Web.UI.jquery.js") + "\" type=\"text/javascript\"></script>\r\n";

                string cropJS = "\r\n<script src=\"" + cs.GetWebResourceUrl(this.GetType(),
                                                                            "CS.Web.UI.jquery.jcrop.js") + "\" type=\"text/javascript\"></script>\r\n";


                if (isInUpdatePanel)
                {
                    if (!cs.IsClientScriptBlockRegistered("jquery"))
                    {
                        cs.RegisterClientScriptBlock(this.GetType(), "jquery", jquery, false);
                    }

                    if (!cs.IsClientScriptBlockRegistered("cropJS"))
                    {
                        cs.RegisterClientScriptBlock(this.GetType(), "cropJS", cropJS, false);
                    }
                }
                else
                {
                    if (!cs.IsClientScriptBlockRegistered("jquery"))
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery", jquery, false);
                    }

                    if (!cs.IsClientScriptBlockRegistered("cropJS"))
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "cropJS", cropJS, false);
                    }
                }
            }



            string cropCss = "<link href=\"" + cs.GetWebResourceUrl(this.GetType(),
                                                                    "CS.Web.UI.jquery.jcrop.css") + "\" type=\"text/css\" rel=\"stylesheet\" />\r\n";

            //if (!Page.Header.Controls.Add(new LiteralControl(("cropCss"))
            //    cs.RegisterStartupScript(this.GetType(), "cropCss", cropCss, false);

            if (!cs.IsClientScriptBlockRegistered("cropJS"))
            {
                Page.Header.Controls.Add(new LiteralControl(cropCss));
            }

            //bool cropInitScriptAdded = cs.IsClientScriptBlockRegistered("cropInit");

            if (true)
            {
                System.Web.UI.WebControls.Image image =
                    (System.Web.UI.WebControls.Image)Parent.FindControl(cropimage);
                ViewState["ImageUrl"] = image.ImageUrl;


                //System.Drawing.Image img =
                //    System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath( image.ImageUrl));


                //check if we set canvas size
                if (this.CanvasHeight > 0 | this.CanvasWidth > 0)
                {
                    string folder = HttpContext.Current.Server.MapPath("~/");
                    GetRawSize(folder + image.ImageUrl);

                    double rratio = (double)rawHeight / rawWidth; //raw image ratio
                    double cratio;                                //canvas ratio
                    if (this.CanvasHeight == 0)
                    {
                        cratio            = rratio;
                        this.CanvasHeight = Convert.ToInt32(this.CanvasWidth * rratio);
                    }
                    else if (this.CanvasWidth == 0)
                    {
                        cratio           = rratio;
                        this.CanvasWidth = Convert.ToInt32(this.CanvasHeight / rratio);
                    }
                    else
                    {
                        cratio = (double)CanvasHeight / CanvasWidth;
                    }
                    if (rratio > cratio)
                    {//we have to fit height
                        if (rawHeight > CanvasHeight)
                        {
                            resizefact   = (double)CanvasHeight / rawHeight;
                            image.Height = CanvasHeight;
                            image.Width  = Convert.ToInt32(rawWidth * resizefact);
                        }
                    }
                    else
                    { //we have to fit width
                        if (rawWidth > CanvasWidth)
                        {
                            resizefact   = (double)CanvasWidth / rawWidth;
                            image.Width  = CanvasWidth;
                            image.Height = Convert.ToInt32(rawHeight * resizefact);
                        }
                    }
                    ViewState["canvasRatio"] = resizefact;
                }

                StringBuilder sb = new StringBuilder();

                sb.Append("<script>$(function(){ ");

                string showPreviewScript = "";

                showPreviewScript = ", onChange: __showPreview" +
                                    ", onSelect: __showPreview";


                sb.Append("$('#" + image.ClientID + "').Jcrop({" +
                          "setSelect: [" + X.ToString() + "," +
                          Y.ToString() + "," +
                          X2.ToString() + ", " +
                          Y2.ToString() + "]" +
                          showPreviewScript);


                if (!string.IsNullOrEmpty(this.Ratio))
                {
                    sb.Append(", aspectRatio: " + this.Ratio);
                }

                if (!string.IsNullOrEmpty(this.MaxSize))
                {
                    sb.Append(", maxSize: [" + this.MaxSize + "]");
                }

                if (!string.IsNullOrEmpty(this.MinSize))
                {
                    sb.Append(", minSize: [" + this.MinSize + "]");
                }

                sb.Append("});");
                sb.Append("});");


                string script = @"function __showPreview(coords){                        

                            try{
                                    
                                    $('#_x').val(coords.x); 
                                    $('#_y').val(coords.y); 
                                    $('#_w').val(coords.w);
                                    $('#_h').val(coords.h); 

                                    var rx = {1} / coords.w;
	                                var ry = {2} / coords.h;

	                                $('#{0}').css({
		                                width: Math.round(rx * {3}) + 'px',
		                                height: Math.round(ry * {4}) + 'px',
		                                marginLeft: '-' + Math.round(rx * coords.x) + 'px',
		                                marginTop: '-' + Math.round(ry * coords.y) + 'px'
	                                });

                            }
                            catch(e){} 
                                                                                      
                        }
                            
                        ";


                GetRawSize(image.ImageUrl);


                script = script.Replace("{0}", this.PreviewImageID);
                script = script.Replace("{1}", rawWidth.ToString());
                script = script.Replace("{2}", rawHeight.ToString());
                script = script.Replace("{3}", rawWidth.ToString());
                script = script.Replace("{4}", rawHeight.ToString());

                sb.Append(script);

                //sb.Append(string.Format(script.ToString(),this.PreviewImageID));



                //if (EnablePreview)
                //{
                //    sb.Append("\r\n function __showPreview22(coords){ \r\n" +
                //                "var rx = 100 / coords.w; \r\n" +
                //                "var ry = 100 / coords.h; \r\n" +
                //                "$('#preview').css({" +
                //                "    width: Math.round(rx * " + img.Width + ") + 'px', \r\n" +
                //                "    height: Math.round(ry * " + img.Height + ") + 'px', \r\n" +
                //                "    marginLeft: '-' + Math.round(rx * coords.x) + 'px', \r\n" +
                //                "    marginTop: '-' + Math.round(ry * coords.y) + 'px' \r\n" +
                //                "});  try {__showDebug(coords); } catch(e) {}}\r\n\r\n");
                //}


                /*sb.Append(@"function __showDebug(coords){
                 *          try{
                 *              $('#wci_xt').val(coords.x);
                 *              $('#wci_yt').val(coords.y);
                 *              $('#wci_wt').val(coords.w);
                 *              $('#wci_ht').val(coords.h);
                 *          }
                 *          catch(e){}
                 *      }");
                 *
                 */

                sb.Append(@"</script>");


                if (isInUpdatePanel)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType()
                                                            , "cropInit"
                                                            , sb.ToString(), false);


                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "alertbabam", "alert('test');", true);

                    ScriptManager.RegisterHiddenField(this, "_x", X.ToString());
                    ScriptManager.RegisterHiddenField(this, "_y", Y.ToString());
                    ScriptManager.RegisterHiddenField(this, "_w", W.ToString());
                    ScriptManager.RegisterHiddenField(this, "_h", H.ToString());
                }
                else
                {
                    cs.RegisterClientScriptBlock(this.GetType()
                                                 , "cropInit"
                                                 , sb.ToString(), false);


                    cs.RegisterHiddenField("_x", X.ToString());
                    cs.RegisterHiddenField("_y", Y.ToString());
                    cs.RegisterHiddenField("_w", W.ToString());
                    cs.RegisterHiddenField("_h", H.ToString());
                }



                image.Dispose();
                //img.Dispose();
            }

            base.CreateChildControls();

            //HttpContext.Current.Response.Write("ssss");
        }