在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#条形码打印程序 源码

C#条形码打印程序 源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:217
  • 浏览次数:3134
  • 发布时间:2016-01-29
  • 实例类别:C#语言基础
  • 发 布 人:sunjun
  • 文件格式:.rar
  • 所需积分:3
 相关标签: 条形码 C# 打印

实例介绍

【实例简介】

【实例截图】

【核心代码】

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();
        }
    }
}

标签: 条形码 C# 打印

实例下载地址

C#条形码打印程序 源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警