Пример #1
0
        public static Bitmap generateLongBitmap(List <Bitmap> bitmaps, Rectangle selectRect)
        {
            if (bitmaps.Count == 0)
            {
                return(new Bitmap(10, 10));
            }
            else if (bitmaps.Count == 1)
            {
                return(bitmaps[0]);
            }
            Bitmap   longBitmap = new Bitmap(selectRect.Width, selectRect.Height * bitmaps.Count);
            Graphics g          = Graphics.FromImage(longBitmap);

            // 将画布涂为白色(底部颜色可自行设置)
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, longBitmap.Width, longBitmap.Height));

            List <int> bitmapRowCRCList1, bitmapRowCRCList2, bitmapColmunCRCList, lastBitmapColmunCRCList;
            Bitmap     bitmap = bitmaps[0];

            bitmapColmunCRCList = ImageTool.GetBitmapCRCCode(bitmap, false);

            g.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
            int edgeIndex1 = 0, edgeIndex2 = 0, height = 0, img1Index = 0, img2Index = 0;

            for (int i = 1; i < bitmaps.Count; i++)
            {
                bitmap = bitmaps[i];
                lastBitmapColmunCRCList = bitmapColmunCRCList;
                bitmapColmunCRCList     = ImageTool.GetBitmapCRCCode(bitmap, false);
                ImageTool.FindMaxCommonEdge(bitmapColmunCRCList, lastBitmapColmunCRCList, ref edgeIndex1, ref edgeIndex2);
                bitmapRowCRCList1 = ImageTool.GetBitmapCRCCode(bitmaps[i - 1], true, edgeIndex1, edgeIndex2);
                bitmapRowCRCList2 = ImageTool.GetBitmapCRCCode(bitmap, true, edgeIndex1, edgeIndex2);
                if (ImageTool.FindMaxCommonList(bitmapRowCRCList1, bitmapRowCRCList2, ref img1Index, ref img2Index))
                {
                    height += img1Index;
                }
                else
                {
                    height   += bitmap.Height;
                    img2Index = 0;
                }
                //g.DrawImage(bitmap, i*selectRect.Width, index, bitmap.Width, bitmap.Height);
                g.DrawImage(bitmap, new Rectangle(0, height, bitmap.Width, bitmap.Height - img2Index),
                            new Rectangle(0, img2Index, bitmap.Width, bitmap.Height - img2Index), GraphicsUnit.Pixel);
                Console.WriteLine(string.Format("合并,i:{0},h:{1},i1:{2},i2:{3},e1;{4},e2:{5}",
                                                i, height, img1Index, img2Index, edgeIndex1, edgeIndex2));
            }
            g.Dispose();
            height += bitmap.Height;

            bitmap = new Bitmap(selectRect.Width, height);
            g      = Graphics.FromImage(bitmap);
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
            g.DrawImage(longBitmap, new Rectangle(0, 0, bitmap.Width, height),
                        new Rectangle(0, 0, bitmap.Width, height), GraphicsUnit.Pixel);
            g.Dispose();

            return(bitmap);
        }