using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using WeifenLuo.WinFormsUI.Docking; using Microsoft.Office; using Packet; namespace TSProducer { public partial class MainForm : Form { // test用函数(Bit 操作) public Byte setBit(Byte targetByte, int targetPos, int value) { if (value == 0 || value == 1) { int targetPosValue = 0; switch (targetPos) { case 0: targetPosValue = (targetByte >> 7) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x80); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0x7f); } break; } case 1: targetPosValue = (targetByte >> 6) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x40); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xbf); } break; } case 2: targetPosValue = (targetByte >> 5) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x20); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xdf); } break; } case 3: targetPosValue = (targetByte >> 4) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x10); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xef); } break; } case 4: targetPosValue = (targetByte >> 3) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x08); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xf7); } break; } case 5: targetPosValue = (targetByte >> 2) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x04); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xfb); } break; } case 6: targetPosValue = (targetByte >> 1) & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x02); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xfd); } break; } case 7: targetPosValue = targetByte & 0x01; if (targetPosValue == value) //如果指定位的值和传的参数值相同,则没有任何操作,返回 { break; } else { if (value == 1) { targetByte = Convert.ToByte(targetByte | 0x01); } else if (value == 0) { targetByte = Convert.ToByte(targetByte & 0xfe); } break; } default: break; } return targetByte; } else { return 0; } } public int getBit(Byte targetByte, int targetPos) { int value = -1; switch (targetPos) { case 0: value = (targetByte >> 7) & 0x01; break; case 1: value = (targetByte >> 6) & 0x01; break; case 2: value = (targetByte >> 5) & 0x01; break; case 3: value = (targetByte >> 4) & 0x01; break; case 4: value = (targetByte >> 3) & 0x01; break; case 5: value = (targetByte >> 2) & 0x01; break; case 6: value = (targetByte >> 1) & 0x01; break; case 7: value = targetByte & 0x01; break; default: break; } return value; } // Global variable private string gFilePath = "Untitled"; //文件路径全局变量 private ParseForm gParseForm = new ParseForm(); private PictureForm gPictureForm = new PictureForm(); private ReplaceForm gReplaceForm = new ReplaceForm(); // Initial public MainForm() { InitializeComponent(); richTextBoxMain.Focus(); LoadParseForm(); //载入解析窗体 //把行号标签字体与richtextbox同步 labelRowIndex.Font = new Font(richTextBoxMain.Font.FontFamily, richTextBoxMain.Font.Size + 1.019f); richTextBoxMain.AllowDrop = true; richTextBoxMain.DragEnter += new DragEventHandler(richTextBoxMain_DragEnter); richTextBoxMain.DragDrop += new DragEventHandler(richTextBoxMain_DragDrop); Test test = new Test(); /*Test TS*/ richTextBoxMain.Text = test.TestTS(); /*Test PAT*/ //richTextBoxMain.Text = test.TestPat(); /******************************** * Test Program ********************************/ //Byte[] bytes = { Convert.ToByte(0xA2), Convert.ToByte(0xC3), Convert.ToByte(0x29), Convert.ToByte(0x41) }; //PAT_Program program = new PAT_Program(bytes); //string programInfo = ""; //programInfo += "PROGRAM_NUMBER: 0x" + Convert.ToString(program.PROGRAM_NUMBER, 16) + "\r\n"; //programInfo += "PROGRAM_MAP_PID: 0x" + Convert.ToString(program.PROGRAM_MAP_PID, 16) + "\r\n"; //richTextBoxMain.Text = programInfo; } /// <summary> /// 载入Parse子窗体 /// </summary> public void LoadParseForm() { gParseForm.Show(dockPanel1); gParseForm.DockTo(dockPanel1, DockStyle.Fill); } /*文本行号显示 begin*/ private void richTextBoxMain_TextChanged(object sender, EventArgs e) { updateLabelRowIndex(); } private void richTextBoxMain_FontChanged(object sender, EventArgs e) { updateLabelRowIndex(); richTextBoxMain_VScroll(null, null); } private void richTextBoxMain_Resize(object sender, EventArgs e) { richTextBoxMain_VScroll(null, null); } private void richTextBoxMain_VScroll(object sender, EventArgs e) { //move location of numberLabel for amount of pixels caused by scrollbar int p = richTextBoxMain.GetPositionFromCharIndex(0).Y % (richTextBoxMain.Font.Height + 1); labelRowIndex.Location = new Point(0, p); updateLabelRowIndex(); } private void updateLabelRowIndex() { //we get index of first visible char and number of first visible line Point pos = new Point(0, 0); int firstIndex = this.richTextBoxMain.GetCharIndexFromPosition(pos); int firstLine = this.richTextBoxMain.GetLineFromCharIndex(firstIndex); //now we get index of last visible char and number of last visible line pos.X += this.richTextBoxMain.ClientRectangle.Width; pos.Y += this.richTextBoxMain.ClientRectangle.Height; int lastIndex = this.richTextBoxMain.GetCharIndexFromPosition(pos); int lastLine = this.richTextBoxMain.GetLineFromCharIndex(lastIndex); //this is point position of last visible char, //we'll use its Y value for calculating numberLabel size pos = this.richTextBoxMain.GetPositionFromCharIndex(lastIndex); labelRowIndex.Text = ""; for (int i = firstLine; i <= lastLine + 1; i++) { labelRowIndex.Text += i + 1 + "\r\n"; } } /*end*/ /*textbox中文件拖放*/ //private void TextBoxEditDragDrop(object sender, DragEventArgs e) //{ // gFilePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); // if (!gFilePath.EndsWith(".png")) // { // setFormTitle(); // } // OpenFile(); //} //private void TextBoxEditDragEnter(object sender, DragEventArgs e) //{ // if (e.Data.GetDataPresent(DataFormats.FileDrop)) // e.Effect = DragDropEffects.All; // else e.Effect = DragDropEffects.None; //} /*richtextbox中文件拖放*/ /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void richTextBoxMain_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } private void richTextBoxMain_DragDrop(object sender, DragEventArgs e) { Array arrayFileName = (Array)e.Data.GetData(DataFormats.FileDrop); string strFileName = arrayFileName.GetValue(0).ToString(); StreamReader sr = new StreamReader(strFileName, System.Text.Encoding.Default); richTextBoxMain.Text = sr.ReadToEnd(); sr.Close(); } // File private void NewFile(object sender, EventArgs e) { richTextBoxMain.Text = string.Empty; gFilePath = "Untitled"; setFormTitle(); } private void OpenFile(object sender, EventArgs e) { openFileDialogMainForm.Filter = "(*dat)|*.dat|(*txt)|*.txt|(*rtf)|*.rtf|(*all)|*.*"; if (openFileDialogMainForm.ShowDialog() == DialogResult.OK) { gFilePath = openFileDialogMainForm.FileName; setFormTitle(); OpenFile(); } } private void SaveFile(object sender, EventArgs e) { if (gFilePath == "Untitled") //no file name { if (saveFileDialogMainForm.ShowDialog() == DialogResult.OK) { gFilePath = saveFileDialogMainForm.FileName; setFormTitle(); SaveFile(); } } else { SaveFile(); } } private void SaveAsTxt(object sender, EventArgs e) { try { saveFileDialogMainForm.Filter = "文本文件(*TXT)|*.txt|所有文件(*.*)|*.*"; if (saveFileDialogMainForm.ShowDialog() == DialogResult.OK) { gFilePath = saveFileDialogMainForm.FileName; FileStream fsTxt = new FileStream(gFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamWriter swTxt = new StreamWriter(fsTxt); swTxt.Write(richTextBoxMain.Text); swTxt.Flush(); swTxt.Close(); fsTxt.Close(); } } catch (IOException ex) { MessageBox.Show(ex.Message, "TSFilesMaker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void SaveAsRtf(object sender, EventArgs e) { saveFileDialogMainForm.Filter = "文本文件(*rtf)|*.rtf|所有文件(*.*)|*.*"; try { if (saveFileDialogMainForm.ShowDialog() == DialogResult.OK) { gFilePath = saveFileDialogMainForm.FileName; richTextBoxMain.SaveFile(gFilePath); } } catch (IOException ex) { MessageBox.Show(ex.Message, "TSFilesMaker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void SaveAsTs(object sender, EventArgs e) { saveFileDialogMainForm.Filter = "TS文件(*.ts)|*.ts | dat文件(*.dat)|*.dat | 所有文件(*.*)|*.*"; try { if (saveFileDialogMainForm.ShowDialog() == DialogResult.OK) { //gFilePath = saveFileDialogMainForm.FileName; //TSPacket tsHeader = new TSPacket(); //Parse parse = new Parse(); //tsHeader.sync_byte = 0x47; //tsHeader.transport_error_indicator = 0; //tsHeader.payload_unit_start_indicator = 1; //tsHeader.transport_priority = 0; //tsHeader.PID = 0x0000; //tsHeader.transport_scrambling_control = 0; //tsHeader.adaption_field_control = 1; //tsHeader.continuity_counter = 0x07; //parse.adjust_TS_Header(tsHeader); //richTextBoxMain.Text = Convert.ToString(tsHeader.sync_byte); //string content = Convert.ToString(tsHeader.sync_byte + tsHeader.transport_error_indicator + tsHeader.payload_unit_start_indicator // + tsHeader.transport_priority + tsHeader.PID + tsHeader.transport_scrambling_control // + tsHeader.adaption_field_control + tsHeader.continuity_counter); //richTextBoxMain.Text = content; //savePacket(content); } } catch (IOException ex) { MessageBox.Show(ex.Message, "TSFilesMaker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void SaveAsCdt(object sender, EventArgs e) { saveFileDialogMainForm.Filter = "CDT文件(*.cdt)|*.cdt | dat文件(*.dat)|*.dat | 所有文件(*.*)|*.*"; try { if (saveFileDialogMainForm.ShowDialog() == DialogResult.OK) { gFilePath = saveFileDialogMainForm.FileName; //string content = CDT.CDTTableStr + richTextBoxMain.Text; string content = richTextBoxMain.Text; savePacket(content); } } catch (IOException ex) { MessageBox.Show(ex.Message, "TSFilesMaker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } /// <summary> /// 打开文件 /// </summary> protected void OpenFile() { try { FileInfo fInfo = new FileInfo(gFilePath); string fileExtension = fInfo.Extension; if (gFilePath.EndsWith(".png")) //如果是图片文件,打开PictureForm,并显示图片 { gPictureForm = new PictureForm(gFilePath); gPictureForm.accept += new EventHandler(strPngAccept); gPictureForm.Show(); } else if (gFilePath.EndsWith(".ts") | gFilePath.EndsWith(".pat") | gFilePath.EndsWith(".pmt") | gFilePath.EndsWith(".sdt") | gFilePath.EndsWith(".cdt") | gFilePath.EndsWith(".sdtt") | gFilePath.EndsWith(".dat")) //处理二进制类型文件 { richTextBoxMain.Clear(); FileStream myStream = new FileStream(gFilePath, FileMode.Open, FileAccess.ReadWrite); BinaryReader myReader = new BinaryReader(myStream); //显示列号 string str = "\r\t\r\t"; for (uint i = 0x0; i <= 0xf; i++) { str += i.ToString("x") + "\r\t"; } str += "\r\n"; UInt64 memRowIndex = 00000000; //用来标示行号 str += memRowIndex.ToString("X08") + "h:\r\t"; int count = 0; int length = (int)myStream.Length; while (length > 0) { count++; byte tempByte = myReader.ReadByte(); string tempStr = tempByte.ToString("X02"); str += tempStr.ToUpper() + "\r\t"; length--; if (count == 16) { memRowIndex += 0xf; str += "\r\n" + memRowIndex.ToString("X08") + "h:\r\t"; //每行的行头标识 count = 0; } } richTextBoxMain.Text = str; myReader.Close(); myStream.Close(); } else if (gFilePath.EndsWith(".rtf")) { richTextBoxMain.LoadFile(gFilePath); } else //if (gFilePath.EndsWith(".txt")) //处理文本文件 { richTextBoxMain.Clear(); StreamReader sr = new StreamReader(gFilePath, Encoding.GetEncoding("gb2312")); StringBuilder output = new StringBuilder(); string str = ""; while ((str = sr.ReadLine()) != null) { output.Append(str + "\r\n"); } richTextBoxMain.Text = output.ToString(); sr.Close(); } } catch (IOException ex) { MessageBox.Show(ex.Message, "TSFilesMaker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } /// <summary> /// 保存文件 /// </summary> protected void SaveFile() { try { if (gFilePath.EndsWith(".ts") | gFilePath.EndsWith(".pat") | gFilePath.EndsWith(".pmt") | gFilePath.EndsWith(".sdt") | gFilePath.EndsWith(".cdt") | gFilePath.EndsWith(".sdtt") | gFilePath.EndsWith(".dat")) //保存二进制类型文件 { FileStream fsDat = new FileStream(gFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); BinaryWriter bwDat = new BinaryWriter(fsDat); bwDat.Write(richTextBoxMain.Text); bwDat.Flush(); bwDat.Close(); fsDat.Close(); } else if (gFilePath.EndsWith(".txt")) //保存.txt文件 { FileStream fsTxt = new FileStream(gFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamWriter swTxt = new StreamWriter(fsTxt); swTxt.Write(richTextBoxMain.Text); swTxt.Flush(); swTxt.Close(); fsTxt.Close(); } else if (gFilePath.EndsWith(".rtf")) //保存.rtf文件 { richTextBoxMain.SaveFile(gFilePath); } } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException - MainForm", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } /// <summary> /// 打开文件后,显示文章标题 /// </summary> protected void setFormTitle() { FileInfo fileinfo = new FileInfo(gFilePath); this.Text = fileinfo.Name + " - TSFilesMaker"; if (gFilePath.EndsWith(".dat")) { this.Text += "(二进制数据以十六进制显示)"; } } /// <summary> /// 保存Packet文件(TS/PAT/PMT/SDT/CDT/SDTT) /// </summary> /// <param name="content"></param> protected void savePacket(string content) { try { FileStream fs = new FileStream(gFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs); bw.Write(content); bw.Flush(); bw.Close(); fs.Close(); } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException - MainForm", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // Edit private void OnClickEditUndo(object sender, EventArgs e) { this.richTextBoxMain.Undo(); } private void OnClickEditCut(object sender, EventArgs e) { if (richTextBoxMain.SelectedText.Length > 0) { this.richTextBoxMain.Cut(); } } private void OnClickEditCopy(object sender, EventArgs e) { if (richTextBoxMain.SelectedText.Length > 0) { richTextBoxMain.Copy(); } } private void OnClickEditPaste(object sender, EventArgs e) { richTextBoxMain.Paste(); } private void OnClickEditDelete(object sender, EventArgs e) { if (richTextBoxMain.SelectedText.Length > 0) { richTextBoxMain.SelectedText = ""; } } private void OnClickEditSelectAll(object sender, EventArgs e) { this.richTextBoxMain.SelectAll(); } private void OnClickOpenReplaceForm(object sender, EventArgs e) { ReplaceForm gReplaceForm = new ReplaceForm(); gReplaceForm.find += new EventHandler(FindMatchKeyword); gReplaceForm.replace += new EventHandler(ReplaceMatchKeyword); gReplaceForm.replaceAll += new EventHandler(replaceAllMatchKeyword); gReplaceForm.Show(); } private int findCount = 0; private int curFindIndex = 0; private int curFindLength = 0; /// <summary> /// 查找关键字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void FindMatchKeyword(object sender, EventArgs e) { gReplaceForm = (ReplaceForm)sender; string keyword = gReplaceForm.FromReplaceForm_Keyword; RichTextBox rtb = richTextBoxMain; FindText(richTextBoxMain, keyword, rtb.SelectionStart + rtb.SelectionLength); } private void FindText(RichTextBox rtb, String keyword, int curIndex) { int index = 0; string searchText = richTextBoxMain.Text.Substring(curIndex); index = searchText.IndexOf(keyword); if (index != -1) { findCount += 1; rtb.SelectionStart = index + curIndex; rtb.SelectionLength = keyword.Length; rtb.Focus(); curFindIndex = richTextBoxMain.SelectionStart; curFindLength = richTextBoxMain.SelectionLength; } else { if (findCount == 0) { MessageBox.Show("未找到匹配项"); } else { findCount = 0; MessageBox.Show("查找到了尽头"); } } } /// <summary> /// 替换关键字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void ReplaceMatchKeyword(object sender, EventArgs e) { gReplaceForm = (ReplaceForm)sender; string keyword = gReplaceForm.FromReplaceForm_Keyword; string replaceWord = gReplaceForm.FromReplaceForm_ReplaceWord; RichTextBox rtb = richTextBoxMain; if (curFindLength > 0) { //rtb.SelectionStart = curFindIndex; //rtb.SelectionLength = curFindLength; //rtb.SelectedText = replaceWord; //curFindIndex = curFindIndex + curFindLength + 1; //curFindLength = 0; string strTmp = rtb.Text.Remove(curFindIndex, curFindLength); rtb.Text = strTmp.Insert(curFindIndex, replaceWord); curFindLength = 0; } } /// <summary> /// 替换所有关键字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void replaceAllMatchKeyword(object sender, EventArgs e) { gReplaceForm = (ReplaceForm)sender; string keyword = gReplaceForm.FromReplaceForm_Keyword; string replaceWord = gReplaceForm.FromReplaceForm_ReplaceWord; int keywordIndex; int searchStart = 0; while (true) { keywordIndex = richTextBoxMain.Find(keyword, searchStart, RichTextBoxFinds.MatchCase); if (keywordIndex >= 0) { richTextBoxMain.SelectionStart = keywordIndex; richTextBoxMain.SelectionLength = keyword.Length; richTextBoxMain.SelectedText = replaceWord; searchStart = keywordIndex + keyword.Length + 1; } else { break; } } } // Format private void OnClickFormatWordWrap(object sender, EventArgs e) { if (MenuItemWordWrap.Checked == false) { this.richTextBoxMain.WordWrap = true; MenuItemWordWrap.Checked = true; } else { this.richTextBoxMain.WordWrap = false; MenuItemWordWrap.Checked = false; } } private void OnClickFormatFont(object sender, EventArgs e) { if (fontDialogMainForm.ShowDialog() == DialogResult.OK) { richTextBoxMain.Font = fontDialogMainForm.Font; } } private void OnClickFormatColor(object sender, EventArgs e) { if (colorDialogMainForm.ShowDialog() == DialogResult.OK) { richTextBoxMain.ForeColor = colorDialogMainForm.Color; } } // Parse private void OnClickParseCdt(object sender, EventArgs e) { gParseForm.parseCdt(gFilePath); } private void OnClickParseTS(object sender, EventArgs e) { } private void OnClickParsePat(object sender, EventArgs e) { } private void OnClickParsePmt(object sender, EventArgs e) { } private void OnClickParseSdt(object sender, EventArgs e) { } private void OnClickParseSdtt(object sender, EventArgs e) { } // Window private void OnOpenWinParse(object sender, EventArgs e) { gParseForm = new ParseForm(); LoadParseForm(); } private void OnOpenWinPicture(object sender, EventArgs e) { gPictureForm = new PictureForm(); gPictureForm.accept += new EventHandler(strPngAccept); gPictureForm.Show(); } /// <summary> /// 接收从Picture子窗体返回的图片二进制内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void strPngAccept(object sender, EventArgs e) { gPictureForm = (PictureForm)sender; this.richTextBoxMain.Text += gPictureForm.FromPngValue; } /// <summary> /// 批处理图片,转为流文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnTraverse(object sender, EventArgs e) { try { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string selectFolder = folderBrowserDialog1.SelectedPath; DirectoryInfo dirInfo = new DirectoryInfo(selectFolder); FileSystemInfo[] fSysInfos = dirInfo.GetFileSystemInfos(); foreach (FileSystemInfo fSysInfo in fSysInfos) { if (fSysInfo is DirectoryInfo) { DirectoryInfo subInfo = new DirectoryInfo(fSysInfo.FullName); richTextBoxMain.Text += subInfo.Name + "\r\n\t"; } else { FileInfo fInfo = new FileInfo(fSysInfo.FullName); string targetFolderName = "D:\\temp\\test\\"; if (!Directory.Exists(targetFolderName)) { Directory.CreateDirectory(targetFolderName); } string changeFileName = targetFolderName + fInfo.Name.Replace(".png", ".cdt"); byte[] imgBytes = ImgToBytes(fInfo.FullName); string strImg = Convert.ToBase64String(imgBytes); FileStream fsDat = new FileStream(changeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); BinaryWriter bwDat = new BinaryWriter(fsDat); bwDat.Write(strImg); bwDat.Flush(); bwDat.Close(); fsDat.Close(); } } MessageBox.Show("completed!"); } } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException - MainForm", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } protected static byte[] ImgToBytes(string gPicFilePath) { MemoryStream ms = null; try { ms = new MemoryStream(); Bitmap bmap = new Bitmap(gPicFilePath); bmap.Save(ms, ImageFormat.Png); ms.Flush(); byte[] pngBytes = ms.ToArray(); return pngBytes; } catch (ArgumentNullException ex) { throw ex; } finally { ms.Close(); } } // ToolScript private void OnToolFontBold(object sender, EventArgs e) { FontStyle fontBold = FontStyle.Bold; ChangeFontStyle(fontBold); } private void OnToolFontItalic(object sender, EventArgs e) { FontStyle fontItalic = FontStyle.Italic; ChangeFontStyle(fontItalic); } private void OnToolFontUnderline(object sender, EventArgs e) { FontStyle fontUnderline = FontStyle.Underline; ChangeFontStyle(fontUnderline); } private void OnToolFontRed(object sender, EventArgs e) { if (richTextBoxMain.SelectedText.Length == 0) { return; } Color colorStyle = this.richTextBoxMain.SelectionColor; if (colorStyle != Color.Red) { richTextBoxMain.SelectionColor = Color.Red; } else { richTextBoxMain.SelectionColor = Color.Black; } } /// <summary> /// 粗体、斜体、下划线统一处理函数 /// </summary> /// <param name="style"></param> private void ChangeFontStyle(FontStyle style) { RichTextBox tempRichTextBox = new RichTextBox(); //将要存放被选中文本的副本 int curRtbStart = richTextBoxMain.SelectionStart; int len = richTextBoxMain.SelectionLength; int tempRtbStart = 0; Font font = richTextBoxMain.SelectionFont; if (len <= 1 && font != null) //与上边的那段代码类似,功能相同 { if (style == FontStyle.Bold && font.Bold || style == FontStyle.Italic && font.Italic || style == FontStyle.Underline && font.Underline) { richTextBoxMain.SelectionFont = new Font(font, font.Style ^ style); } else if (style == FontStyle.Bold && !font.Bold || style == FontStyle.Italic && !font.Italic || style == FontStyle.Underline && !font.Underline) { richTextBoxMain.SelectionFont = new Font(font, font.Style | style); } return; } tempRichTextBox.Rtf = richTextBoxMain.SelectedRtf; //选中副本中的最后一个文字 //克隆被选中的文字Font,这个tempFont主要是用来判断 //最终被选中的文字是否要加粗、去粗、斜体、去斜、下划线、去下划线 tempRichTextBox.Select(len - 1, 1); Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone(); //清空2和3 for (int i = 0; i < len; i++) { tempRichTextBox.Select(tempRtbStart + i, 1); //每次选中一个,逐个进行加粗或去粗 if (style == FontStyle.Bold && tempFont.Bold || style == FontStyle.Italic && tempFont.Italic || style == FontStyle.Underline && tempFont.Underline) { tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont, tempRichTextBox.SelectionFont.Style ^ style); } else if (style == FontStyle.Bold && !tempFont.Bold || style == FontStyle.Italic && !tempFont.Italic || style == FontStyle.Underline && !tempFont.Underline) { tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont, tempRichTextBox.SelectionFont.Style | style); } } tempRichTextBox.Select(tempRtbStart, len); richTextBoxMain.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型 richTextBoxMain.Select(curRtbStart, len); } } }