コード例 #1
0
        public Bitmap CreateImage(ImageSizeHelper imageSize, string basePath)
        {
            // Create Bitmap
            Bitmap   bmp = new Bitmap(imageSize.Width, imageSize.Height);
            Graphics gh  = Graphics.FromImage(bmp);

            // Background
            DrawImage(gh, imageSize, _imageSetting.Background, basePath);
            // Zoom and Draw Image with Effect
            try
            {
                Image img = ValidateFileAndGetImage(basePath);
                DrawImage(gh, imageSize, (PositionModes)Enum.Parse(typeof(PositionModes), _imageSetting.PositionMode), img);
            }
            catch
            {
            }

            // Draw Copyright Text

            if (imageSize.Width * imageSize.Height < 100 * 100)
            {
                return(bmp);
            }
            try
            {
                SizeF textSize = gh.MeasureString(_imageSetting.Copyright, new Font("tahoma", 8.50f));
                int   textLeft = (int)(((double)imageSize.Width - (double)textSize.Width) / 2);
                int   textTop  = (int)((double)imageSize.Height - (double)textSize.Height) - 2;


                //gh.DrawString(_imageSetting.Copyright, new Font("tahoma", 8.00f), new SolidBrush(Color.FromArgb(200, 255, 255, 255)), (float)textLeft+1.0f, (float)textTop+1.0f);
                gh.FillRectangle(new SolidBrush(Color.FromArgb(100, 255, 255, 255)), (float)textLeft, (float)textTop, textSize.Width, textSize.Height);
                gh.DrawString(_imageSetting.Copyright, new Font("tahoma", 8.00f), new SolidBrush(Color.FromArgb(255, 0, 0, 0)), (float)textLeft, (float)textTop);

                // Draw Watermark

                textSize = gh.MeasureString(_imageSetting.Watermark, new Font("Arial", 16.00f, FontStyle.Bold));
                textLeft = (int)(((double)imageSize.Width - (double)textSize.Width) / 2);
                textTop  = (int)(((double)imageSize.Height - (double)textSize.Height) / 2);


                // gh.DrawString(_imageSetting.Watermark, new Font("Arial", 16.00f, FontStyle.Bold), new SolidBrush(Color.FromArgb(50, 0, 0, 0)), (float)textLeft+1.0f, (float)textTop+1.0f);
                gh.DrawString(_imageSetting.Watermark, new Font("Arial", 16.00f, FontStyle.Bold), new SolidBrush(Color.FromArgb(150, 255, 255, 255)), (float)textLeft, (float)textTop);
            }
            catch
            {
            }
            return(bmp);
        }
コード例 #2
0
        public void DrawImage(Graphics graphics, ImageSizeHelper imageSize, PositionModes positionMode, Image imageObject)
        {
            #region Draw Base Image
            try
            {
                Rectangle rect = new Rectangle();

                double W = (double)imageSize.Width     // Result Width
                , H      = (double)imageSize.Height    // Result Heigth
                , w      = (double)imageObject.Width   // Source Width
                , h      = (double)imageObject.Height; // Source Height

                double RWH = W / H;                    // Result Ratio Width In Height
                double rwh = w / h;                    // Source Ratio Width In Height
                int    Wi  = (int)((H * w) / h);       // image width after fit
                int    Hi  = (int)((W * h) / w);       // image heigth after fit
                int    Li  = (int)((W - Wi) / 2);      // image Left after fit
                int    Ti  = (int)((H - Hi) / 2);

                switch (positionMode)
                {
                case PositionModes.Stretch:
                {
                    rect = new Rectangle(0, 0, imageSize.Width, imageSize.Height);
                    break;
                }

                case PositionModes.Title:
                {
                    rect = new Rectangle(0, 0, imageSize.Width, imageSize.Height);
                    graphics.FillRectangle(new TextureBrush(imageObject, System.Drawing.Drawing2D.WrapMode.Tile), rect);
                    return;
                }

                case PositionModes.Fit:
                {
                    if (RWH > rwh)         // V Mode
                    {
                        rect = new Rectangle(Li, 0, Wi, imageSize.Height);
                    }
                    else                   // H Mode
                    {
                        rect = new Rectangle(0, Ti, imageSize.Width, Hi);
                    }

                    break;
                }

                case PositionModes.Fill:
                {
                    if (RWH < rwh)         // V Mode
                    {
                        rect = new Rectangle(Li, 0, Wi, imageSize.Height);
                    }
                    else                   // H Mode
                    {
                        rect = new Rectangle(0, Ti, imageSize.Width, Hi);
                    }

                    break;
                }

                case PositionModes.Center:
                {
                    rect = new Rectangle((int)(((double)imageSize.Width - (double)imageObject.Width) / 2)
                                         , (int)(((double)imageSize.Height - (double)imageObject.Height) / 2), imageObject.Width, imageObject.Height);
                    break;
                }
                }
                graphics.SmoothingMode      = SmoothingMode.Default;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.DrawImage(imageObject, rect);
            }
            catch
            {
            }
            #endregion
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="imageSize"></param>
        /// <param name="imageStructJsonFormatString">{PositionMode=[Fill|Fit|Stretch|Center|Title],BackColor=#ffff00,ImagePath=FullImagePath}</param>
        public void DrawImage(Graphics graphics, ImageSizeHelper imageSize, string imageStructJsonFormatString, string basePath)
        {
            JsonDictionaryConverter convertor = new JsonDictionaryConverter(imageStructJsonFormatString);

            PositionModes positionMode = PositionModes.Title;

            if (convertor.GetDictionaryItem("PositionMode") != null)
            {
                positionMode = (PositionModes)Enum.Parse(typeof(PositionModes), convertor.GetDictionaryItem("PositionMode"));
            }

            if (convertor.GetDictionaryItem("BackColor") != null)
            {
                graphics.FillRectangle(new SolidBrush(System.Drawing.ColorTranslator.FromHtml(convertor.GetDictionaryItem("BackColor"))), new Rectangle(0, 0, imageSize.Width, imageSize.Height));
            }

            try
            {
                Image img = Image.FromFile(convertor.GetDictionaryItem("ImagePath"));
                DrawImage(graphics, imageSize, positionMode, img);
            }
            catch
            {
                try
                {
                    var   page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
                    Image img  = Image.FromFile(page.MapPath("~") + convertor.GetDictionaryItem("ImagePath").Replace("/", "\\"));
                    DrawImage(graphics, imageSize, positionMode, img);
                }
                catch
                {
                    try
                    {
                        System.Text.Encoding enc = new System.Text.UTF8Encoding();
                        string type = "Unknown Type";
                        try { type = System.Web.HttpContext.Current.Request.Files[0].ContentType.Replace("application/", "").Replace("-", " ").Replace("/", " "); }
                        catch { }// convertor.GetDictionaryItem("FileType").Replace("application/", "").Replace("-", " ").Replace("/", " ");
                        var bb = (enc).GetBytes(type).ToList();
                        var by = from x in bb.Cast <byte>()
                                 let i = int.Parse(x.ToString())
                                         select i;

                        byte r = (byte)by.ToList <int>()[0];
                        byte g = (byte)by.ToList <int>()[1];
                        byte b = (byte)by.ToList <int>()[2];

                        Color co = Color.FromArgb(255, r, g, b);
                        Color cf = Color.FromArgb(255, (byte)((int)r + 128), (byte)((int)g + 128), (byte)((int)b + 128));

                        if (type.Length > 20)
                        {
                            type = type.Substring(0, 20);
                        }
                        float fs = (float)type.Length * (-1.3f) + 23.92f;
                        if (fs < 10f)
                        {
                            fs = 10f;
                        }
                        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected;
                        graphics.TextRenderingHint  = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                        graphics.FillRectangle(new SolidBrush(co), new RectangleF(0f, 0f, (float)imageSize.Width, (float)imageSize.Height));

                        graphics.DrawString(
                            type,
                            new Font("tahoma", fs, FontStyle.Bold, GraphicsUnit.Point),
                            new SolidBrush(cf),
                            new RectangleF(0f, 0f, (float)imageSize.Width, (float)imageSize.Height),
                            new StringFormat()
                        {
                            LineAlignment = StringAlignment.Center, FormatFlags = StringFormatFlags.DisplayFormatControl, Alignment = StringAlignment.Center
                        }
                            );
                    }
                    catch
                    {
                    }
                }
            }
        }