首页 > 代码库 > 二维码_(一)生成与拼接

二维码_(一)生成与拼接

毕业后工作单位第一个工作:好坑~~~!

一个简单的小DEMO

各位大神勿喷

生成:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;

namespace FrmImage
{
    public partial class FrmImage1 : Form
    {
        public FrmImage1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string enCodeString = this.textBox1.Text;
            QRCodeEncoder qrce = new QRCodeEncoder();

            this.pictureBox1.Image = qrce.Encode(enCodeString,Encoding.UTF8);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Jpg图像|*.jpg|Bmp图像|*.bmp";
            sfd.Title = "保存图像";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image.Save(sfd.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FrmImage2 frm2 = new FrmImage2();
            this.Hide();
            frm2.ShowDialog();           
        }


    }
}

拼接:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmImage
{
    public partial class FrmImage2 : Form
    {
        public FrmImage2()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 打开图像1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openpic1 = new OpenFileDialog();
            if (openpic1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openpic1.FileName);
            }
        }
        /// <summary>
        /// 打开图像2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openpic2 = new OpenFileDialog();
            if (openpic2.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(openpic2.FileName);
            }
        }
        /// <summary>
        /// 图像纵向拼接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap firstmap = new Bitmap(pictureBox1.Image);
            Bitmap secondmap = new Bitmap(pictureBox2.Image);
            if (firstmap != null && secondmap != null)
            {
                Bitmap result = MergeImageHorizontal(firstmap, secondmap);
                SaveFileDialog saveDG = new SaveFileDialog();
                saveDG.Filter = "Jpeg图片(*.jpg)|*.jpg";
                if (saveDG.ShowDialog() == DialogResult.OK)
                {
                    result.Save(saveDG.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Bitmap firstmap = new Bitmap(pictureBox1.Image);
            Bitmap secondmap = new Bitmap(pictureBox2.Image);
            if (firstmap != null && secondmap != null)
            {
                Bitmap result = MergeImageVertical(firstmap, secondmap);
                if (result != null)
                {
                    SaveFileDialog saveDG = new SaveFileDialog();
                    saveDG.Filter = "Jpeg图片(*.jpg)|*.jpg";
                    if (saveDG.ShowDialog() == DialogResult.OK)
                    {
                        result.Save(saveDG.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
        }

        private void FrmImage2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }


        #region 将两张图片沿水平方向拼接
        /// <summary>
        /// 将两张图片沿水平方向拼接
        /// </summary>
        /// <param name="firstmap">第一张图片</param>
        /// <param name="secondmap">第二张图片</param>
        /// <returns></returns>
        private Bitmap MergeImageHorizontal(Bitmap firstMap, Bitmap secondMap)
        {
            Bitmap newMap = null;
            int firstW, firstH, secondW, secondH;
            firstW = firstMap.Width;
            firstH = firstMap.Height;
            secondW = secondMap.Width;
            secondH = secondMap.Height;
            BitmapData firstBD =
                firstMap.LockBits(new Rectangle(0, 0, firstW, firstH),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData secondBD =
                secondMap.LockBits(new Rectangle(0, 0, secondW, secondH),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int firstStride = firstBD.Stride;
            int secondStride = secondBD.Stride;
            System.IntPtr firstScan0 = firstBD.Scan0;
            System.IntPtr secondScan0 = secondBD.Scan0;
            int maxH = firstH;
            if (maxH < secondH)
            {
                maxH = secondH;
            }
            newMap = new Bitmap(firstW + secondW, maxH);
            BitmapData newBD =
                newMap.LockBits(new Rectangle(0, 0, firstW + secondW, maxH),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            System.IntPtr newScan0 = newBD.Scan0;
            unsafe
            {
                int newMApStride = newBD.Stride;
                byte* pFirst = (byte*)(void*)firstScan0;
                byte* pSencond = (byte*)(void*)secondScan0;
                byte* pNew = (byte*)(void*)newScan0;
                int x, y, temp3W, tempYStride, tempYNewMapStride;
                byte temp1;

                // 写入左侧图像
                temp3W = 3 * firstW;
                for (y = 0; y < firstH; y++)
                {
                    tempYStride = y * firstStride;
                    tempYNewMapStride = y * newMApStride;
                    for (x = 0; x < temp3W; x++)
                    {
                        temp1 = pFirst[x + tempYStride];
                        pNew[x + tempYNewMapStride] = temp1;
                    }
                }

                // 写入右侧图像
                temp3W = 3 * secondW;
                for (y = 0; y < secondH; y++)
                {
                    tempYStride = y * secondStride;
                    tempYNewMapStride = y * (newMApStride) + firstStride;
                    for (x = 0; x < temp3W; x++)
                    {
                        temp1 = pSencond[x + tempYStride];
                        pNew[x + tempYNewMapStride] = temp1;
                    }
                }
                newMap.UnlockBits(newBD);
            }
            firstMap.UnlockBits(firstBD);
            secondMap.UnlockBits(secondBD);

            return newMap;
        }
        #endregion
        #region  将两张图片沿垂直方向进行拼接
        /// <summary>
        ///  将两张图片沿垂直方向进行拼接
        /// </summary>
        /// <param name="firstMap">第一张图片</param>
        /// <param name="secondMap">第二张图片</param>
        /// <returns></returns>
        private Bitmap MergeImageVertical(Bitmap firstMap, Bitmap secondMap)
        {
            Bitmap newMap = null;
            int firstW, firstH, secondW, secondH;
            firstW = firstMap.Width;
            firstH = firstMap.Height;
            secondW = secondMap.Width;
            secondH = secondMap.Height;

            BitmapData firstBD =
            firstMap.LockBits(new Rectangle(0, 0, firstW, firstH),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData secondBD =
            secondMap.LockBits(new Rectangle(0, 0, secondW, secondH),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int firstStride = firstBD.Stride;
            int secondStride = secondBD.Stride;
            System.IntPtr firstScan0 = firstBD.Scan0;
            System.IntPtr secondScan0 = secondBD.Scan0;

            int maxW = firstW;
            if (maxW < secondW)
                maxW = secondW;
            newMap = new Bitmap(maxW, firstH + secondH);
            BitmapData newBD =
            newMap.LockBits(new Rectangle(0, 0, maxW, firstH + secondH),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            System.IntPtr newScan0 = newBD.Scan0;
            unsafe
            {
                int newMapStride = newBD.Stride;
                byte* pFirst = (byte*)(void*)firstScan0;
                byte* pSecond = (byte*)(void*)secondScan0;
                byte* pNew = (byte*)(void*)newScan0;
                int x, y, temp3W, tempYStride, tempYNewMapStride;
                byte temp1;
                //写入上边图像
                temp3W = 3 * firstW;
                for (y = 0; y < firstH; y++)
                {
                    tempYStride = y * firstStride;
                    tempYNewMapStride = y * newMapStride;
                    for (x = 0; x < temp3W; x++)
                    {
                        temp1 = pFirst[x + tempYStride];
                        pNew[x + tempYNewMapStride] = temp1;
                    }
                }

                //写入下边图像
                temp3W = 3 * secondW;
                for (y = 0; y < secondH; y++)
                {
                    tempYStride = y * secondStride;
                    tempYNewMapStride = (y + firstH) * (newMapStride);
                    for (x = 0; x < temp3W; x++)
                    {
                        temp1 = pSecond[x + tempYStride];
                        pNew[x + tempYNewMapStride] = temp1;
                    }
                }
                newMap.UnlockBits(newBD);
            }

            firstMap.UnlockBits(firstBD);
            secondMap.UnlockBits(secondBD);

            return newMap;
        }
        #endregion


    }
}