C 使用ESC指令控制POS機(jī)打印小票_第1頁
C 使用ESC指令控制POS機(jī)打印小票_第2頁
C 使用ESC指令控制POS機(jī)打印小票_第3頁
C 使用ESC指令控制POS機(jī)打印小票_第4頁
C 使用ESC指令控制POS機(jī)打印小票_第5頁
已閱讀5頁,還剩63頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、C#使用ESC指令控制POS打印機(jī)打印小票 1.前言C#打印小票可以與普通打印機(jī)一樣,調(diào)用PrintDocument實現(xiàn)。也可以發(fā)送標(biāo)注你的ESC指令實現(xiàn)。由于 調(diào)用PrintDocument類時,無法操作使用串口或TCP/IP接口連接的pos打印機(jī),并且無法發(fā)送控制指令實現(xiàn)pos打印機(jī)的切紙、走紙等動作。因此個人建議使用ESC指令進(jìn)行打印會更通用。本類需要調(diào)用 ImageProcessor.cs HYPERLINK mailto:25035844qq 25035844qq 2.POS機(jī)打印小票ReceiptHelperusing System;using System.Collections

2、.Generic;using System.Text;using System.Runtime.InteropServices;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using LaisonTech monBLL;using Microsoft.Win32.SafeHandles;namespace LaisonTech.MediaLib#region 結(jié)構(gòu)體定義 StructLayout(LayoutKind.S

3、equential) public struct OVERLAPPED int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent; ; StructLayout(LayoutKind.Sequential) public struct PRINTER_DEFAULTS public int pDatatype; public int pDevMode; public int DesiredAccess; / / 對齊方式 / public enum eTextAlignMode Left = 0, Middle

4、 = 1, Right = 2 #endregion / / 小票打印類/ 使用方法:/ 1 GetPrinterList獲取已經(jīng)安裝的所有打印機(jī)列表./ Open 打開指定打印機(jī)/ 2 控制打印機(jī)動作、執(zhí)行打印內(nèi)容之前,必須先調(diào)用StartPrint,準(zhǔn)備向打印機(jī)發(fā)送控制指令/ 3 調(diào)用SetLeft, SetBold, SetAlignMode, SetFontSize . .設(shè)置打印參數(shù)/ 4 PrintText 打印內(nèi)容.注意:打印該行內(nèi)容后會自動換行(本類會在該行內(nèi)容末尾添加一個換行符)/ PrintImageFile 或 PrintBitMap打印圖片/ 5 控制指令和打印內(nèi)容都發(fā)

5、送完畢后,調(diào)用 EndPrint執(zhí)行真正打印動作 / 6 退出程序前調(diào)用Close / public class ReceiptHelper #region 指令定義 private static Byte Const_Init = new byte 0 x1B, 0 x40, 0 x20, 0 x20, 0 x20, 0 x0A, 0 x1B, 0 x64,0 x10; /設(shè)置左邊距 private const string Const_SetLeft = 1D 4C ; /設(shè)置粗體 private const string Const_SetBold = 1B 45 ; private c

6、onst String Const_Bold_YES = 01; private const String Const_Bold_NO = 00; /設(shè)置對齊方式 private const string Const_SetAlign = 1B 61 ; private const String Const_Align_Left = 30; private const String Const_Align_Middle = 31; private const String Const_Align_Right = 32; /設(shè)置字體大小,與 SetBigFont 不能同時使用 private c

7、onst string Const_SetFontSize = 1D 21 ; /設(shè)置是否大字體,等同于 SetFontSize = 2 /private const String Const_SetBigFontBold = 1B 21 38; /private const String Const_SetBigFontNotBold = 1B 21 30; /private const String Const_SetCancelBigFont = 1B 21 00; / / 打印并走紙 / private static Byte Const_Cmd_Print = new byte 0

8、x1B, 0 x4A, 0 x00 ; /走紙 private const string Const_FeedForward = 1B 4A ; private const string Const_FeedBack = 1B 6A ; /切紙 private static Byte Const_SetCut = new byte 0 x1D, 0 x56, 0 x30; /查詢打印機(jī)狀態(tài) private static Byte Const_QueryID = new byte 0 x1D, 0 x67, 0 x61; /回復(fù)幀以 ID 開頭 private static String Con

9、st_ResponseQueryID = ID; / / 設(shè)置圖標(biāo)的指令 / private static Byte Const_SetImageCommand = new Byte 0 x1B, 0 x2A, 0 x21 ;#endregion #region 常量定義 / / 最大字體大小 / public const Int32 Const_MaxFontSize = 8; / / 最大走紙距離 / public const Int32 Const_MaxFeedLength = 5000; / / 最大高寬 / public const Int32 Const_MaxImageLeng

10、th = 480; / / 每次通信最多打印的行數(shù) / public const Int32 Const_OncePrintRowCount = 24; public const Int32 Const_BrightnessGate = 100; / / 無效句柄 / public const Int32 Const_InvalidHandle = -1; #endregion #region 私有成員 / / 打印機(jī)句柄 / private int m_Handle = -1; / / 是否已經(jīng)初始化 / private Boolean m_Inited = false; #endregio

11、n #region 私有函數(shù) DllImport(winspool.Drv, EntryPoint = OpenPrinterA, SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool OpenPrinter(MarshalAs(UnmanagedType.LPStr) string szPrinter, out Int32 hPrinter, IntPtr pd); D

12、llImport(winspool.Drv, EntryPoint = StartDocPrinterA, SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, In, MarshalAs(UnmanagedType.LPStruct) DOCINFOA di); DllImport

13、(winspool.Drv, EntryPoint = EndDocPrinter, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool EndDocPrinter(Int32 hPrinter); DllImport(winspool.Drv, EntryPoint = StartPagePrinter, SetLastError = true, ExactSpelling = true, CallingConve

14、ntion = CallingConvention.StdCall) public static extern bool StartPagePrinter(Int32 hPrinter); DllImport(winspool.Drv, EntryPoint = EndPagePrinter, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool EndPagePrinter(Int32 hPrinter); DllI

15、mport(winspool.Drv, EntryPoint = WritePrinter, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool WritePrinter(Int32 hPrinter, Byte pBytes, Int32 dwCount, out Int32 dwWritten); DllImport(winspool.Drv, EntryPoint = ClosePrinter, SetLast

16、Error = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall) public static extern bool ClosePrinter(Int32 hPrinter); / / 發(fā)送指令 / / / private Boolean SendCommand(Byte cmd) if (m_Handle = Const_InvalidHandle | cmd = null | cmd.Length = cmd.Length); / / 發(fā)送文本格式的指令 / / / private Bool

17、ean SendCommand(String hexstrcmd) if (m_Handle = Const_InvalidHandle | hexstrcmd = null | hexstrcmd.Length 4) return false; byte mybyte = null; Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte); bl = SendCommand(mybyte); return bl; #endregion #region 內(nèi)部處理 - 打印圖片 / / 把圖片轉(zhuǎn)換為指令字節(jié)

18、,圖片最大高寬不能超過480 / / / / public static Boolean LoadImage(Bitmap image, ref Byte bitarray,ref Int32 datawidth,ref Int32 dataheight) Int32 newwidth = 0; Int32 newheight = 0; Bitmap destimage = image; Boolean bl = false; /如果高度超過范圍,或?qū)挾瘸^范圍,需要進(jìn)行縮小 if (image.Width Const_MaxImageLength | image.Height Const_M

19、axImageLength) /按照高度和寬度,較大的那一邊,進(jìn)行縮放 if (image.Width image.Height) newwidth = Const_MaxImageLength; newheight = (Int32)(image.Height * newwidth / (float)image.Width); else newheight = Const_MaxImageLength; newwidth = (Int32)(newheight * image.Width / (float)image.Height); bl = ImageProcessor.ResizeIm

20、age(image, newwidth, newheight, ref destimage); /把數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組 bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight); return bl; / / 把圖片轉(zhuǎn)換為指令字節(jié),圖片最大高寬不能超過480 / 如果圖片的高度不是24的整數(shù)倍,則修改為24的整數(shù)倍 / / / / public static Boolean LoadImageFromFile(String imagefilename, ref Byte bmpbytes, ref Int32 wi

21、dth, ref Int32 height) Bitmap img = ImageProcessor.LoadBitImage(imagefilename); if (img = null) return false; Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height); return bl; / / 把圖片轉(zhuǎn)換為位圖數(shù)組,每個字節(jié)的每個比特位,對應(yīng)當(dāng)前像素 是否需要打印 / / / / public static Boolean GetBitArray(Bitmap img, ref Byte allbitary,

22、 ref Int32 width, ref Int32 height) if (img = null) return false; /ESC指令格式規(guī)定: /1 打印圖片時,每條指令最多只打印24行;不足24行的,也要用全0填充滿數(shù)據(jù)字節(jié) /2 打印24行數(shù)據(jù)時,按照光柵模式縱向獲取數(shù)據(jù) / 即先獲取所有x=0的點(第0列)轉(zhuǎn)換為3個字節(jié); / 再獲取所有x=1的點轉(zhuǎn)換為3個字節(jié);.直到獲取到最右側(cè)一列的點 /3 打印完當(dāng)前24行數(shù)據(jù)后,再獲取后續(xù)24行的數(shù)據(jù)內(nèi)容,直到所有的數(shù)據(jù)獲取完畢 /獲取亮度數(shù)組 Boolean briary = null; Boolean bl = ImageProc

23、essor.ToBooleanArray(img, Const_BrightnessGate, ref briary); if (!bl) return false; height = img.Height;/如果圖像高度不是24整數(shù)倍,設(shè)置為24的整數(shù)倍 if (height % Const_OncePrintRowCount != 0) height = height + Const_OncePrintRowCount - height % Const_OncePrintRowCount; width = img.Width;/如果圖像寬度不是8的整數(shù)倍,設(shè)置為8的整數(shù)倍 if (widt

24、h % 8 != 0) width = width + 8 - width % 8; Int32 bytelen = height * width / 8;/每個像素對應(yīng)1個比特位,因此總字節(jié)數(shù)=像素位數(shù)/8 allbitary = new Bytebytelen; Int32 byteidxInCol = 0;/當(dāng)前列里首個像素,在目標(biāo)字節(jié)數(shù)組里的下標(biāo) Int32 byteidx = 0;/當(dāng)前像素在目標(biāo)數(shù)組里的字節(jié)下標(biāo) Int32 bitidx = 0;/當(dāng)前像素在目標(biāo)數(shù)組里當(dāng)前字節(jié)里的比特位下標(biāo) Int32 pixidxInCol = 0;/當(dāng)前像素在當(dāng)前列里的第幾個位置 Int32 p

25、ixidx = 0;/當(dāng)前像素在原始圖片里的下標(biāo) Int32 rowidx = 0; /當(dāng)前 處理的像素點所在行,不能超過 圖像高度 Int32 curprocrows = 0;/當(dāng)前需要處理的行數(shù)量 while (rowidx height) /按照縱向次序,把當(dāng)前列的24個數(shù)據(jù),轉(zhuǎn)換為3個字節(jié) for (Int32 colidx = 0; colidx img.Width; +colidx) /如果當(dāng)前還剩余超過24行沒處理,處理24行 if (rowidx + Const_OncePrintRowCount = img.Height) curprocrows = Const_OncePr

26、intRowCount; else /已經(jīng)不足24行,只處理剩余行數(shù) curprocrows = img.Height - rowidx; pixidxInCol = 0; /本列里從像素0開始處理 for (Int32 y = rowidx; y rowidx + curprocrows; +y) /原始圖片里像素位置 pixidx = y * img.Width + colidx; /獲取當(dāng)前像素的亮度值.如果當(dāng)前像素是黑點,需要把數(shù)組里的對應(yīng)比特位設(shè)置為1 if (briarypixidx) bitidx = 7 - pixidxInCol % 8;/最高比特位對應(yīng)首個像素.最低比特位對

27、應(yīng)末個像素 byteidx = byteidxInCol + pixidxInCol / 8; /由于最后一段可能不足24行,因此不能使用byteidx+ DataFormatProcessor.SetBitValue(bitidx, true, ref allbitarybyteidx); pixidxInCol+; byteidxInCol += 3;/每列固定24個像素,3個字節(jié) rowidx += Const_OncePrintRowCount; return true; #endregion #region 公開函數(shù) private static ReceiptHelper m_in

28、stance = new ReceiptHelper(); / / 當(dāng)前使用的打印機(jī)名稱 / public String PrinterName get;private set; / / 單件模式 / / public static ReceiptHelper GetInstance() return m_instance; / / 獲取本機(jī)安裝的所有打印機(jī) / / public static List GetPrinterList() List ret = new List(); if (PrinterSettings.InstalledPrinters.Count 1) return re

29、t; foreach (String printername in PrinterSettings.InstalledPrinters) ret.Add(printername); return ret; / / 打開打印機(jī) / / / public Boolean Open(String printername) if (m_Inited) return true; Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero); m_Inited = (bl & m_Handle != 0); ret

30、urn true; / / 開始打印,在打印之前必須調(diào)用此函數(shù) / / public Boolean StartPrint() if (!m_Inited) return false; DOCINFOA di = new DOCINFOA(); di.pDocName = My C#.NET RAW Document; di.pDataType = RAW; /Start a document. Boolean bl = StartDocPrinter(m_Handle, 1, di); if (!bl) return false; / Start a page. bl = StartPage

31、Printer(m_Handle); return bl; / / 結(jié)束打印,在打印結(jié)束之后必須調(diào)用此函數(shù) / / public Boolean EndPrint() if (!m_Inited) return false; Boolean bl = EndPagePrinter(m_Handle); bl = EndDocPrinter(m_Handle); return bl; / / 銷毀 / / public Boolean Close() if (!m_Inited) return true; m_Inited = false; /關(guān)閉設(shè)備句柄 ClosePrinter(m_Hand

32、le); m_Handle = -1; return true; / / 打印文本.在調(diào)用本函數(shù)之前必須先調(diào)用正確的 設(shè)置字體、左邊距 / / / public Boolean PrintText(String content) if (!m_Inited) return false; byte bytes = null; if (content.Length 1) content = ; if (contentcontent.Length - 1 != (char)0 x0D & contentcontent.Length - 1 != (char)0 x0A) content = cont

33、ent + (char)0 x0A; bytes = DataFormatProcessor.StringToBytes(content); bool bl = SendCommand(bytes); return bl; / / 設(shè)置對齊方式 / / / public bool SetAlignMode(eTextAlignMode alignmode) if (!m_Inited) return false; String code = String.Empty; switch (alignmode) case eTextAlignMode.Left: code = Const_Align

34、_Left; break; case eTextAlignMode.Middle: code = Const_Align_Middle; break; case eTextAlignMode.Right: code = Const_Align_Right; break; default: code = Const_Align_Left; break; /注意:先低字節(jié)后高字節(jié) string str = Const_SetAlign + code; bool bl = SendCommand(str); return bl; / / 設(shè)置左邊距 / / / public bool SetLeft

35、(int left) if (!m_Inited) return false; /注意:先低字節(jié)后高字節(jié) String hexstr = left.ToString(X4); string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2); bool bl = SendCommand(str); return bl; / / 設(shè)置粗體 / / / public Boolean SetBold(Boolean bold) if (!m_Inited) return false; /注意:先低字節(jié)后高字節(jié) S

36、tring str = String.Empty; if (bold) str = Const_SetBold + Const_Bold_YES; else str = Const_SetBold + Const_Bold_NO; bool bl = SendCommand(str); return bl; / / 切紙 / / public bool Cut() if (!m_Inited) return false; bool bl = SendCommand(Const_SetCut); return bl; / / 打印圖片 / / / public bool PrintImageFi

37、le(String imgfilename) if (!m_Inited) return false; Bitmap img = ImageProcessor.LoadBitImage(imgfilename); if (img = null) return false; Boolean bl = PrintBitmap(img); return bl; / / 打印圖片 / / / public bool PrintBitmap(Bitmap bitmap) if (!m_Inited) return false; if (bitmap = null | bitmap.Width Const

38、_MaxImageLength | bitmap.Height Const_MaxImageLength) return false; Byte bitary = null; Int32 width = 0; Int32 height = 0; Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height); bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height); return bl; / / 打印圖片 / / / public bool PrintBitma

39、pBytes(Byte imgbitarray, Int32 width, Int32 height) if (!m_Inited) return false; Int32 bytes = width * height / 8; /檢查是否尺寸符合要求 if (width Const_MaxImageLength | height Const_MaxFeedLength | width 1 | height 1 | imgbitarray = null) return false; /每次獲取24行的數(shù)據(jù)進(jìn)行發(fā)送,這24行的字節(jié)數(shù) Int32 blockbytes = width * Cons

40、t_OncePrintRowCount / 8; if (blockbytes 1) return false; Boolean bl = false; /一共需要發(fā)送的塊數(shù)量 Int32 blocks = imgbitarray.Length / blockbytes; /每次發(fā)送的數(shù)據(jù)字節(jié)數(shù) = 1B 2A 21 2字節(jié)長度 +數(shù)據(jù)內(nèi)容 Byte cmdbytes = new Byte5 + blockbytes; /指令 Array.Copy(Const_SetImageCommand, cmdbytes, 3); /數(shù)據(jù)長度,即 每行的點數(shù) DataFormatProcessor.In

41、t16ToBytes(width, ref cmdbytes, 3); /數(shù)據(jù)內(nèi)容 for (Int32 blockidx = 0; blockidx blocks; +blockidx) Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes); /發(fā)送當(dāng)前指令 bl = SendCommand(cmdbytes); if (!bl) return false; /休眠20毫秒 Thread.Sleep(20); /發(fā)送 打印指令 bl = SendCommand(Const_Cmd_Print); if

42、(!bl) return false; return bl; / / 走紙 / / / public bool Feed(int length) if (!m_Inited) return false; if (length Const_MaxFeedLength) length = Const_MaxFeedLength; string len = length.ToString(X2); len = Const_FeedForward + len; bool bl = SendCommand(len); return bl; / / 回退走紙 / / / public bool FeedB

43、ack(int length) if (!m_Inited) return false; if (length Const_MaxFeedLength) length = Const_MaxFeedLength; string len = length.ToString(X2); len = Const_FeedBack + len; bool bl = SendCommand(len); return bl; / / 設(shè)置字體大小.本函數(shù)不可與SetBigFont同時使用 / / 大小倍率,取值范圍 1 - 8 / public bool SetFontSize(Int32 sizerate

44、) if (!m_Inited) return false; if (sizerate Const_MaxFontSize) sizerate = Const_MaxFontSize; sizerate-; String sizecodestr = Const_SetFontSize + sizerate.ToString(X1) + sizerate.ToString(X1); bool bl = SendCommand(sizecodestr); return bl; #endregion 3.圖像處理 ImageProcessorusing System;using System.Col

45、lections.Generic;using System.Linq;using System.Text;using System.Drawing;using LaisonTech monBLL;using System.Drawing.Imaging;using System.IO; using System.Drawing.Drawing2D;using System.Windows.Forms;using AForge.Imaging.Filters;namespace LaisonTech.MediaLib / / 圖片格式 / public enum ePictureFileForm

46、at Bmp = 0, Gif = 1, Icon = 2, Jpeg = 3, Png = 4, / / 轉(zhuǎn)為灰度圖像的方式 / public enum eGrayMode / / 算數(shù)平均 / ArithmeticAverage = 0, / / 加權(quán)平均 / WeightedAverage = 1, / / 比較2個圖片的指定區(qū)域范圍,像素的相同類型 / public enum eAreaDifferentType / / 所有像素都相同 / AllSame = 0, / / 所有像素都不同 / AllDifferent = 1, / / 部分相同部分不同 / Partial = 2,

47、/ / 圖片文件處理 / public class ImageProcessor #region 常量定義 public const Byte Const_BrightnessWhite = 255; public const Byte Const_BrightnessBlack = 0; / / 比較結(jié)果的圖片里,亮度相同部分的填充顏色 / public static Color Const_SameBrightnessColor = Color.Black; / / 比較結(jié)果的圖片里,亮度相同部分的填充顏色 / public static Color Const_DifferentBrig

48、htnessColor = Color.White; public const Byte Const_BlackBrightness = 0; public const Byte Const_WhiteBrightness = 255; public const Int32 Const_MaxBrightness = 255; public const Int32 Const_MinBrightness = -255; / / 亮度的中間值 / public const Int32 Const_MiddleBrightness = 128; #endregion #region 屏幕截圖,打印

49、 / / 獲取屏幕分辨率 / / / public static void GetScreenSize(ref Int32 width, ref Int32 height) height = Screen.PrimaryScreen.Bounds.Height; width = Screen.PrimaryScreen.Bounds.Width; / /截圖指定控件上顯示的內(nèi)容 / / / public static Image CaptureControlImage(Control ctrl) if (ctrl = null) return null; Control parent = ct

50、rl; if (ctrl.Parent != null) parent = ctrl.Parent; Point screenPoint = parent.PointToScreen(ctrl.Location); Image ret = new Bitmap(ctrl.Width, ctrl.Height); Graphics g = Graphics.FromImage(ret); g.CopyFromScreen(screenPoint.X, screenPoint.Y, 0, 0, ctrl.Size); g.DrawImage(ret, 0, 0); return ret; #end

51、region #region 裝載圖片 / / 裝載圖像文件 / / / public static Image LoadImage(String filename) /Boolean bl = FileProcessor.FileExist(filename); /if (!bl) / / return null; / /Bitmap image = (Bitmap)Bitmap.FromFile(filename); /return image; /以上方法會導(dǎo)致圖片文件被鎖定,無法刪除移動 Byte photodata = null; Boolean bl = FileProcessor

52、.FileExist(filename); if (!bl) return null; bl = FileProcessor.ReadFileBytes(filename, out photodata); if (!bl) return null; MemoryStream ms = null; Image myImage = null; try ms = new MemoryStream(photodata); myImage = Bitmap.FromStream(ms); ms.Close(); catch (System.Exception ex) Console.WriteLine(

53、LoadImage error: + ex.Message); myImage = null; return myImage; / / 裝載圖像文件 / / / public static Bitmap LoadBitImage(String filename) Bitmap ret = (Bitmap)LoadImage(filename); return ret; / / 保存圖片到指定路徑 / / / / public static Boolean SaveImage(Image img, String filename) FileProcessor.DeleteFile(filenam

54、e); if (img = null) return false; /獲取保存圖片的路徑,如果路徑不存在,新建 String folder = FileProcessor.GetDirectoryName(filename); if (!FileProcessor.DirectoryExist(folder) FileProcessor.CreateDirectory(folder); img.Save(filename); Boolean bl = FileProcessor.FileExist(filename); return bl; #endregion #region 轉(zhuǎn)換圖片格式

55、/ / 轉(zhuǎn)換圖片格式 / / / / public static Boolean BmpToJpg(String bmpfilename, String jpgfilename) Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg); return bl; / / 轉(zhuǎn)換圖片格式 / / / / / public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileForm

56、at destformat) Boolean bl = FileProcessor.FileExist(srcfilename); if (!bl) return false; Image image = Image.FromFile(srcfilename); ImageFormat IFMT = null; switch (destformat) case ePictureFileFormat.Bmp: IFMT = ImageFormat.Bmp; break; case ePictureFileFormat.Gif: IFMT = ImageFormat.Gif; break; cas

57、e ePictureFileFormat.Icon: IFMT = ImageFormat.Icon; break; case ePictureFileFormat.Jpeg: IFMT = ImageFormat.Jpeg; break; case ePictureFileFormat.Png: IFMT = ImageFormat.Png; break; default: IFMT = ImageFormat.Jpeg; break; image.Save(destfilename, IFMT); image.Dispose(); bl = FileProcessor.FileExist(

58、destfilename); if (!bl) return false; Int32 filelen = FileProcessor.GetFileLength(destfilename); return (filelen 0); / / 變成黑白圖 / / 原始圖 / 模式。0:加權(quán)平均 1:算數(shù)平均 / public static Bitmap ToGray(Bitmap bitmap, eGrayMode mode = eGrayMode.ArithmeticAverage) if (bitmap = null) return null; int width = bitmap.Widt

59、h; int height = bitmap.Height; byte newColor = 0; try BitmapData srcData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe byte* curpix = (byte*)srcData.Scan0.ToPointer(); if (mode = eGrayMode.ArithmeticAverage)/ 算數(shù)平均 for (int y = 0; y

60、 height; y+) for (int x = 0; x width; x+) newColor = (byte)(float)(curpix0 + curpix1 + curpix2) / 3.0f); curpix0 = newColor; curpix1 = newColor; curpix2 = newColor; curpix += 3; curpix += srcData.Stride - width * 3; else / 加權(quán)平均 for (int y = 0; y height; y+) for (int x = 0; x width; x+) newColor = (b

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論