Beispiel #1
0
    protected void drawpicture(picturelayout pl, sessionpicture sp)
    {
        Bitmap fpicture = new Bitmap(HostingEnvironment.ApplicationPhysicalPath + "\\uploadedpictures\\" + sp.filename);

        // the hard way is to cope with the panning and zooming and to also draw the border.
        Region clip = grapher.Clip;

        grapher.SetClip(new Rectangle(pl.left, pl.top, pl.width, pl.height));
        //code to use if we have rect of image to draw
        //grapher.DrawImage(fpicture, new Rectangle(pl.left, pl.top, pl.width, pl.height), new Rectangle(sp.left, sp.top, sp.right - sp.left, sp.bottom - sp.top), GraphicsUnit.Pixel);
        // code to use it we have position and zoom
        grapher.DrawImage(fpicture, pl.left + sp.left, pl.top + sp.top, sp.imagewidth * sp.scale, sp.imageheight * sp.scale);
        //grapher.DrawImage(fpicture, pl.left, pl.top, sp.imagewidth * sp.scale, sp.imageheight * sp.scale);
        grapher.SetClip(clip, CombineMode.Replace);

        if (sp.borderfilename != null)
        {
            fpicture = new Bitmap(HostingEnvironment.ApplicationPhysicalPath + "\\borders\\" + sp.borderfilename);
            grapher.DrawImage(fpicture, pl.left, pl.top, pl.width, pl.height);
        }

        if (pl.borderwidth > 0)
        {
            // top edge
            grapher.FillRectangle(pl.bordercolour.getsolidbrush, pl.left - pl.borderwidth, pl.top - pl.borderwidth, pl.width + pl.borderwidth, pl.borderwidth);
            // bottom edge
            grapher.FillRectangle(pl.bordercolour.getsolidbrush, pl.left - pl.borderwidth, pl.top + pl.height, pl.width + pl.borderwidth, pl.borderwidth);
            // left edge
            grapher.FillRectangle(pl.bordercolour.getsolidbrush, pl.left - pl.borderwidth, pl.top, pl.borderwidth, pl.height);
            // right edge
            grapher.FillRectangle(pl.bordercolour.getsolidbrush, pl.left + pl.width, pl.top - pl.borderwidth, pl.borderwidth, pl.height + pl.borderwidth + pl.borderwidth);
        }

        // also want to conditionally draw the low res or the full res picture
    }
    protected void bcrop_Click(object sender, EventArgs e)
    {
        // would be strange if no picinfo as would mean that there was no picture either, but check anyway
        if (picinfo == null)
        {
            picinfo      = new sessionpicture();
            picinfo.name = layoutinfo.name;
            sps.Add(picinfo);
        }

        // update position and scale
        try { picinfo.left = (int)(Convert.ToSingle(Request.Form["imagex"]) / pagescale); }
        catch { picinfo.left = 0; }
        try { picinfo.top = (int)(Convert.ToSingle(Request.Form["imagey"]) / pagescale); }
        catch { picinfo.top = 0; }
        try { picinfo.scale = Convert.ToSingle(Request.Form["imagesize"]); }
        catch { picinfo.scale = 1; }

        // save picture info
        sps.setcookie(Response);
        Session["sessionpictures"] = sps;

        // move on to next picture or next step
        redirectnext();
    }
    protected void bupload_Click(object sender, EventArgs e)
    {
        FileUpload fu = fupicture;
        Single     xscale, yscale, scale;

        if (fu.HasFile == false)
        {
            // follow the redirect on post pattern
            Response.Redirect(Request.Url.ToString(), true);
        }

        Guid   gfilename     = Guid.NewGuid();
        string filename      = Convert.ToString(gfilename);
        String fileextension = System.IO.Path.GetExtension(fu.FileName).ToLower();
        bool   fileok        = false;

        System.Drawing.Image inpic, outpic, thumbnailpic;
        string path = Server.MapPath("~/uploadedpictures/");

        // validate the picture (high security!)
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".tif", ".tiff", ".bmp" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileextension == allowedExtensions[i])
            {
                fileok = true;
            }
        }

        if (fileok == false)
        {
            //lpicerror.Text = "Please specify a valid picture (.gif, .png, .jpeg, .jpg, .tif, .tiff, .bmp)";
            //ppicture.Visible = true;
            return;
        }

        // save the picture and a thumbnail, ensure unique filenames
        inpic        = System.Drawing.Image.FromStream(fu.PostedFile.InputStream);
        outpic       = savepic(path + filename + ".jpg", inpic, 3000);
        thumbnailpic = savepic(path + filename + "thumbnail.jpg", inpic, 50);

        // update the session picture with this uploaded picture, set the cropping so that it is roughly half the size of the picture and is the correct proportions for the layoutpicture it is going into
        if (picinfo == null)
        {
            picinfo      = new sessionpicture();
            picinfo.name = layoutinfo.name;
            sps.Add(picinfo);
        }

        picinfo.filename          = filename + ".jpg";
        picinfo.thumbnailfilename = filename + "thumbnail.jpg";
        picinfo.imageheight       = outpic.Height;
        picinfo.imagewidth        = outpic.Width;
        picinfo.thumbnailheight   = thumbnailpic.Height;
        picinfo.thumbnailwidth    = thumbnailpic.Width;
        picinfo.left  = 0;
        picinfo.top   = 0;
        xscale        = (layoutinfo.width / picinfo.imagewidth);
        yscale        = (layoutinfo.height / picinfo.imageheight);
        scale         = Math.Max(xscale, yscale);
        picinfo.scale = Math.Max(1, scale);

        sps.setcookie(Response);
        Session["sessionpictures"] = sps;

        inpic        = null;
        outpic       = null;
        thumbnailpic = null;
        GC.Collect();

        // follow the redirect on post pattern
        Response.Redirect(Request.Url.AbsolutePath + Request.Url.Query, true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Single xscale, yscale;

        // get the article index to choose from the query string
        try
        {
            fpictureindex = Convert.ToInt16(Request.QueryString["picture"]);
        }
        catch
        {
            fpictureindex = 0;
        }

        // get layout and picture info from the seesion, redirect to home page if we don't have all that we need
        try
        {
            spl = (sessionpagelayout)Session["sessionpagelayout"];
            if (spl != null)
            {
                pl = ceddio.loadpagelayout(spl.pagelayoutfilename);
            }
            layoutinfo = pl.pictures[pictureindex];
            // get the total number of articles
            fpicturestotal = pl.pictures.Count;

            // get the prompt for the current article
            fpicturename   = pl.pictures[pictureindex].name;
            fpictureprompt = pl.pictures[pictureindex].prompt;
            sps            = (sessionpictures)Session["sessionpictures"];
            if (sps == null)
            {
                sps = new sessionpictures();
                Session["sessionpictures"] = sps;
            }
        }
        catch
        {
            Response.Redirect("default.aspx", true);
        }

        // if this is the first load of this page then save an image containing just the text of the articles. This will be overlaid on the uploaded pictures so that the user can line up the picture and the headline nicely.
        if ((Request.UrlReferrer == null) || (Request.Url.AbsolutePath != Request.UrlReferrer.AbsolutePath))
        {
            Session["articleoverlayfilename"] = "";
            // get an image containing just the text overlay
            Bitmap barticles = articlesdrawer.drawarticles(pl, (sessionarticles)Session["sessionarticles"], (sessioncustomisations)Session["sessioncustomisation"], sps, true);
            string articleoverlayfilename = Guid.NewGuid().ToString() + ".png";
            barticles.Save(Server.MapPath("~/articleoverlays/") + articleoverlayfilename, ImageFormat.Png);
            Session["articleoverlayfilename"] = articleoverlayfilename;
        }

        // work out the scaling for this picture so that all pictures take up the same space on screen when positioning. This is so that small pictures are easily visible and so large pictures are not unmanageably large
        xscale    = (400f / (Single)layoutinfo.width);
        yscale    = (300f / (Single)layoutinfo.height);
        pagescale = Math.Min(xscale, yscale);

        // get the session info for this picture, if there is any
        try { picinfo = sps.picture(fpicturename); }
        catch { picinfo = null; }

        if (Page.IsPostBack == true)
        {
            return;
        }

        // show hide the controls for an optional picture
        poptional.Visible = layoutinfo.optional;

        // hide the javascript if there is no picture to work with
        if ((picinfo == null) || (System.IO.File.Exists(HostingEnvironment.ApplicationPhysicalPath + "\\uploadedpictures\\" + picinfo.filename)) == false)
        {
            pimgscript.Visible = false;
            //pcrop.Visible = false;
        }
        else
        {
            //
        }

        DataBind();
    }