Example #1
0
        /// <summary>
        /// 输出验证码
        /// </summary>
        /// <param name="checkCode"></param>
        public static byte[] CreateImage(string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 15);

            System.DrawingCore.Bitmap   image = new System.DrawingCore.Bitmap(iwidth, 20);
            System.DrawingCore.Graphics g     = System.DrawingCore.Graphics.FromImage(image);
            g.Clear(System.DrawingCore.Color.White);
            //定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
            //定义字体
            string[] font = { "Times New Roman", "Microsoft Sans Serif", "MS Mincho", "Book Antiqua", "PMingLiU" };
            Random   rand = new Random();

            //随机输出噪点
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }

            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);

                Font  f  = new Font(font[findex], 10, FontStyle.Bold);
                Brush b  = new SolidBrush(c[cindex]);
                int   ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 14), ii);
            }
            //画一个边框
            g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);

            //输出到浏览器
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.DrawingCore.Imaging.ImageFormat.Jpeg);
            byte[] buffer = ms.ToArray();
            g.Dispose();
            image.Dispose();

            return(buffer);
        }
Example #2
0
        /// <summary>
        /// 生成图片缩略图
        /// </summary>
        /// <param name="sourcePath">图片路径</param>
        /// <param name="newPath">新图片路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static void MakeThumbnail(string sourcePath, string newPath, int width, int height)
        {
            System.DrawingCore.Image ig = System.DrawingCore.Image.FromFile(sourcePath);
            int towidth  = width;
            int toheight = height;
            int x        = 0;
            int y        = 0;
            int ow       = ig.Width;
            int oh       = ig.Height;

            if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight)
            {
                oh = ig.Height;
                ow = ig.Height * towidth / toheight;
                y  = 0;
                x  = (ig.Width - ow) / 2;
            }
            else
            {
                ow = ig.Width;
                oh = ig.Width * height / towidth;
                x  = 0;
                y  = (ig.Height - oh) / 2;
            }
            System.DrawingCore.Image    bitmap = new System.DrawingCore.Bitmap(towidth, toheight);
            System.DrawingCore.Graphics g      = System.DrawingCore.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.DrawingCore.Drawing2D.InterpolationMode.High;
            g.SmoothingMode     = System.DrawingCore.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.DrawingCore.Color.Transparent);
            g.DrawImage(ig, new System.DrawingCore.Rectangle(0, 0, towidth, toheight), new System.DrawingCore.Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
            try
            {
                bitmap.Save(newPath, System.DrawingCore.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ig.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }