Csharp: print Card using HiTi CS310

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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 spath = textBox1.Text;
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "rundll32.exe";
            string sy = System.Environment.SystemDirectory.ToString();//System.Environment.ExpandEnvironmentVariables("%SystemRoot%");<br>           <br>            startInfo.Arguments = sy + @"\shimgvw.dll,ImageView_Fullscreen" + " " + spath;<br>            //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);
            //}              
 
        }
    }
}

 

posted @   ®Geovin Du Dream Park™  阅读(609)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示