/// <summary> /// Convert Bytes to Metafile. /// </summary> public static Metafile BytesToMetafile(byte[] bytes) { MemoryStream stream = new MemoryStream(bytes); Metafile mf = Metafile.FromStream(stream) as Metafile; return(mf); }
public string GetSelectionImg(string paperName) { cutTimes = 1; cutTimesCount = 0; const int MAX_height = 3000; //string timeResult = ""; Range range = Globals.ThisAddIn.Application.Selection.Range; Range range2 = Globals.ThisAddIn.Application.Selection.Range; //timeResult = timeResult + "Time1:" + DateTime.Now.ToString() + "\n"; string imgName = Guid.NewGuid().ToString() + ".png"; if (!Directory.Exists(Globals.ThisAddIn.exerciseJsonPath + paperName)) { Directory.CreateDirectory(Globals.ThisAddIn.exerciseJsonPath + paperName); } double zoom = 0.33; const int imgWidth = 1188; Image imgTemp = Metafile.FromStream(new MemoryStream(range.EnhMetaFileBits)); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); if (MAX_height < imgTemp.Height) { Paragraphs paragraphs = range.Paragraphs; Tables tables = range.Tables; List <StartEnd> paragraphList = new List <StartEnd>(); List <StartEnd> tableList = new List <StartEnd>(); for (int i = 0; i < paragraphs.Count; i++) { Paragraph paragraph = paragraphs[i + 1]; StartEnd startEnd = new StartEnd(); startEnd.Start = paragraph.Range.Start; startEnd.End = paragraph.Range.End; paragraphList.Add(startEnd); } for (int i = 0; i < tables.Count; i++) { Table table = tables[i + 1]; StartEnd startEnd = new StartEnd(); startEnd.Start = table.Range.Start; startEnd.End = table.Range.End; tableList.Add(startEnd); } List <StartEnd> resultList = CutRange(paragraphList, tableList); List <StartEnd> finalImgRangeList = new List <StartEnd>(); for (int i = 0; i < resultList.Count; i++) { StartEnd startendImg = new StartEnd(); startendImg.Start = resultList[i].Start; startendImg.End = resultList[i].End; for (int j = i; j < resultList.Count; j++) { range2.SetRange((int)resultList[i].Start, (int)resultList[j].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Height < MAX_height) { startendImg.End = resultList[j].End; if (j == resultList.Count - 1) { i = j; } } else { if (i == j) { MessageBox.Show("请确定没有超过一页的段落或表格"); Globals.ThisAddIn.Application.ActiveWindow.ScrollIntoView(range2); return(""); } i = j - 1; break; } } finalImgRangeList.Add(startendImg); } cutTimes = finalImgRangeList.Count; //timeResult = timeResult + "Time2:" + DateTime.Now.ToString() + "\n"; int allImgHeight = 0; int allImgWidth = 0; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Width > allImgWidth) { allImgWidth = img.Width; } allImgHeight += img.Height; } //for(int i=0;i<resultList.Count;i++) //{ // int start=(int)resultList[i].Start; // int end=(int)resultList[i].End; // range2.SetRange(start,end); // if (i % 2 == 0) // range2.HighlightColorIndex = WdColorIndex.wdRed; // else // range2.HighlightColorIndex = WdColorIndex.wdYellow; //} //zoom = (double)imgWidth / (double)allImgWidth; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(allImgHeight * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; int startPosition = 0; double oldZoom = zoom; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if ((double)imgWidth / (double)img.Width < zoom) { zoom = (double)imgWidth / (double)img.Width; } gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom)); gx.DrawImage(img, new System.Drawing.Rectangle(0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom))); startPosition += (int)(img.Height * zoom); zoom = oldZoom; cutTimesCount = i + 1; } //bmp = KiSharpen(bmp,(float)0.3); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } else { //zoom = (double)imgWidth / (double)imgTemp.Width; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(imgTemp.Height * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom)); gx.DrawImage(imgTemp, new System.Drawing.Rectangle(0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom))); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } //timeResult = timeResult + "Time3:" + DateTime.Now.ToString() + "\n"; cutTimes = 1; cutTimesCount = 0; //MessageBox.Show(timeResult); return(imgName); }
public void ToImg(string From, string To) { Word.Application app = new Microsoft.Office.Interop.Word.Application(); Word.Document doc = null; object unknow = Type.Missing; app.Visible = false; object file = From; doc = app.Documents.Open(ref file, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow); cutTimes = 1; cutTimesCount = 0; const int MAX_height = 3000; Range range = doc.Range(); Range range2 = doc.Range(); double zoom = 0.33; const int imgWidth = 1188; Image imgTemp = Metafile.FromStream(new MemoryStream(range.EnhMetaFileBits)); if (MAX_height < imgTemp.Height) { Paragraphs paragraphs = range.Paragraphs; Tables tables = range.Tables; List <StartEnd> paragraphList = new List <StartEnd>(); List <StartEnd> tableList = new List <StartEnd>(); for (int i = 0; i < paragraphs.Count; i++) { Paragraph paragraph = paragraphs[i + 1]; StartEnd startEnd = new StartEnd(); startEnd.Start = paragraph.Range.Start; startEnd.End = paragraph.Range.End; paragraphList.Add(startEnd); } for (int i = 0; i < tables.Count; i++) { Table table = tables[i + 1]; StartEnd startEnd = new StartEnd(); startEnd.Start = table.Range.Start; startEnd.End = table.Range.End; tableList.Add(startEnd); } List <StartEnd> resultList = CutRange(paragraphList, tableList); List <StartEnd> finalImgRangeList = new List <StartEnd>(); for (int i = 0; i < resultList.Count; i++) { StartEnd startendImg = new StartEnd(); startendImg.Start = resultList[i].Start; startendImg.End = resultList[i].End; for (int j = i; j < resultList.Count; j++) { range2.SetRange((int)resultList[i].Start, (int)resultList[j].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Height < MAX_height) { startendImg.End = resultList[j].End; if (j == resultList.Count - 1) { i = j; } } else { if (i == j) { MessageBox.Show("请确定没有超过一页的段落或表格"); //doc.Application.ActiveWindow.ScrollIntoView(range2); } i = j - 1; break; } } finalImgRangeList.Add(startendImg); } cutTimes = finalImgRangeList.Count; //timeResult = timeResult + "Time2:" + DateTime.Now.ToString() + "\n"; int allImgHeight = 0; int allImgWidth = 0; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Width > allImgWidth) { allImgWidth = img.Width; } allImgHeight += img.Height; } //zoom = (double)imgWidth / (double)allImgWidth; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(allImgHeight * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; int startPosition = 0; double oldZoom = zoom; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if ((double)imgWidth / (double)img.Width < zoom) { zoom = (double)imgWidth / (double)img.Width; } gx.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom)); gx.DrawImage(img, new System.Drawing.Rectangle(0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom))); startPosition += (int)(img.Height * zoom); zoom = oldZoom; cutTimesCount = i + 1; } //bmp = KiSharpen(bmp,(float)0.3); bmp.Save(To, System.Drawing.Imaging.ImageFormat.Png); } else { //zoom = (double)imgWidth / (double)imgTemp.Width; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(imgTemp.Height * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; gx.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom)); gx.DrawImage(imgTemp, new System.Drawing.Rectangle(0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom))); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); bmp.Save(To, System.Drawing.Imaging.ImageFormat.Png); } //timeResult = timeResult + "Time3:" + DateTime.Now.ToString() + "\n"; doc.Close(); }
/// /////////////////////////////////////// protected override CResultAErreur MySerialize(C2iSerializer serializer) { int nVersion = GetNumVersion(); CResultAErreur result = serializer.TraiteVersion(ref nVersion); if (!result) { return(result); } bool bHasImage = m_metafileDefault != null; serializer.TraiteBool(ref bHasImage); if (bHasImage) { switch (serializer.Mode) { case ModeSerialisation.Lecture: Byte[] bt = null; serializer.TraiteByteArray(ref bt); if (m_metafileDefault != null) { m_metafileDefault.Dispose(); } m_metafileDefault = null; MemoryStream stream = new MemoryStream(bt); try { Metafile meta = (Metafile)Metafile.FromStream(stream); m_metafileDefault = meta; } catch { m_metafileDefault = null; } stream.Close(); break; case ModeSerialisation.Ecriture: try { int nHandle = m_metafileDefault.GetHenhmetafile().ToInt32(); int nBufSize = GetEnhMetaFileBits(nHandle, 0, null); byte[] buffer = new byte[nBufSize]; if (GetEnhMetaFileBits(nHandle, nBufSize, buffer) > 0) { serializer.TraiteByteArray(ref buffer); } else { buffer = new byte[0]; serializer.TraiteByteArray(ref buffer); } } catch (Exception e) { string strVal = e.ToString(); } break; } } return(result); }
public C1XLBook CreateSample() { // create C1XLBook C1XLBook wb = new C1XLBook(); // three methods add images to Excel file XLPictureShape picture; // create images Metafile metafile = (Metafile)Metafile.FromStream(GetManifestResource("meta.emf")); Image spbImage = Image.FromStream(GetManifestResource("spb.jpg")); Image canadaImage = Image.FromStream(GetManifestResource("canada.bmp")); Image googleImage = Image.FromStream(GetManifestResource("google.bmp")); Image babyImage = Image.FromStream(GetManifestResource("baby.png")); ///////////////////////////////////////////////////////// // List "Images" -- three methods add images ///////////////////////////////////////////////////////// XLSheet sheet = wb.Sheets[0]; sheet.Name = "Images"; sheet.PrintSettings.Header = "&LCanada:&G&D&CHeader Center&R&P"; sheet.PrintSettings.Footer = "&C&Z"; // first method picture = new XLPictureShape(googleImage, 0, 0, 2200, 5000); picture.DashedLineStyle = XLShapeDashedLineStyleEnum.Solid; picture.LineStyle = XLShapeLineStyleEnum.Simple; picture.LineColor = Color.BlueViolet; picture.Rotation = 90.0f; picture.LineWidth = 10; sheet[1, 7].Value = picture; sheet[1, 1].Value = metafile; // second method picture = new XLPictureShape(spbImage, 100, 3000, 8000, 6000); //picture = new XLPictureShape(canadaImage); picture.Brightness = .55f; picture.Contrast = .5f; sheet.Shapes.Add(picture); picture = new XLPictureShape(canadaImage); picture.LineColor = Color.Aqua; picture.LineWidth = 100; sheet.PrintSettings.HeaderPictureLeft = picture; // third method picture = new XLPictureShape(babyImage); picture.Rotation = 30.0f; picture.LineColor = Color.Aqua; picture.LineWidth = 100; sheet[15, 2].Value = picture; // header or footer image //picture = new XLPictureShape(spbImage); //picture.Rotation = 45.0f; // for header or footer ignored //picture.LineColor = Color.Blue; //picture.LineWidth = 50; //picture.Brightness = 0.4f; //sheet.PrintSettings.HeaderPictureCenter = picture; ///////////////////////////////////////////////////////// // List "Types" -- support image types (bmp, png, jpg, emf) ///////////////////////////////////////////////////////// sheet = wb.Sheets.Add("Types"); sheet[1, 0].Value = "Bmp:"; sheet[1, 1].Value = googleImage; sheet[8, 0].Value = "Png:"; sheet[8, 1].Value = babyImage; sheet[25, 0].Value = "Jpeg:"; sheet[25, 1].Value = spbImage; sheet[34, 0].Value = "Emf:"; sheet[34, 1].Value = metafile; ///////////////////////////////////////////////////////// // List "Borders" -- various picture borders ///////////////////////////////////////////////////////// sheet = wb.Sheets.Add("Borders"); int row = 1, col = 0; foreach (XLShapeLineStyleEnum style in Enum.GetValues(typeof(XLShapeLineStyleEnum))) { col = 1; sheet.Rows[row].Height = 3700; foreach (XLShapeDashedLineStyleEnum dashedStyle in Enum.GetValues(typeof(XLShapeDashedLineStyleEnum))) { sheet.Columns[col].Width = 2300; picture = new XLPictureShape(babyImage); picture.LineWidth = 100; picture.LineColor = Color.FromArgb(100, 200, Math.Min(col * 12, 255)); picture.DashedLineStyle = dashedStyle; picture.LineStyle = style; sheet[row, col].Value = picture; sheet[row + 1, col].Value = "style: " + style.ToString(); sheet[row + 2, col].Value = "dashed: " + dashedStyle.ToString(); col += 2; } row += 4; } ///////////////////////////////////////////////////////// // List "Alignment" -- position image using ContentAlignment ///////////////////////////////////////////////////////// sheet = wb.Sheets.Add("Alignment"); row = 1; sheet.Columns[1].Width = sheet.Columns[4].Width = 6000; sheet.Columns[7].Width = sheet.Columns[10].Width = 2000; foreach (ContentAlignment alignment in Enum.GetValues(typeof(ContentAlignment))) { sheet.Rows[row].Height = 2400; Size cellSize = new Size(sheet.Columns[1].Width, sheet.Rows[row].Height); picture = new XLPictureShape(googleImage, cellSize, alignment, ImageScaling.Clip); sheet[row, 1].Value = picture; sheet[row, 2].Value = "clip: " + alignment.ToString(); picture = new XLPictureShape(googleImage, cellSize, alignment, ImageScaling.Scale); sheet[row, 4].Value = picture; sheet[row, 5].Value = "scale: " + alignment.ToString(); row += 4; } ///////////////////////////////////////////////////////// // List "Properties" -- various picture properties ///////////////////////////////////////////////////////// sheet = wb.Sheets.Add("Properties"); // associating hyperlink with the shape sheet.Rows[1].Height = 2000; sheet.Columns[1].Width = 3600; picture = new XLPictureShape(spbImage); picture.Hyperlink = "http://www.spb.ru/"; sheet[1, 1].Value = picture; sheet[2, 1].Value = "hyperlink (click on the picture)"; // others view type col = 1; sheet.Rows[4].Height = 2000; foreach (XLPictureViewType viewType in Enum.GetValues(typeof(XLPictureViewType))) { sheet.Columns[col].Width = 3600; picture = new XLPictureShape(spbImage); picture.ViewType = viewType; sheet[4, col].Value = picture; sheet[5, col].Value = "view type: " + viewType.ToString();; col += 2; } // brightness & contrast col = 1; sheet.Rows[7].Height = sheet.Rows[10].Height = 2000; for (int i = 0; i <= 100; i += 10) { sheet.Columns[col].Width = 3600; picture = new XLPictureShape(spbImage); picture.Brightness = (float)i / 100; sheet[7, col].Value = picture; sheet[8, col].Value = string.Format("brightness: {0}", picture.Brightness); picture = new XLPictureShape(spbImage); picture.Contrast = (float)i / 100; sheet[10, col].Value = picture; sheet[11, col].Value = string.Format("contrast: {0}", picture.Contrast); col += 2; } // done return(wb); }