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();
            }
        }
Example #3
0
        private Bitmap ConvertPixelDataToBitmap(PixelData pixelData)
        {
            using (var bitmap = new System.DrawingCore.Bitmap(pixelData.Width, pixelData.Height, System.DrawingCore.Imaging.PixelFormat.Format32bppRgb))
                using (var ms = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new System.DrawingCore.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.DrawingCore.Imaging.ImageLockMode.WriteOnly, System.DrawingCore.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    // save to stream as PNG
                    bitmap.Save(ms, System.DrawingCore.Imaging.ImageFormat.Jpeg);

                    return(bitmap);
                }
        }
Example #4
0
        /// <summary>
        /// 生成缩略图到MemoryStream
        /// </summary>
        /// <param name="SourceFile"></param>
        /// <param name="intThumbWidth"></param>
        /// <param name="intThumbHeight"></param>
        /// <param name="gap">是否补足空白</param>
        public static MemoryStream ThumbImageToStream(string SourceFile, int intThumbWidth, int intThumbHeight, bool gap = true)
        {
            MemoryStream ms = new MemoryStream();

            //原图加载
            using (System.DrawingCore.Image sourceImage = System.DrawingCore.Image.FromFile(SourceFile))
            {
                //是否显示原图
                if (intThumbWidth == 0 && intThumbHeight == 0)
                {
                    sourceImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png);
                    return(ms);
                }
                //原图宽度和高度
                int width  = sourceImage.Width;
                int height = sourceImage.Height;
                int smallWidth;
                int smallHeight;
                //如果原图长宽均比缩略图的要小则返回原stream
                if (width <= intThumbWidth && height <= intThumbHeight)
                {
                    sourceImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png);
                    return(ms);
                }
                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)
                if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                {
                    smallWidth  = intThumbHeight * width / height;
                    smallHeight = intThumbHeight;
                }
                else
                {
                    smallWidth  = intThumbWidth;
                    smallHeight = intThumbWidth * height / width;
                }
                Image newimg = sourceImage.GetThumbnailImage(smallWidth, smallHeight, null, IntPtr.Zero);

                //使用原宽高比输出,不补足空白
                if (!gap)
                {
                    newimg.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png);
                    return(ms);
                }


                //新建一个图板,以最小等比例压缩大小绘制原图
                using (System.DrawingCore.Image bitmap = new System.DrawingCore.Bitmap(smallWidth, smallHeight))
                {
                    //绘制中间图
                    using (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(Color.Transparent);
                        g.DrawImage(
                            sourceImage,
                            new System.DrawingCore.Rectangle(0, 0, smallWidth, smallHeight),
                            new System.DrawingCore.Rectangle(0, 0, width, height),
                            System.DrawingCore.GraphicsUnit.Pixel
                            );
                    }
                    //新建一个图板,以缩略图大小绘制中间图
                    using (System.DrawingCore.Image bitmap1 = new System.DrawingCore.Bitmap(intThumbWidth, intThumbHeight))
                    {
                        //绘制缩略图
                        using (System.DrawingCore.Graphics g = System.DrawingCore.Graphics.FromImage(bitmap1))
                        {
                            //高清,平滑
                            g.InterpolationMode = System.DrawingCore.Drawing2D.InterpolationMode.High;
                            g.SmoothingMode     = System.DrawingCore.Drawing2D.SmoothingMode.HighQuality;
                            g.Clear(Color.Transparent);
                            int lwidth  = (smallWidth - intThumbWidth) / 2;
                            int bheight = (smallHeight - intThumbHeight) / 2;
                            g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                            g.Dispose();
                            bitmap1.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png);
                            return(ms);
                        }
                    }
                }
            }
        }