Image drawing example

1 comments
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Beweb;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Site.SiteCustom;
using TextBlock = Beweb.TextBlock;

...

        public void CreateImage(int entryID, EntryMember crewMember1, EntryMember crewMember2, EntryMember crewMember3) {
            var data = new ViewModel();

            var entryAndCrew = Entry.LoadID(entryID);
            var attachmentFileName = FileSystem.GetUniqueAttachmentFilename(Crypto.RandomChars(12)+".png");

            // Declare Variables
            Bitmap emptyScene = null;
            Bitmap scene = null;
            Bitmap member1 = null;
            Bitmap member2 = null;
            Bitmap member3 = null;
            Bitmap member4 = null;
            Bitmap brushTexture = null;
            Graphics tempScene = null;
            PrivateFontCollection fontFile = null;

            try {
                // Image bitmaps
                emptyScene = new Bitmap(Server.MapPath(Web.Root)+"images/temp-scene-bg.png");
                scene = new Bitmap(Server.MapPath(Web.Root)+"images/"+Request["GCSceneImage"]);
                member1 = new Bitmap(GetImageStream("http://graph.facebook.com/"+entryAndCrew.FacebookUserID+"/picture?width=275"));
                member2 = new Bitmap(GetImageStream("http://graph.facebook.com/"+crewMember1.ID + "/picture?width=275"));
                member3 = new Bitmap(GetImageStream("http://graph.facebook.com/"+crewMember2.ID+"/picture?width=275"));
                member4 = new Bitmap(GetImageStream("http://graph.facebook.com/"+crewMember3.ID+"/picture?width=275"));
               
                // Create custom font, ready for drawing
                fontFile = new PrivateFontCollection();
                fontFile.AddFontFile(Server.MapPath(Web.Root)+"theme/blanktheme/fonts/kulturista-bold-webfont.ttf");
                var font = new Font(fontFile.Families[0], 22f, FontStyle.Bold);

                // New Image data, with dimensions and offsets
                var iData1 = new ImageData(Request["crewMemberPhoto1"]);
                var iData2 = new ImageData(Request["crewMemberPhoto2"]);
                var iData3 = new ImageData(Request["crewMemberPhoto3"]);
                var iData4 = new ImageData(Request["crewMemberPhoto4"]);

                // Clipping Regions
                Rectangle r1 = new Rectangle(Pos.Left, Pos.Top, 118, 118);
                Rectangle r2 = new Rectangle(Pos.Right, Pos.Top, 118, 118);
                Rectangle r3 = new Rectangle(Pos.Left, Pos.Bottom, 118, 118);
                Rectangle r4 = new Rectangle(Pos.Right, Pos.Bottom, 118, 118);

                // Image drawing
                tempScene = Graphics.FromImage(emptyScene);
                tempScene.TextRenderingHint = TextRenderingHint.AntiAlias;

                tempScene.SetClip(r1);
                tempScene.DrawImage(member1, Pos.Left - iData1.OffsetX, Pos.Top - iData1.OffsetY, iData1.Width, iData1.Height);
                tempScene.ResetClip();

                tempScene.SetClip(r2);
                tempScene.DrawImage(member2, Pos.Right - iData2.OffsetX, Pos.Top - iData2.OffsetY, iData2.Width, iData2.Height);
                tempScene.ResetClip();

                tempScene.SetClip(r3);
                tempScene.DrawImage(member3, Pos.Left - iData3.OffsetX, Pos.Bottom - iData3.OffsetY, iData3.Width, iData3.Height);
                tempScene.ResetClip();
           
                tempScene.SetClip(r4);
                tempScene.DrawImage(member4, Pos.Right - iData4.OffsetX, Pos.Bottom - iData4.OffsetY, iData4.Width, iData4.Height);
                tempScene.ResetClip();

                // Add Scene image over crew
                tempScene.DrawImage(scene, 0, 0);

                //Add Crew Name and text Shadow
                brushTexture = new Bitmap(Server.MapPath(Web.Root)+"images/Gold-Foil-Tile.png");
                Rectangle TextContainerShadow = new Rectangle(17, 120, 400, 70);
                Rectangle TextContainer = new Rectangle(15, 118, 400, 70);
                var format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                tempScene.DrawString(entryAndCrew.CrewName, font, new SolidBrush(Color.FromArgb(200, Color.Black)), TextContainerShadow, format);
                tempScene.DrawString(entryAndCrew.CrewName, font, new TextureBrush(brushTexture), TextContainer,format);
                tempScene.Save(); // save scene/canvas

                // Save to attachments folder as a PNG
                emptyScene.Save(Server.MapPath(Web.Attachments) + attachmentFileName, ImageFormat.Png);

                // Save filename to DB against the entry record
                entryAndCrew.GCScenePicture = attachmentFileName;
                entryAndCrew.Save();
           
            } finally {
                // Clean up
                if(tempScene != null) tempScene.Dispose();
                if(emptyScene != null) emptyScene.Dispose();
                if(scene != null) scene.Dispose();
                if(member1 != null) member1.Dispose();
                if(member2 != null) member2.Dispose();
                if(member3 != null) member3.Dispose();
                if(member4 != null) member4.Dispose();
                if(fontFile != null) fontFile.Dispose();
                if(brushTexture != null) brushTexture.Dispose();
            }

            //return View("Home", data);
        }

        public static class Pos {
            public static int Top = 11;
            public static int Left = 12;
            public static int Right = 292;
            public static int Bottom = 181;
        }

        public static MemoryStream GetImageStream(string address) {
            byte[] data;
            using(WebClient client = new WebClient()) {
                data = client.DownloadData(address);
            }

            return new MemoryStream(data);
        }

Comments


Leave a Comment