首页 > 代码库 > 二维码_(二)分割与读取

二维码_(二)分割与读取

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 System.Collections;
using System.Drawing.Imaging;

namespace FrmImage
{
    public partial class FrmImage1 : Form
    {
        public FrmImage1()
        {
            InitializeComponent();
        }
        public static bool values = false;
        public string file = "";
        private void button1_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog openpic1 = new OpenFileDialog();
            if (openpic1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openpic1.FileName);
                file = openpic1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ImageManager.Cut(file, 181, 181, @"D:/", ".jpg");
            if (values == true)
            {
                this.lblUrlName.Text = "操作成功";
            }
            else
            {
                this.lblUrlName.Text = "操作失败";
            }
        }

        private void btnReader_Click(object sender, EventArgs e)
        {
            FrmImageReader fir = new FrmImageReader();
            this.Hide();
            fir.Show();
        }

        public class ImageManager
        {
            public static void Cut(string url, int widtch, int height, string savePsath, string fileExt)
            {
                try
                {
                    Bitmap bitmap = new Bitmap(url);
                    Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
                    Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / widtch);
                    for (decimal i = 0; i < MaxRow; i++)
                    {
                        for (decimal j = 0; j < MaxColumn; j++)
                        {
                            string filename = i.ToString() + "," + j.ToString() + "," + fileExt;
                            Bitmap bmp = new Bitmap(widtch,height);
                            for (int offsetX = 0; offsetX < widtch; offsetX++)
                            {
                                for (int offsetY = 0; offsetY < height; offsetY++)
                                {
                                    if (((j * widtch + offsetX)< bitmap.Width)&& ((i * height+offsetY)<bitmap.Height))
                                    {
                                        bmp.SetPixel(offsetX,offsetY,bitmap.GetPixel((int)(j * widtch + offsetX),
                                            (int)(i * height + offsetY)));
                                    }
                                }
                            }
                            //Graphics g = Graphics.FromImage(bmp);
                            //g.DrawString("添加水印", new Font("黑体", 20), new SolidBrush(Color.FromArgb(70, Color.WhiteSmoke)), 60, height/2);
                            ImageFormat format = ImageFormat.Png;
                            switch (fileExt.ToLower())
                            {
                                case"png":
                                    format = ImageFormat.Png;
                                    break;
                                case"bmp":
                                    format = ImageFormat.Bmp;
                                    break;
                                case"gif":
                                    format = ImageFormat.Gif;
                                    break;
                            }
                            bmp.Save(savePsath + "//" + filename, format);
                        }
                    }
                    values = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    values = false;
                }
            }
        }
    }
}