Exemple #1
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;
        }
Exemple #2
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);
        }
Exemple #3
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();
            }
        }
Exemple #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);
                        }
                    }
                }
            }
        }
Exemple #5
0
        private void DrawImageSized(PageImage pi, System.DrawingCore.Image im, System.DrawingCore.Graphics g, System.DrawingCore.RectangleF r)
        {
            float     height, width;  // some work variables
            StyleInfo si = pi.SI;

            // adjust drawing rectangle based on padding
            System.DrawingCore.RectangleF r2 = new System.DrawingCore.RectangleF(r.Left + PixelsX(si.PaddingLeft),
                                                                                 r.Top + PixelsY(si.PaddingTop),
                                                                                 r.Width - PixelsX(si.PaddingLeft + si.PaddingRight),
                                                                                 r.Height - PixelsY(si.PaddingTop + si.PaddingBottom));

            System.DrawingCore.Rectangle ir;   // int work rectangle
            switch (pi.Sizing)
            {
            case ImageSizingEnum.AutoSize:
                // Note: GDI+ will stretch an image when you only provide
                //  the left/top coordinates.  This seems pretty stupid since
                //  it results in the image being out of focus even though
                //  you don't want the image resized.
                if (g.DpiX == im.HorizontalResolution &&
                    g.DpiY == im.VerticalResolution)
                {
                    ir = new System.DrawingCore.Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top),
                                                          im.Width, im.Height);
                }
                else
                {
                    ir = new System.DrawingCore.Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top),
                                                          Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height));
                }
                g.DrawImage(im, ir);

                break;

            case ImageSizingEnum.Clip:
                Region saveRegion = g.Clip;
                Region clipRegion = new Region(g.Clip.GetRegionData());
                clipRegion.Intersect(r2);
                g.Clip = clipRegion;
                if (g.DpiX == im.HorizontalResolution &&
                    g.DpiY == im.VerticalResolution)
                {
                    ir = new System.DrawingCore.Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top),
                                                          im.Width, im.Height);
                }
                else
                {
                    ir = new System.DrawingCore.Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top),
                                                          Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height));
                }
                g.DrawImage(im, ir);
                g.Clip = saveRegion;
                break;

            case ImageSizingEnum.FitProportional:
                float ratioIm = (float)im.Height / (float)im.Width;
                float ratioR  = r2.Height / r2.Width;
                height = r2.Height;
                width  = r2.Width;
                if (ratioIm > ratioR)
                {       // this means the rectangle width must be corrected
                    width = height * (1 / ratioIm);
                }
                else if (ratioIm < ratioR)
                {       // this means the ractangle height must be corrected
                    height = width * ratioIm;
                }
                r2 = new RectangleF(r2.X, r2.Y, width, height);
                g.DrawImage(im, r2);
                break;

            case ImageSizingEnum.Fit:
            default:
                g.DrawImage(im, r2);
                break;
            }
            return;
        }
Exemple #6
0
 private void ProcessHtml(PageTextHtml pth, System.DrawingCore.Graphics g)
 {
     pth.Build(g);            // Builds the subobjects that make up the html
     this.ProcessPage(g, pth);
 }
Exemple #7
0
        //drawstring justified with paragraph format
        public static void DrawStringJustified(System.DrawingCore.Graphics graphics, string s, System.DrawingCore.Font font, System.DrawingCore.Brush brush, System.DrawingCore.RectangleF layoutRectangle, char paragraphFormat)
        {
            try
            {
                //save the current state of the graphics handle
                GraphicsState graphicsState = graphics.Save();
                //obtain the font height to be used as line height
                double lineHeight = (double)Math.Round(font.GetHeight(graphics), 1);
                //string builder to format the text
                StringBuilder text         = new StringBuilder(s);
                var           originalFont = new System.DrawingCore.Font(font.FontFamily, font.Size, font.Style);

                //adjust the text string to ease detection of carriage returns
                text = text.Replace("\r\n", " <CR> ");
                text = text.Replace("\r", " <CR> ");
                text.Append(" <CR> ");

                //ensure measure string will bring the best measures possible (antialias)
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

                //create a string format object with the generic typographic to obtain the most accurate string measurements
                //strange, but the recommended for this case is to use a "cloned" stringformat
                var stringFormat = (System.DrawingCore.StringFormat)System.DrawingCore.StringFormat.GenericTypographic.Clone();

                //allow the correct measuring of spaces
                stringFormat.FormatFlags = System.DrawingCore.StringFormatFlags.MeasureTrailingSpaces;

                //create a stringformat for leftalignment
                System.DrawingCore.StringFormat leftAlignHandle = new System.DrawingCore.StringFormat();
                leftAlignHandle.LineAlignment = System.DrawingCore.StringAlignment.Near;

                //create a stringformat for rightalignment
                System.DrawingCore.StringFormat rightAlignHandle = new System.DrawingCore.StringFormat();
                rightAlignHandle.LineAlignment = System.DrawingCore.StringAlignment.Far;

                //measure space for the given font
                var    stringSize = graphics.MeasureString(" ", font, layoutRectangle.Size, stringFormat);
                double spaceWidth = stringSize.Width + 1;

                //measure paragraph format for the given font
                double paragraphFormatWidth = 0;
                if (paragraphFormat != ' ')
                {
                    var paragraphFormatSize = graphics.MeasureString(paragraphFormat.ToString(), new System.DrawingCore.Font(font.FontFamily, font.Size, System.DrawingCore.FontStyle.Regular), layoutRectangle.Size, stringFormat);
                    paragraphFormatWidth = paragraphFormatSize.Width;
                }

                //total word count
                int totalWords = Regex.Matches(text.ToString(), " ").Count;

                //array of words
                ArrayList words = new ArrayList();

                //measure each word
                int n = 0;
                while (true)
                {
                    //original word
                    string word = Regex.Split(text.ToString(), " ").GetValue(n).ToString();

                    //add to words array the word without tags
                    words.Add(new Word(word.Replace("<b>", "").Replace("</b>", "").Replace("<i>", "").Replace("</i>", "")));

                    //marque to start bolding or/and italic
                    if (word.ToLower().Contains("<b>") && word.ToLower().Contains("<i>"))
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold & System.DrawingCore.FontStyle.Italic);
                    }
                    else if (word.ToLower().Contains("<b>"))
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold);
                    }
                    else if (word.ToLower().Contains("<i>"))
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Italic);
                    }

                    Word currentWord = (Word)words[n];
                    currentWord.StartBold   = word.ToLower().Contains("<b>");
                    currentWord.StopBold    = word.ToLower().Contains("</b>");
                    currentWord.StartItalic = word.ToLower().Contains("<i>");
                    currentWord.StopItalic  = word.ToLower().Contains("</i>");

                    //size of the word
                    var   wordSize  = graphics.MeasureString(currentWord.String, font, layoutRectangle.Size, stringFormat);
                    float wordWidth = wordSize.Width;

                    if (wordWidth > layoutRectangle.Width && currentWord.String != "<CR>")
                    {
                        int reduce = 1;
                        while (true)
                        {
                            int    lengthChars = (int)Math.Round(currentWord.String.Length / (wordWidth / layoutRectangle.Width), 0) - reduce;
                            string cutWord     = currentWord.String.Substring(0, lengthChars);

                            //the new size of the word
                            wordSize  = graphics.MeasureString(cutWord, font, layoutRectangle.Size, stringFormat);
                            wordWidth = wordSize.Width;

                            //update the word string
                            ((Word)words[n]).String = cutWord;

                            //add new word
                            if (wordWidth <= layoutRectangle.Width)
                            {
                                totalWords++;
                                words.Add(new Word("", 0,
                                                   currentWord.StartBold, currentWord.StopBold,
                                                   currentWord.StartItalic, currentWord.StopItalic));
                                text.Replace(currentWord.String, cutWord + " " + currentWord.String.Substring(lengthChars + 1), 0, 1);
                                break;
                            }

                            reduce++;
                        }
                    }

                    //update the word size
                    ((Word)words[n]).Length = wordWidth;

                    //marque to stop bolding or/and italic
                    if (word.ToLower().Contains("</b>") && font.Style == System.DrawingCore.FontStyle.Italic)
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Italic);
                    }
                    else if (word.ToLower().Contains("</i>") && font.Style == System.DrawingCore.FontStyle.Bold)
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold);
                    }
                    else if (word.ToLower().Contains("</b>") || word.ToLower().Contains("</i>"))
                    {
                        font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Regular);
                    }

                    n++;
                    if (n > totalWords - 1)
                    {
                        break;
                    }
                }

                //before we start drawing, its wise to restore ou graphics objecto to its original state
                graphics.Restore(graphicsState);

                //restore to font to the original values
                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, originalFont.Style);

                //start drawing word by word
                int currentLine = 0;
                for (int i = 0; i < totalWords; i++)
                {
                    bool   endOfSentence = false;
                    double wordsWidth    = 0;
                    int    wordsInLine   = 0;

                    int j = i;
                    for (j = i; j < totalWords; j++)
                    {
                        if (((Word)words[j]).String == "<CR>")
                        {
                            endOfSentence = true;
                            break;
                        }

                        wordsWidth += ((Word)words[j]).Length + spaceWidth;
                        if (wordsWidth > layoutRectangle.Width && j > i)
                        {
                            wordsWidth = wordsWidth - ((Word)words[j]).Length - (spaceWidth * wordsInLine);
                            break;
                        }

                        wordsInLine++;
                    }

                    if (j > totalWords)
                    {
                        endOfSentence = true;
                    }

                    double widthOfBetween = 0;
                    if (endOfSentence)
                    {
                        widthOfBetween = spaceWidth;
                    }
                    else
                    {
                        widthOfBetween = (layoutRectangle.Width - wordsWidth) / (wordsInLine - 1);
                    }

                    double currentTop = layoutRectangle.Top + (int)(currentLine * lineHeight);

                    if (currentTop > (layoutRectangle.Height + layoutRectangle.Top))
                    {
                        i = totalWords;
                        break;
                    }

                    double currentLeft = layoutRectangle.Left;

                    bool lastWord = false;
                    for (int currentWord = 0; currentWord < wordsInLine; currentWord++)
                    {
                        bool loop = false;

                        if (((Word)words[i]).String == "<CR>")
                        {
                            i++;
                            loop = true;
                        }

                        if (!loop)
                        {
                            //last word in sentence
                            if (currentWord == wordsInLine && !endOfSentence)
                            {
                                lastWord = true;
                            }

                            if (wordsInLine == 1)
                            {
                                currentLeft = layoutRectangle.Left;
                                lastWord    = false;
                            }

                            System.DrawingCore.RectangleF   rectangleF;
                            System.DrawingCore.StringFormat stringFormatHandle;

                            if (lastWord)
                            {
                                rectangleF         = new System.DrawingCore.RectangleF(layoutRectangle.Left, (float)currentTop, layoutRectangle.Width, (float)(currentTop + lineHeight));
                                stringFormatHandle = rightAlignHandle;
                            }
                            else
                            {
                                //lets zero size for word to drawstring auto-size de word
                                rectangleF         = new System.DrawingCore.RectangleF((float)currentLeft, (float)currentTop, 0, 0);
                                stringFormatHandle = leftAlignHandle;
                            }

                            //start bolding and/or italic
                            if (((Word)words[i]).StartBold && ((Word)words[i]).StartItalic)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold & System.DrawingCore.FontStyle.Italic);
                            }
                            else if (((Word)words[i]).StartBold)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold);
                            }
                            else if (((Word)words[i]).StartItalic)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Italic);
                            }

                            //draw the word
                            graphics.DrawString(((Word)words[i]).String, font, brush, rectangleF, stringFormatHandle);

                            //stop bolding and/or italic
                            if (((Word)words[i]).StopBold && font.Style == System.DrawingCore.FontStyle.Italic)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Regular);
                            }
                            else if (((Word)words[i]).StopItalic && font.Style == System.DrawingCore.FontStyle.Bold)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Bold);
                            }
                            else if (((Word)words[i]).StopBold || ((Word)words[i]).StopItalic)
                            {
                                font = new System.DrawingCore.Font(originalFont.FontFamily, originalFont.Size, System.DrawingCore.FontStyle.Regular);
                            }

                            //paragraph formating
                            if (endOfSentence && currentWord == wordsInLine - 1 && paragraphFormat != ' ')
                            {
                                currentLeft += ((Word)words[i]).Length;
                                //draw until end of line
                                while (currentLeft + paragraphFormatWidth <= layoutRectangle.Left + layoutRectangle.Width)
                                {
                                    rectangleF = new System.DrawingCore.RectangleF((float)currentLeft, (float)currentTop, 0, 0);
                                    //draw the paragraph format
                                    graphics.DrawString(paragraphFormat.ToString(), font, brush, rectangleF, stringFormatHandle);
                                    currentLeft += paragraphFormatWidth;
                                }
                            }
                            else
                            {
                                currentLeft += ((Word)words[i]).Length + widthOfBetween;
                            }

                            //go to next word
                            i++;
                        }
                    }

                    currentLine++;

                    if (i >= totalWords)
                    {
                        break;
                    }

                    //compensate endfor
                    if (((Word)words[i]).String != "<CR>")
                    {
                        i--;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #8
0
 //drawstring justified without paragraph format
 public static void DrawStringJustified(System.DrawingCore.Graphics graphics, string s, System.DrawingCore.Font font, System.DrawingCore.Brush brush, System.DrawingCore.RectangleF layoutRectangle)
 {
     DrawStringJustified(graphics, s, font, brush, layoutRectangle, ' ');
 }