Csharp: print Card using HiTi CS310

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace FeaTon.WaterImage
{
    /// <summary>
    /// 員工IC卡打印
    /// 塗聚文 20121225
    ///
    /// </summary>
    public partial class IdCardForm : Form
    {
        /// <summary>
        /// 卡底圖
        /// </summary>
        string path = string.Empty;
        /// <summary>
        /// 卡相片
        /// </summary>
        string Photopath = string.Empty;
        /// <summary>
        /// 設置字體
        /// </summary>
        string setFont = "宋体";

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]//应用API函数
        private static extern bool BitBlt(
        IntPtr hdcDest, // 目标设备的句柄
        int nXDest, // 目标对象的左上角的X坐标
        int nYDest, // 目标对象的左上角的X坐标
        int nWidth, // 目标对象的矩形的宽度
        int nHeight, // 目标对象的矩形的长度
        IntPtr hdcSrc, // 源设备的句柄
        int nXSrc, // 源对象的左上角的X坐标
        int nYSrc, // 源对象的左上角的X坐标
        System.Int32 dwRop // 光栅的操作值
        );

        /// <summary>
        ///
        /// </summary>
        public IdCardForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 查找安裝的打印機
        /// </summary>
        private void PopulateInstalledPrintersCombo()
        {
            System.Data.DataTable PrintersList = new System.Data.DataTable();
            PrintersList.Columns.Add("id", typeof(int));
            PrintersList.Columns.Add("InstalledPrinters", typeof(string));
            // Add list of installed printers found to the combo box.
            // The pkInstalledPrinters string will be used to provide the display string.
            String pkInstalledPrinters;
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                //comboInstalledPrinters.Items.Add(pkInstalledPrinters);
                PrintersList.Rows.Add(i, pkInstalledPrinters);

            }
            comboInstalledPrinters.DataSource = PrintersList;
            comboInstalledPrinters.DisplayMember = "InstalledPrinters";
            comboInstalledPrinters.ValueMember = "id";
            comboInstalledPrinters.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboInstalledPrinters.AutoCompleteSource = AutoCompleteSource.ListItems;

        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IdCardForm_Load(object sender, EventArgs e)
        {
            //尋找本地安裝打印機
            PopulateInstalledPrintersCombo();

            //
            //尋找本地安裝字體
            InstalledFontCollection fontCollection = new InstalledFontCollection();
            foreach (FontFamily fontFamily in fontCollection.Families)
            {
                this.comboBoxFont.Items.Add(fontFamily.Name);

            }

           this.comboBoxFont.SelectedIndex = 10;

                //Response.Redirect(newPath);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboInstalledPrinters_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboInstalledPrinters.SelectedIndex != -1)
            {
                // The combo box's Text property returns the selected item's text, which is the printer name.
                printDocument1.PrinterSettings.PrinterName = comboInstalledPrinters.Text;
            }
        }
        /// <summary>
        ///  Define DrawImageAbort callback method.
        /// </summary>
        /// <param name="callBackData"></param>
        /// <returns></returns>
        private bool DrawImageCallback4(IntPtr callBackData)
        {

            // Test for call that passes callBackData parameter.
            if (callBackData == IntPtr.Zero)
            {

                // If no callBackData passed, abort DrawImage method.
                return true;
            }
            else
            {

                // If callBackData passed, continue DrawImage method.
                return false;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {

            // Create callback method.
            Graphics.DrawImageAbort imageCallback
                = new Graphics.DrawImageAbort(DrawImageCallback4);
            IntPtr imageCallbackData = new IntPtr(1);

            pictureBox1.Width = Width;
            pictureBox1.Height = Height;
            System.Drawing.Image image = this.pictureBox1.Image;
            int x = Convert.ToInt32(e.MarginBounds.X - e.PageSettings.HardMarginX);
            x = Screen.PrimaryScreen.Bounds.Width;
            int y = Convert.ToInt32(e.MarginBounds.Y - e.PageSettings.HardMarginY);
            y = Screen.PrimaryScreen.Bounds.Height;

            System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, Width, Height);

           System.Drawing.Image image1 = resizeImage(image,new  Size(Width, Height));
           // Create image attributes and set large gamma.
           ImageAttributes imageAttr = new ImageAttributes();
           imageAttr.SetGamma(4.0F);
           GraphicsUnit units = GraphicsUnit.Pixel;

           e.Graphics.DrawImage(image1, destRect, 0, 0, image1.Width, image1.Height, units);//, imageAttr, imageCallback, imageCallbackData
           //e.HasMorePages = false;
           //IntPtr dc1 = e.Graphics.GetHdc();
           //IntPtr dc2 = e.Graphics.GetHdc();
           //   BitBlt(dc2, 0, 0, panel4.ClientRectangle.Width, panel4.ClientRectangle.Height,dc1, 0, 0, 13369376);
           //BitBlt(dc1, 0, 0, image1.Width, 1024, dc1, 0, 0, 13369376);

        }
        /// <summary>
        /// 因為沒有引用HiTi打卡打印機的SDK,無法打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPrint_Click(object sender, EventArgs e)
        {
            PaperSize pkCustomSize = new PaperSize("Custum", 775, 453); //員工IC卡格式
            this.printDocument1.DefaultPageSettings.PaperSize = pkCustomSize;
            //printPreviewDialog1.SetBounds(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            pageSetupDialog1.PageSettings.Landscape = false; //true縱向   //false橫向
            int width = 775;  //216*97
            int height = 453;//(int)(101 * 100 / 25.4 + 1);
            foreach (PaperSize paperSize in printDocument1.PrinterSettings.PaperSizes)
            {
                if (paperSize.Height == width && paperSize.Height == height)//paperSize.PaperName == "Custum" &&
                {

                    printDocument1.DefaultPageSettings.PaperSize = paperSize;
                    break;
                }
            }
            if(printPreviewDialog1.ShowDialog()== DialogResult.Yes)
            {
                //System.Drawing.Printing.Duplex
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
                pd.Print();
                //printDocument1.Print();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="imgToResize"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            setFont = this.comboBoxFont.Text;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonImage_Click(object sender, EventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + "\\temp\\");
            //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
            path = dirInfo + "id4.jpg";
            string newpath = dirInfo + "id4001.jpg";
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Font f = new Font(setFont, 18, FontStyle.Bold); //字體大小
            Font fno = new Font(setFont, 10, FontStyle.Bold);
            Font fclerk = new Font(setFont, 8, FontStyle.Bold);
            Brush b = new SolidBrush(Color.Black); //字體顏色
            string addText = "塗聚文";
            //new RectangleF(0, 0, 500, 500), strFormat)
            g.DrawString(addText, f, b, new RectangleF(130, 220, 550, 110), new StringFormat());//放的姓名位置
            g.DrawString("行政及人事管理人員", fclerk, b, new RectangleF(130, 410, 550, 110), new StringFormat());//放職稱的位置
            g.DrawString("行政及人力资源管理部", fno, b, new RectangleF(130, 470, 550, 110), new StringFormat());//放部門名稱的位置
            g.DrawString("L00094", fno, b, new RectangleF(130, 540, 550, 110), new StringFormat()); //放員工編號的位置

            image.Save(newpath);
            g.Dispose();
            image.Dispose();

            //加图片水印 System.Drawing.Image
            image = System.Drawing.Image.FromFile(newpath);

            int xPosOfWm;
            int yPosOfWm;
            int phWidth = image.Width;
            int phHeight = image.Height;
            Photopath = dirInfo + "2cun.jpg"; //2寸照片
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Photopath);
            int wmWidth = copyImage.Width;
            int wmHeight = copyImage.Height;
            Graphics g1 = Graphics.FromImage(image);

            xPosOfWm = phWidth - wmWidth - 80;
            yPosOfWm = 80;
            StringFormat strFormat = new StringFormat();

            g1.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g1.Dispose();

            //保存加水印过后的图片,删除原始图片
            string newPath = string.Empty; //Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
            newPath = dirInfo + DateTime.Now.Ticks.ToString() + "_1011.jpg";
            image.Save(newPath);
            pictureBox1.Image = Image.FromFile(newPath);
            this.textBox1.Text = newPath;
            image.Dispose();
            if (File.Exists(newpath))
            {
                File.Delete(newpath);
            }               

        }
        /// <summary>
        /// 調用Windows圖片和傳真查看器
        /// HiTiCS3xx 卡片打印機,沒有調用其SDK只能這種方打印了。
        /// 塗聚文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //Process.Start("rundll32.exe", @"C:\Windows\System32\shimgvw.dll,ImageView_PrintTo /pt C:\0JQJ\Rose.tif");
            //Process.Start("rundll32.exe", @"C:\Windows\System32\shimgvw.dll,ImageView_PrintTo /pt C:\0JQJ\Rose.tif ADD-PRINTER17");
            //Process.Start("rundll32.exe", @"C:\Windows\System32\shimgvw.dll,ImageView_PrintTo /pt C:\0JQJ\Rose.tif ADDFILE00\ADD-PRINTER17");
            string Path = textBox1.Text;
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "rundll32.exe";
            startInfo.Arguments = @"D:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen" + " " + Path;
            Process.Start(startInfo);
        }
        /// <summary>
        /// 員工卡圖片生成
        /// 由背景圖,貼上二寸照片,再加上個人信息而成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + "\\temp\\");
            //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
            path = dirInfo + "id4.jpg"; //底圖片
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Font f = new Font(setFont, 18, FontStyle.Bold); //字體大小
            Font fno = new Font(setFont, 10, FontStyle.Bold);
            Font fclerk = new Font(setFont, 8, FontStyle.Bold);
            Brush b = new SolidBrush(Color.Black); //字體顏色
            string addText = "塗聚文";
            //new RectangleF(0, 0, 500, 500), strFormat)
            g.DrawString(addText, f, b, new RectangleF(130, 220, 550, 110), new StringFormat());//放的姓名位置
            g.DrawString("行政及人事管理人員", fclerk, b, new RectangleF(130, 410, 550, 110), new StringFormat());//放職稱的位置
            g.DrawString("行政及人力资源管理部", fno, b, new RectangleF(130, 470, 550, 110), new StringFormat());//放部門名稱的位置
            g.DrawString("L00094", fno, b, new RectangleF(130, 540, 550, 110), new StringFormat()); //放員工編號的位置

            int xPosOfWm;
            int yPosOfWm;
            int phWidth = image.Width;
            int phHeight = image.Height;
            Photopath = dirInfo + "2cun.jpg"; //2寸照片
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Photopath);
            int wmWidth = copyImage.Width;
            int wmHeight = copyImage.Height;

            xPosOfWm = phWidth - wmWidth - 80;
            yPosOfWm = 80;
            StringFormat strFormat = new StringFormat();

            g.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

            //保存加水印过后的图片,删除原始图片
            string newPath = string.Empty; //Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
            newPath = dirInfo + DateTime.Now.Ticks.ToString() + "_1011.jpg";
            image.Save(newPath);
            pictureBox1.Image = Image.FromFile(newPath);
            this.textBox1.Text = newPath;
            g.Dispose();
            image.Dispose();
            //if (File.Exists(newPath))
            //{
            //    File.Delete(newPath);
            //}               

        }
    }
}
时间: 2024-09-02 00:24:20

Csharp: print Card using HiTi CS310的相关文章

用ARDUNO自制RFID读写器、复旦M1卡初探

1. 为什么要用Arduino 一提到,我们可能会想到ACR122.Proxmark3这些设备,还有Radiowar出售的专业级RFID设备,实际上我们完全可以自己使用arduino单片机和RC522这种RFID模组制作简易且足够使用的RFID读写器,并实现简单的攻防实验.为了实现这个目的,我们需要了解一些基本的RFID协议和射频收发器的基本参数知识 2. RC522芯片(读卡器)简介 MF RC522是应用于13.56MHz非接触式通信中高集成度的读写卡芯片,是NXP公司针对"三表"

java-Java 梭哈 编译出错怎么解决

问题描述 Java 梭哈 编译出错怎么解决 import java.util.*;public class ShowHand{ //定义该游戏最多支持多少个玩家 private final int PLAY_NUM = 5; //定义扑克牌的所有花色和数值, //下面四个是特殊字符,会在控制台打印出方块 草花 红心 黑桃 private String[] types = {""4 "" 5 "" 3 "" 6 "&q

Swift内存管理(ARC)

原文出自:Swift ARC总结 引言 Swift使用自动引用计数(ARC)机制来处理内存.通常情况下,Swift内存管理机制会自动管理内存,无须我们考虑内存的管理.ARC会在类的实例不再被使用(也就是没有引用)时,会自动释放其占用的内存. 可是,在少数情况下,ARC需要更多地了解我们代码之间的联系,才能正确管理内存.本篇文章就这少数情况而讨论和分析其应用场景及如何更好地解决循环引用的问题. 注意:ARC仅应用于类的实例.结构体和枚举类型是值类型,不是引用类型,也不是通过引用的方式存储和传递.

csharp customer style print

/// <summary> /// 自定格式設置打印 /// 塗聚文 /// 締友計算機信息技術有限公司 /// Geovin Du /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDocument1_PrintPage(object sender, P

Singleton Pattern in CSharp

Singleton Pattern in CSharp Level Author Beginner Kunal Cheda Singleton assures that there is one and only one instance of the class and provides a global point of access to it.There are number of cases in programming where you need to make sure that

grep, egrep, fgrep, rgrep - print lines matching a pattern

grep, egrep, fgrep, rgrep - print lines matching a pattern 14.1. -v, --invert-match grep -v "grep" [root@development ~]# ps ax | grep httpd 6284 ? Ss 0:10 /usr/local/httpd-2.2.14/bin/httpd -k start 8372 ? S 0:00 perl ./wrapper.pl -chdir -name ht

关于import static net.mindview.util.Print.*;怎么设置环境变量

问题描述 关于import static net.mindview.util.Print.*;怎么设置环境变量 看下面这段代码: import java.util.*; import static net.mindview.util.Print.*; public class HelloDate { public static void main(String args[]) { print("Hello, it's: "); pritn(new Date()); } } 我是在htt

Cloud Card能否干掉App

感谢同事[向西]投递本稿 云OS 3.0已发布,总算向外界表达了我们想做个啥,很多人也开始质疑,Cloud Card到底是个啥?云OS 3.0算不算自主研发的OS?等等,今天想就Cloud Card能否干掉App这个主题聊聊这些事情. 昨天三丰和我们开会讲到一个观点,IOS&Android以App为核心的OS过时了,用户使用各种移动设备,要的不是App,是服务,虽然App也是承载服务的形式,但App与App之间是割裂的.仔细想想确实如此,我们现在为了做一件事情往往需要多个App来回折腾,用户装的

SQL中Print语句的烦恼

语句 前几天写一个程序时,无意中遇到一个怪问题. 症状描述如下: 一台联想服务器上安装了SQLSERVER服务,在其一个数据库里写了一个存储过程.此存储过程接受一个参数,里面的语句对参数进行判断然后执行分支. if @CustType=0        .................. else if @CustType=1 ....................... else ............... 基本结构是这样. 当我在开发的时候,用我的笔记本(康柏)上的程序连上服务器,程