Exemple #1
0
        /// <summary>
        /// Creates a bitmap from text
        /// http://chiragrdarji.wordpress.com/2008/05/09/generate-image-from-text-using-c-or-convert-text-in-to-image-using-c/
        /// </summary>
        /// <param name="sImageText"></param>
        /// <param name="sTitle"></param>
        /// <param name="style">What should it look like?</param>
        /// <param name="nLength">How much text to show, -1 means full length</param>
        /// <returns></returns>
        public static Bitmap CreateBitmapImageFromText(string sTitle, string sImageText, 
		                                               FromTextStyles style, int nLength, TextToImageAppearance appearance, Image backImage)
        {
            if (sTitle == null || sImageText == null)
            {
                throw new Exception("Create Bitmap IMage from text You must supply both text and a title");
            }

            // truncate text based on limit
            if (nLength != -1)
            {
                if (nLength > sImageText.Length)
                {
                    nLength = sImageText.Length;
                }
                sImageText = sImageText.Substring(0, nLength);
            }

            Bitmap objBmpImage = new Bitmap(1, 1);
            int intWidth = 0;
            int intHeight = 0;

            Color background = Color.Black;
            Color textcolor = Color.Black;
            Font objFont = null;
            Font titleFont = null;

            // appearance is passed in
            if (appearance == null)
            {
                appearance = new TextToImageAppearance();
            }
            appearance.Set(style);

            objFont = new Font(appearance.MainTextFont, appearance.MainTextFont.Style);
            titleFont = new Font(appearance.TitleFont, appearance.TitleFont.Style);
            background = appearance.BackgroundColor;
            textcolor = appearance.TextColor;

            if (objFont == null || titleFont == null)
            {
                //Logs.Line("GeneralImage.CreateBitmapImageFromText", "FromText font null","", Logs.CRITICAL);
                lg.Instance.Line("CreateBitMapImageFromText", ProblemType.WARNING, "FromText font null");
                return null;
            }

            // Create the Font object for the image text drawing.

            // Create a graphics object to measure the text's width and height.
            Graphics objGraphics = Graphics.FromImage(objBmpImage);
            // This is where the bitmap size is determined.
            intWidth = Math.Max((int)objGraphics.MeasureString(sImageText, objFont).Width,
                                ((int)objGraphics.MeasureString(sTitle, titleFont).Width));
            intHeight = ((int)objGraphics.MeasureString(sImageText, objFont).Height)
                + ((int)objGraphics.MeasureString(sTitle, titleFont).Height);

            // Create the bmpImage again with the correct size for the text and font.
            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
            // Add the colors to the new bitmap.
            objGraphics = Graphics.FromImage(objBmpImage);

            // Set Background color
            objGraphics.Clear(background);

            /* Gradient Test */

            if (backImage != null)
            {
            //				ImageAttributes attr = new ImageAttributes();
            //				attr.
                objGraphics.DrawImage(backImage, 0, 0, intWidth, intHeight);
            }

            else //back image is mutually exclusive with gradient

                if (appearance.IsGradient == true)
            {    // Create a diagonal linear gradient with four stops.
                Rectangle rect = new Rectangle(0, 0, intWidth, intHeight);
                Color gradientColorOne = appearance.GradientColor1;
                Color gradientColorTwo = appearance.GradientColor2;

                // Dispose of brush resources after use
                using (LinearGradientBrush lgb = new LinearGradientBrush(rect,
                                                                         gradientColorOne, gradientColorTwo, appearance.mLinearGradientMode))

                    objGraphics.FillRectangle(lgb, rect);

            }
            /* end gradient test*/

            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            objGraphics.DrawString(sTitle, titleFont, new SolidBrush(appearance.TitleColor), 0, 0);

            // put the body of the text below the title
            objGraphics.DrawString(sImageText, objFont, new SolidBrush(textcolor), 0,
                                   ((int)objGraphics.MeasureString(sTitle, titleFont).Height));

            objGraphics.Flush();
            return (objBmpImage);
        }
Exemple #2
0
            /// <summary>
            /// sets the appearance to a predefined style
            /// </summary>
            /// <param name="style"></param>
            public TextToImageAppearance Set(FromTextStyles style)
            {
                try
                {
                    switch (style)
                    {
                    case FromTextStyles.NEWSPAPER:
                    {

                        this.TitleFont = new Font("Arial", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-2302756);
                        this.TextColor = Color.FromArgb(-10066330);
                        this.MainTextFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-10066330);
                        this.FrameColor = Color.FromArgb(-657931);
                        this.LeftWidth = 5;
                        this.TopWidth = 5;
                        this.BottomWidth = 5;
                        this.RightWidth = 5;
                        this.ResizeWidth = 5;
                        this.IsGradient = true;
                        this.GradientColor1 = Color.FromArgb(-657931);
                        this.GradientColor2 = Color.FromArgb(-2302756);
                        this.mLinearGradientMode = LinearGradientMode.ForwardDiagonal;
                        this.frameGradient = Color.FromArgb(-2039584);

                    }
                        break;
                    case FromTextStyles.NOTEPAD:
                    {

                        this.TitleFont = new Font("Arial", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-256);
                        this.TextColor = Color.FromArgb(-16777216);
                        this.MainTextFont = new Font("Microsoft Sans Serif", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-16777216);
                        this.FrameColor = Color.FromArgb(-256);
                        this.LeftWidth = 1;
                        this.TopWidth = 1;
                        this.BottomWidth = 1;
                        this.RightWidth = 1;
                        this.ResizeWidth = 1;
                        this.IsGradient = true;
                        this.GradientColor1 = Color.FromArgb(-32);
                        this.GradientColor2 = Color.FromArgb(-256);
                        this.mLinearGradientMode = LinearGradientMode.ForwardDiagonal;

                    } break;

                    case FromTextStyles.PAPER:
                    {
                        BackImageResourceName = "CoreUtilities.Resources.lined4.jpg";

                        this.TitleFont = new Font("Monotype Corsiva", 24, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(16777215);
                        this.TextColor = Color.FromArgb(-12490271);
                        this.MainTextFont = new Font("Monotype Corsiva", 20, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-16777088);
                        this.FrameColor = Color.FromArgb(-16777216);
                        this.LeftWidth = 1;
                        this.TopWidth = 1;
                        this.BottomWidth = 1;
                        this.RightWidth = 1;
                        this.ResizeWidth = 10;
                        this.IsGradient = false;
                        this.GradientColor1 = Color.FromArgb(0);
                        this.GradientColor2 = Color.FromArgb(0);
                        this.mLinearGradientMode = LinearGradientMode.Horizontal;
                        this.frameGradient = Color.FromArgb(0);

                    } break;
                    case FromTextStyles.BLUETAG:
                    {
                        this.TitleFont = new Font("Arial", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-657931);
                        this.TextColor = Color.FromArgb(-657931);
                        this.MainTextFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-7876885);
                        this.FrameColor = Color.FromArgb(-15132304);
                        this.LeftWidth = 30;
                        this.TopWidth = 1;
                        this.BottomWidth = 1;
                        this.RightWidth = 1;
                        this.ResizeWidth = 1;
                        this.IsGradient = true;
                        this.GradientColor1 = Color.FromArgb(-16777216);
                        this.GradientColor2 = Color.FromArgb(-15132304);
                        this.mLinearGradientMode = LinearGradientMode.Horizontal;
                        this.frameGradient = Color.FromArgb(-10185235);

                    } break;
                    case FromTextStyles.POLAROID:
                    {
                        this.TitleFont = new Font("Arial", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-1);
                        this.TextColor = Color.FromArgb(-657931);
                        this.MainTextFont = new Font("Times New Roman", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-657931);
                        this.FrameColor = Color.FromArgb(-657931);
                        this.LeftWidth = 5;
                        this.TopWidth = 10;
                        this.BottomWidth = 50;
                        this.RightWidth = 5;
                        this.ResizeWidth = 10;
                        this.IsGradient = true;
                        this.GradientColor1 = Color.FromArgb(-16777216);
                        this.GradientColor2 = Color.FromArgb(-9868951);
                        this.mLinearGradientMode = LinearGradientMode.Horizontal;
                        this.frameGradient = Color.FromArgb(-657931);

                    } break;
                    case FromTextStyles.JUSTGRADIENT:
                    {
                        this.TitleFont = new Font("Arial", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-657931);
                        this.TextColor = Color.FromArgb(-657931);
                        this.MainTextFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-7876885);
                        this.FrameColor = Color.FromArgb(-15132304);
                        this.LeftWidth = 40;
                        this.TopWidth = 10;
                        this.BottomWidth = 10;
                        this.RightWidth = 10;
                        this.ResizeWidth = 1;
                        this.IsGradient = true;
                        this.GradientColor1 = Color.FromArgb(-16777216);
                        this.GradientColor2 = Color.FromArgb(-15132304);
                        this.mLinearGradientMode = LinearGradientMode.Horizontal;
                        this.frameGradient = Color.FromArgb(-32640);
                    } break;
                    case FromTextStyles.CORK:
                    {
                        this.TitleFont = new Font("Times New Roman", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.BackgroundColor = Color.FromArgb(-16776961);
                        this.TextColor = Color.FromArgb(-5171);
                        this.MainTextFont = new Font("Trebuchet MS", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                        this.TitleColor = Color.FromArgb(-2039584);
                        this.FrameColor = Color.FromArgb(-16777216);
                        this.LeftWidth = 1;
                        this.TopWidth = 1;
                        this.BottomWidth = 1;
                        this.RightWidth = 1;
                        this.ResizeWidth = 10;
                        this.IsGradient = false;
                        this.GradientColor1 = Color.FromArgb(0);
                        this.GradientColor2 = Color.FromArgb(0);
                        this.mLinearGradientMode = LinearGradientMode.Horizontal;
                        this.frameGradient = Color.FromArgb(0);

                        BackImageResourceName = "CoreUtilities.Resources.corksource5.jpg";
                    }
                        break;
                    case FromTextStyles.CUSTOM:
                    {
                        // do nothing, will be assumed that the user has done something with this.
                    }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    lg.Instance.Line("GeneralImages.Set", ProblemType.EXCEPTION, ex.ToString());
                }
                return this;
            }