コード例 #1
0
    private void CombineImages(FileInfo[] files, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical)
    {
        //change the location to store the final image.
        // URL:http://www.bianceng.cn/Programming/csharp/201410/45751.htm
        var finalImage = toPath;

        // 获取原始图片的长度宽度
        var finalWidth  = 1;
        var finalHeight = 1;

        foreach (FileInfo file in files)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
            if (mergeType == ImageMergeOrientation.Vertical)
            {
                finalWidth   = finalWidth > img.Width ? finalWidth : img.Width;
                finalHeight += img.Height;
            }
            else if (mergeType == ImageMergeOrientation.Horizontal)
            {
                finalHeight = finalHeight > img.Height ? finalHeight : img.Height;
                finalWidth += img.Width;
            }
        }

        var finalImg = new Bitmap(finalWidth, finalHeight);

        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImg);
        g.Clear(SystemColors.AppWorkspace);

        var width  = finalWidth;
        var height = finalHeight;
        var nIndex = 0;

        foreach (FileInfo file in files)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
            if (nIndex == 0)
            {
                g.DrawImage(img, new Point(0, 0));
                nIndex++;
                width  = img.Width;
                height = img.Height;
            }
            else
            {
                switch (mergeType)
                {
                case ImageMergeOrientation.Horizontal:
                    g.DrawImage(img, new Point(width, 0));
                    width += img.Width;
                    break;

                case ImageMergeOrientation.Vertical:
                    g.DrawImage(img, new Point(0, height));
                    height += img.Height;
                    break;
                }
            }
            img.Dispose();
        }
        g.Dispose();
        finalImg.Save(finalImage, System.Drawing.Imaging.ImageFormat.Png);
        finalImg.Dispose();
    }
コード例 #2
0
        public void CombineImages(FileInfo[] files, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical)
        {
            //change the location to store the final image.
            var          finalImage = toPath;
            List <Image> list       = new List <Image>();

            foreach (var item in files)
            {
                list.Add(Image.FromFile(item.FullName));
            }
            var finalWidth = mergeType == ImageMergeOrientation.Horizontal ?
                             list.Sum(img => img.Width) :
                             list.Max(img => img.Width);
            var finalHeight = mergeType == ImageMergeOrientation.Vertical ?
                              list.Sum(img => img.Height) :
                              list.Max(img => img.Height);


            //var imgs = files.Select(f => Image.FromFile(f.FullName));

            //var finalWidth = mergeType == ImageMergeOrientation.Horizontal ?
            //    imgs.Sum(img => img.Width):
            //    imgs.Max(img => img.Width);

            //var finalHeight = mergeType == ImageMergeOrientation.Vertical ?
            //    imgs.Sum(img => img.Height) :
            //    imgs.Max(img => img.Height);

            var      finalImg = new Bitmap(finalWidth, finalHeight);
            Graphics g        = Graphics.FromImage(finalImg);

            g.Clear(SystemColors.AppWorkspace);

            var width  = finalWidth;
            var height = finalHeight;
            var nIndex = 0;

            foreach (FileInfo file in files)
            {
                Image img = Image.FromFile(file.FullName);

                if (nIndex == 0)
                {
                    g.DrawImage(img, new Point(0, 0));
                    nIndex++;
                    width  = img.Width;
                    height = img.Height;
                }
                else
                {
                    switch (mergeType)
                    {
                    case ImageMergeOrientation.Horizontal:
                        g.DrawImage(img, new Point(width, 0));
                        width += img.Width;
                        break;

                    case ImageMergeOrientation.Vertical:
                        g.DrawImage(img, new Point(0, height));
                        height += img.Height;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("mergeType");
                    }
                }
                img.Dispose();
            }
            g.Dispose();
            finalImg.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
            finalImg.Dispose();
            foreach (var item in list)
            {
                item.Dispose();
            }
        }
コード例 #3
0
        //合并图片
        private void CombineImages(FileInfo[] files, string fileName, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical, ConvertType convertType = ConvertType.PDF)
        {
            var finalImage = toPath;
            var imgs       = files.Select(f => System.Drawing.Image.FromFile(f.FullName));

            var finalWidth = mergeType == ImageMergeOrientation.Horizontal ?
                             imgs.Sum(img => img.Width) :
                             imgs.Max(img => img.Width);

            var finalHeight = mergeType == ImageMergeOrientation.Vertical ?
                              imgs.Sum(img => img.Height) :
                              imgs.Max(img => img.Height);

            var finalImg = new Bitmap(finalWidth, finalHeight - (files.Count() * 1000));

            switch (convertType)
            {
                #region JPG合成
            case ConvertType.JPG:
                Graphics g = Graphics.FromImage(finalImg);
                g.Clear(SystemColors.AppWorkspace);

                var width  = finalWidth;
                var height = finalHeight;
                var nIndex = 0;
                foreach (FileInfo file in files)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
                    if (nIndex == 0)
                    {
                        g.DrawImage(img, new Point(500, 0));
                        nIndex++;
                        width  = img.Width;
                        height = img.Height;
                    }
                    else
                    {
                        switch (mergeType)
                        {
                        case ImageMergeOrientation.Horizontal:
                            g.DrawImage(img, new Point(width, 0));
                            width += img.Width;
                            break;

                        case ImageMergeOrientation.Vertical:
                            g.DrawImage(img, new Point(500, height - 1000));
                            height += img.Height - 1000;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException("mergeType");
                        }
                    }
                    img.Dispose();
                }
                string imageFilePath = finalImage + "\\" + fileName + ".jpg";
                g.Dispose();
                finalImg.Save(imageFilePath);
                finalImg.Dispose();
                break;

                #endregion
                #region PDF合成
            case ConvertType.PDF:
                Document document = new Document();
                document.SetPageSize(new iTextSharp.text.Rectangle(WWidth + 72f, HHeight + 72f));
                PdfWriter write = PdfWriter.GetInstance(document, new FileStream(finalImage + "\\" + fileName + ".pdf", FileMode.OpenOrCreate, FileAccess.Write));
                document.Open();
                iTextSharp.text.Image jpg;

                foreach (FileInfo file in files)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
                    jpg = iTextSharp.text.Image.GetInstance(img, ImageFormat.Jpeg);
                    document.NewPage();
                    document.Add(jpg);
                }
                if (document != null && document.IsOpen())
                {
                    document.Close();
                }
                if (write != null)
                {
                    write.Close();
                }
                break;

                #endregion
            default:
                break;
            }
        }