Example #1
0
        internal void DrawImage(ref System.DrawingCore.Bitmap bm, string qrcode)
        {
            Graphics g = null;

            g = Graphics.FromImage(bm);
            float mag = PixelConversions.GetMagnification(g, bm.Width, bm.Height, OptimalHeight, OptimalWidth);

            int barHeight = PixelConversions.PixelXFromMm(g, OptimalHeight * mag);
            int barWidth  = PixelConversions.PixelYFromMm(g, OptimalWidth * mag);
            var writer    = new ZXing.BarcodeWriterPixelData
            {
                Format  = ZXing.BarcodeFormat.CODE_39,
                Options = new QrCodeEncodingOptions
                {
                    Height = barHeight,
                    Width  = barWidth,
                    Margin = 0
                }
            };

            writer.Format         = ZXing.BarcodeFormat.QR_CODE;
            writer.Options.Height = barHeight;
            writer.Options.Width  = barWidth;

            bm = ConvertPixelDataToBitmap(writer.Write(qrcode));
        }
Example #2
0
        public static void ApplyWaterMark(int x, int y, Bitmap bmp, string[] watermark)
        {
            if (watermark.Length == 0)
            {
                return;
            }
            try
            {
                int yOffset   = (int)(SystemFonts.DefaultFont.SizeInPoints * 1.5f);
                int maxLength = watermark.Max(s => s.Length);

                int xWidth = (int)Math.Round(maxLength * SystemFonts.DefaultFont.SizeInPoints * 0.7);
                int i      = 0;
                var pix    = bmp.GetPixel(x, y);


                using (var g = Graphics.FromImage(bmp))
                {
                    using (var textBrush = Brushes.Wheat) //new SolidBrush(textColor)
                    {
                        g.FillRectangle(Brushes.Black, 0, y, xWidth, y + yOffset * watermark.Length);
                        foreach (var s in watermark)
                        {
                            g.DrawString(s, SystemFonts.DefaultFont, textBrush, x, Math.Min(y + i * yOffset, 719));
                            i++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Error applying watermark : "));
                Trace.WriteLine(ex);
            }
        }
Example #3
0
        public void RunPages(Pages pgs)   // this does all the work
        {
            int pageNo = 1;

            // STEP: processing a page.
            foreach (Page p in pgs)
            {
                System.DrawingCore.Bitmap   bm = CreateObjectBitmap();
                System.DrawingCore.Graphics g  = System.DrawingCore.Graphics.FromImage(bm);

                g.PageUnit = GraphicsUnit.Pixel;
                g.ScaleTransform(1, 1);

                DpiX = g.DpiX;
                DpiY = g.DpiY;

                // STEP: Fill backgroup
                g.FillRectangle(Brushes.White, 0F, 0F, (float)bm.Width, (float)bm.Height);

                // STEP: draw page to bitmap
                ProcessPage(g, p);

                // STEP:
                System.DrawingCore.Bitmap bm2 = ConvertToBitonal(bm);

                if (pageNo == 1)
                {
                    _tif = bm2;
                }

                SaveBitmap(_tif, bm2, tw, pageNo);

                pageNo++;
            }

            if (_tif != null)
            {
                // STEP: prepare encoder parameters
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(
                    System.DrawingCore.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush
                    );

                // STEP:
                _tif.SaveAdd(encoderParams);
            }

            return;
        }
Example #4
0
        private Bitmap CreateObjectBitmap()
        {
            float dpiX = 200F;
            float dpiY = 200F;

            Bitmap bm = new System.DrawingCore.Bitmap(
                Convert.ToInt32(r.ReportDefinition.PageWidth.Size / 2540F * dpiX),
                Convert.ToInt32(r.ReportDefinition.PageHeight.Size / 2540F * dpiY)
                );

            bm.MakeTransparent(Color.White);
            bm.SetResolution(dpiX, dpiY);

            return(bm);
        }
Example #5
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 #6
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 #7
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 #8
0
 /// <summary>
 /// Design time: Draw a hard coded BarCode for design time;  Parameters can't be
 /// relied on since they aren't available.
 /// </summary>
 /// <param name="bm"></param>
 public void DrawDesignerImage(ref System.DrawingCore.Bitmap bm)
 {
     DrawImage(ref bm, "MYFYI");
 }
Example #9
0
 void ICustomReportItem.DrawImage(ref System.DrawingCore.Bitmap bm)
 {
     DrawImage(ref bm, _qrCode);
 }
Example #10
0
 /// <summary>
 /// Design time: Draw a hard coded BarCode for design time;  Parameters can't be
 /// relied on since they aren't available.
 /// </summary>
 /// <param name="bm"></param>
 void ICustomReportItem.DrawDesignerImage(ref System.DrawingCore.Bitmap bm)
 {
     DrawImage(ref bm, "https://github.com/majorsilence/My-FyiReporting");
 }
Example #11
0
 void DrawImage(ref System.DrawingCore.Bitmap bm)
 {
     DrawImage(ref bm, _aztecCode);
 }
Example #12
0
 /// <summary>
 /// Design time: Draw a hard coded BarCode for design time;  Parameters can't be
 /// relied on since they aren't available.
 /// </summary>
 /// <param name="bm"></param>
 public void DrawDesignerImage(ref System.DrawingCore.Bitmap bm)
 {
     DrawImage(bm, "00" + "12345" + "12345");
 }
Example #13
0
        /// <summary>
        /// Runtime: Draw the BarCode
        /// </summary>
        /// <param name="bm">Bitmap to draw the barcode in.</param>
        public void DrawImage(ref System.DrawingCore.Bitmap bm)
        {
            string upcode = _NumberSystem + _ManufacturerCode + _ProductCode;

            DrawImage(bm, upcode);
        }
Example #14
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);
                        }
                    }
                }
            }
        }
Example #15
0
 /// <summary>
 /// Runtime: Draw the BarCode
 /// </summary>
 /// <param name="bm">Bitmap to draw the barcode in.</param>
 public void DrawImage(ref System.DrawingCore.Bitmap bm)
 {
     _Ean13.DrawImage(ref bm);
 }
Example #16
0
 /// <summary>
 /// Design time: Draw a hard coded BarCode for design time;  Parameters can't be
 /// relied on since they aren't available.
 /// </summary>
 /// <param name="bm"></param>
 public void DrawDesignerImage(ref System.DrawingCore.Bitmap bm)
 {
     _Ean13.DrawImage(bm, "978015602732");    // Yann Martel-Life of Pi
 }
Example #17
0
        /// <summary>
        /// DrawImage given a Bitmap and a upcode does all the drawing work.
        /// </summary>
        /// <param name="bm"></param>
        /// <param name="upcode"></param>
        internal void DrawImage(System.DrawingCore.Bitmap bm, string upcode)
        {
            string   barPattern = this.GetEncoding(upcode);
            Graphics g          = null;

            g = Graphics.FromImage(bm);
            float mag = PixelConversions.GetMagnification(g, bm.Width, bm.Height, OptimalHeight, OptimalWidth);

            float barWidth     = ModuleWidth * mag;
            float barHeight    = OptimalHeight * mag;
            float fontHeight   = FontHeight * mag;
            float fontHeightMM = fontHeight / 72.27f * 25.4f;
            Font  f            = null;

            try
            {
                g.PageUnit = System.DrawingCore.GraphicsUnit.Millimeter;

                // Fill in the background with white
                g.FillRectangle(Brushes.White, 0, 0, bm.Width, bm.Height);

                // Draw the bars
                int barCount = LeftQuietZoneModules;
                foreach (char bar in barPattern)
                {
                    if (bar == '1')
                    {
                        float bh = ((barCount > ModulesToManufacturingStart && barCount < ModulesToManufacturingEnd) ||
                                    (barCount > ModulesToProductStart && barCount < ModulesToProductEnd)) ?
                                   barHeight - fontHeightMM : barHeight;

                        g.FillRectangle(Brushes.Black, barWidth * barCount, 0, barWidth, bh);
                    }
                    barCount++;
                }

                // Draw the human readable portion of the barcode
                f = new Font("Arial", fontHeight);

                // Draw the left guard text (i.e. 2nd digit of the NumberSystem)
                string wc = upcode.Substring(0, 1);
                g.DrawString(wc, f, Brushes.Black,
                             new PointF(barWidth * LeftQuietZoneModules - g.MeasureString(wc, f).Width, barHeight - fontHeightMM));

                // Draw the manufacturing digits
                wc = upcode.Substring(1, 6);
                g.DrawString(wc, f, Brushes.Black,
                             new PointF(barWidth * ModulesToManufacturingEnd - g.MeasureString(wc, f).Width, barHeight - fontHeightMM));

                // Draw the product code + the checksum digit
                wc = upcode.Substring(7, 5) + CheckSum(upcode).ToString();
                g.DrawString(wc, f, Brushes.Black,
                             new PointF(barWidth * ModulesToProductEnd - g.MeasureString(wc, f).Width, barHeight - fontHeightMM));
            }
            finally
            {
                if (f != null)
                {
                    f.Dispose();
                }
                if (g != null)
                {
                    g.Dispose();
                }
            }
        }