基本信息
源码名称:C#条形码打印程序 源码
源码大小:0.14M
文件格式:.rar
开发语言:C#
更新时间:2016-01-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using Cobainsoft.Windows.Forms;

namespace BarCodeWinDemo
{
    public partial class BarCodeSetupBox : Form
    {
        public BarCodeSetupBox()
        {
            InitializeComponent();
        }

        private void comType_SelectedIndexChanged(object sender, EventArgs e)
        {
            barcodeControl2.BarcodeType = GetBarcodeType(comType.Text);
            Invalidate();
        }

        BarcodeType GetBarcodeType(string cType)
        {
            BarcodeType bt = BarcodeType.CODE128A;
            switch (cType.ToUpper())
            {
                case "C2OF5":
                    bt = BarcodeType.C2OF5;
                    break;
                case "EAN128A":
                    bt = BarcodeType.EAN128A;
                    break;
            }
            return bt;
        }

        private void cheCode39_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.ShowCode39StartStop = cheCode39.Checked;
            Invalidate();
        }

        private void txtData_TextChanged(object sender, EventArgs e)
        {
            barcodeControl2.Data = txtData.Text;
            Invalidate();
        }

        private void txtSubData_TextChanged(object sender, EventArgs e)
        {
            barcodeControl2.AddOnData = txtSubData.Text;
            Invalidate();
        }

        private void comDataShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            barcodeControl2.TextPosition = GetTextPosition(comDataShow.Text);
            Invalidate();
        }

        BarcodeTextPosition GetTextPosition(string str)
        {
            switch (str)
            {
                case "顶部":
                    return BarcodeTextPosition.Above;
                case "不显示":
                    return BarcodeTextPosition.NotShown;
                case "底部":
                default:
                    return BarcodeTextPosition.Below;
            }
        }

        private void BarCodeSetupBox_Load(object sender, EventArgs e)
        {
            comType.SelectedIndex = 0;
            comDataShow.SelectedIndex = 0;
            Invalidate();
        }

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            PrintDocument pd = new PrintDocument();
            pd.PrintPage  = new PrintPageEventHandler(pd_PrintPage);
            PrintPreviewDialog cppd = new PrintPreviewDialog();
            cppd.Document = pd;
            cppd.ShowDialog();
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            //Graphics g = e.Graphics;
            //barcodeControl2.Draw(g, barcodeControl2.ClientRectangle, GraphicsUnit.Inch, 0.01f, 0, null);
            //g.Dispose();
            e.Graphics.DrawImage(memoryImage, 0, 0);

        }


        [System.Runtime.InteropServices.DllImport("gdi32.dll ")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private Bitmap memoryImage;
        private void CaptureScreen()
        {
            Graphics mygraphics = this.panel1.CreateGraphics();//创建的是整个panel
            Size s = this.panel1.Size;//取panel大小
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, this.panel1.ClientRectangle.Width, this.panel1.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.StretchText = checkBox1.Checked;
        }

        void txtTitle_TextChanged(object sender, System.EventArgs e)
        {
            barcodeControl2.CopyRight = txtTitle.Text;
        }

        private void vistaButton3_Click(object sender, EventArgs e)
        {
            System.IO.Stream myStream;
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP files (*.bmp;*.dib)|*.bmp;*.dib|TIFF files (*.tif)|*.tif";
            fileDialog.FilterIndex = 1;
            fileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            fileDialog.RestoreDirectory = false;
            ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff };

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = fileDialog.OpenFile()) != null)
                {
                    int barcodeWidth =  1;
                    int barcodeHeight = 50;
                   
                    barcodeControl2.MakeImage(afmt[fileDialog.FilterIndex - 1], barcodeWidth, barcodeHeight, true, false, null, myStream);
                    myStream.Close();
                    memoryImage.Save(@"c:\temp.jpg");

                }
            }
        }

        private void vistaButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}