protected override void Print_PrintPage(object sender, PrintPageEventArgs e) { if (printingPage == -1) { printingPage = startPage; } else { printingPage++; } int rx = e.PageSettings.PrinterResolution.X, ry = e.PageSettings.PrinterResolution.Y; const int rdef = 300; OnPagePrinded(new PageEventArgs(printingPage - startPage)); var pi = images[printingPage - startPage] as Tiff.PageInfo; if (pi != null && pi.Image != null) { Image img = pi.Image; if (annotations) { var renderAnnotations = new RenderAnnotations(0); byte[] notes = pi.Annotation; if (notes != null) { Bitmap bitmapImg; using (TiffAnnotation tiffAnnotation = new TiffAnnotation(null)) { tiffAnnotation.Parse(notes); bitmapImg = new Bitmap(img); if (img.HorizontalResolution == 0) { bitmapImg.SetResolution(200, 200); } else { bitmapImg.SetResolution(img.HorizontalResolution, img.VerticalResolution); } using (Graphics g = Graphics.FromImage(bitmapImg)) { g.InterpolationMode = InterpolationMode.High; Rectangle[] rects = null; renderAnnotations.DrawAnnotation(g, tiffAnnotation, ImageControl.ImageControl.AllMarkGroupsVisibleList, bitmapImg, InterpolationMode.High, ref rects, null); } } img = bitmapImg; } } float hr = img.HorizontalResolution > 0 ? img.HorizontalResolution : 200; float vr = img.VerticalResolution > 0 ? img.VerticalResolution : 200; if (scaleMode == 1) { e.Graphics.DrawImage(img, 0f, 0f, img.Width * 100f / hr, img.Height * 100f / vr); if (images[printingPage - startPage] is PrintImageInfo) { PrintImageInfo ii = images[printingPage - startPage] as PrintImageInfo; if (ii != null && ii.StampsItems != null && ii.StampsItems.Length > 0) { float drez = img.VerticalResolution / img.HorizontalResolution; foreach (StampItem si in ii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * 100 / hr + (simg.Width * si.Zoom) / 2 / hr, si.Y * 100 / vr + (simg.Height * si.Zoom * drez) / 2 / vr)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * 100f / hr, si.Y * 100f / vr, (simg.Width * si.Zoom) / hr, (simg.Height * si.Zoom * drez) / vr); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (ii != null && ii.IsAnnuled) { DrowAnnuledString(e); } } } else { IntPtr hdc = e.Graphics.GetHdc(); int realwidth = GetDeviceCaps(hdc, HORZRES); int realheight = GetDeviceCaps(hdc, VERTRES); e.Graphics.ReleaseHdc(hdc); if (rx < 1) { rx = rdef; } if (ry < 1) { ry = rx; } if (realwidth > 0) { realwidth = realwidth * 100 / rx; } else { realwidth = e.PageBounds.Width * 100 / ry; } if (realheight > 0) { realheight = realheight * 100 / ry; } else { realheight = e.PageBounds.Height * 100 / ry; } bool isScale = img.Width > (realwidth) * vr / 100f || img.Height > realheight * hr / 100f; bool isWidth = img.Width / hr / realwidth > img.Height / vr / realheight; if (isScale) { int swidth; int sheight; if (isWidth) { swidth = realwidth; sheight = (int)(swidth / (float)img.Width * hr / vr * img.Height); } else { sheight = realheight; swidth = (int)(sheight / (float)img.Height * vr / hr * img.Width); } e.Graphics.DrawImage(img, 0, 0, swidth, sheight); float scale = realwidth * vr / 100f / img.Width; if (!isWidth) { scale = realheight * vr / 100f / img.Height; } if (images[printingPage - startPage] is PrintImageInfo) { var ii = images[printingPage - startPage] as PrintImageInfo; if (ii != null && ii.StampsItems != null && ii.StampsItems.Length > 0) { float drez = img.VerticalResolution / img.HorizontalResolution; foreach (StampItem si in ii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * 100 * scale / hr + (simg.Width * si.Zoom * scale) / 2 / hr, si.Y * 100 * scale / vr + (simg.Height * si.Zoom * scale * drez) / 2 / vr)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * 100 * scale / hr, si.Y * 100 * scale / vr, (simg.Width * scale * si.Zoom) / hr, (simg.Height * scale * si.Zoom * drez) / vr); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (ii != null && ii.IsAnnuled) { DrowAnnuledString(e); } } } else { e.Graphics.DrawImage(img, 0f, 0f, img.Width * 100f / hr, img.Height * 100f / vr); if (images[printingPage - startPage] is PrintImageInfo) { var ii = images[printingPage - startPage] as PrintImageInfo; if (ii != null && ii.StampsItems != null && ii.StampsItems.Length > 0) { float drez = img.VerticalResolution / img.HorizontalResolution; foreach (StampItem si in ii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * 100 / hr + (simg.Width * si.Zoom) / 2 / hr, si.Y * 100 / vr + (simg.Height * si.Zoom * drez) / 2 / vr)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * 100f / hr, si.Y * 100f / vr, (simg.Width * si.Zoom) / hr, (simg.Height * si.Zoom * drez) / vr); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (ii != null && ii.IsAnnuled) { DrowAnnuledString(e); } } } } } else { float hr = rx, vr = ry; var pii = images[printingPage - startPage] as PrintImageInfo; if (pii == null) { e.HasMorePages = false; OnEndPrint(); return; } if (scaleMode == 1) { pii.ResX = rx; pii.ResY = ry; Bitmap[] bml = pii.GetMatrix(); if (bml != null) { for (int i = 0; i < bml.Length; i++) { e.Graphics.DrawImage(bml[i], 0f, (bml[0].Height * 100f / ry) * i, (bml[i].Width * 100f / rx), (bml[i].Height * 100f / ry)); if (i > 0) { bml[i].Dispose(); } } if (bml.Length > 0) { bml[0].Dispose(); } bml = null; } if (pii.StampsItems != null && pii.StampsItems.Length > 0) { float drez = ry / rx; foreach (StampItem si in pii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * 100 / rx + (simg.Width * si.Zoom) / 2 / rx, si.Y * 100 / ry + (simg.Height * si.Zoom * drez) / 2 / ry)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * 100f / rx, si.Y * 100f / ry, (simg.Width * si.Zoom) / ry, (simg.Height * si.Zoom * drez) / ry); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (pii.IsAnnuled) { DrowAnnuledString(e); } } else { IntPtr hdc = e.Graphics.GetHdc(); int realwidth = GetDeviceCaps(hdc, HORZRES); int realheight = GetDeviceCaps(hdc, VERTRES); int wid = pii.GetWidth(); int hei = pii.GetHeight(); e.Graphics.ReleaseHdc(hdc); if (rx < 1) { rx = rdef; } if (ry < 1) { ry = rx; } if (realwidth > 0) { realwidth = realwidth * 100 / rx; } else { realwidth = e.PageBounds.Width * 100 / ry; } if (realheight > 0) { realheight = realheight * 100 / ry; } else { realheight = e.PageBounds.Height * 100 / ry; } bool isScale = wid * 100f > realwidth * 72f || hei * 100f > realheight * 72f; bool isWidth = wid * 1f / realwidth > hei * 1f / realheight; if (isScale) { int swidth; int sheight; if (isWidth) { swidth = realwidth; sheight = (int)(swidth * hr / vr / wid * hei); } else { sheight = realheight; swidth = (int)(sheight * vr / hr / hei * wid); } hr *= swidth * 72 / 100f / wid; vr *= sheight * 72 / 100f / hei; float scale = vr / ry; if (!isWidth) { scale = hr / rx; } pii.ResX = (int)hr; pii.ResY = (int)vr; Bitmap[] bml = pii.GetMatrix(); if (bml != null) { for (int i = 0; i < bml.Length; i++) { e.Graphics.DrawImage(bml[i], 0f, (bml[0].Height * 100f / ry) * i, swidth, (bml[i].Height * 100f / ry)); if (i > 0) { bml[i].Dispose(); } } if (bml.Length > 0) { bml[0].Dispose(); } bml = null; } if (pii.StampsItems != null && pii.StampsItems.Length > 0) { float drez = ry / rx; foreach (StampItem si in pii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * scale * 100 / rx + (simg.Width * scale * si.Zoom) / 2 / rx, si.Y * scale * 100 / ry + (simg.Height * scale * si.Zoom * drez) / 2 / ry)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * scale * 100f / rx, si.Y * scale * 100f / ry, (simg.Width * scale * si.Zoom) / ry, (simg.Height * scale * si.Zoom * drez) / ry); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (pii.IsAnnuled) { DrowAnnuledString(e); } } else { pii.ResX = rx; pii.ResY = ry; Bitmap[] bml = pii.GetMatrix(); if (bml != null) { for (int i = 0; i < bml.Length; i++) { e.Graphics.DrawImage(bml[i], 0f, (bml[0].Height * 100f / ry) * i, realwidth, (bml[i].Height * 100f / ry)); if (i > 0) { bml[i].Dispose(); } } if (bml.Length > 0) { bml[0].Dispose(); } bml = null; } if (pii.StampsItems != null && pii.StampsItems.Length > 0) { float drez = ry / rx; foreach (StampItem si in pii.StampsItems) { if (si.TypeID == 101) { continue; } Image simg = Environment.GetStamp(si.StampID, si.ImageID); if (!si.Rotate.Equals(0)) { Matrix mx = new Matrix(); mx.RotateAt(si.Rotate, new PointF(si.X * 100 / rx + (simg.Width * si.Zoom) / 2 / rx, si.Y * 100 / ry + (simg.Height * si.Zoom * drez) / 2 / ry)); e.Graphics.Transform = mx; } e.Graphics.DrawImage(simg, si.X * 100f / rx, si.Y * 100f / ry, (simg.Width * si.Zoom) / ry, (simg.Height * si.Zoom * drez) / ry); if (!si.Rotate.Equals(0)) { e.Graphics.ResetTransform(); } } } if (pii.IsAnnuled) { DrowAnnuledString(e); } } } } if (printingPage < endPage) { SetOrientation(IsLand(images[printingPage - startPage + 1] as Tiff.PageInfo), e.PageSettings); e.HasMorePages = true; } else { e.HasMorePages = false; OnEndPrint(); } }
protected virtual void Print_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (printingPage == -1) { printingPage = startPage; } else { printingPage++; } Tiff.PageInfo pi = images[printingPage - startPage] as Tiff.PageInfo; if (pi != null && pi.Image != null) { OnPagePrinded(new PageEventArgs(printingPage - 1)); System.Drawing.Image img = pi.Image; if (annotations) { RenderAnnotations renderAnnotations = new RenderAnnotations(0); byte[] notes = pi.Annotation; TiffAnnotation tiffAnnotation = null; if (notes != null) { tiffAnnotation = new TiffAnnotation(null); tiffAnnotation.Parse(notes); Bitmap bitmapImg = new Bitmap(img); if (img.HorizontalResolution == 0 || img.VerticalResolution == 0) { bitmapImg.SetResolution(200, 200); } else { bitmapImg.SetResolution(img.HorizontalResolution, img.VerticalResolution); } using (Graphics g = Graphics.FromImage(bitmapImg)) { g.InterpolationMode = InterpolationMode.High; Rectangle[] rects = null; renderAnnotations.DrawAnnotation(g, tiffAnnotation, ImageControl.AllMarkGroupsVisibleList, bitmapImg, InterpolationMode.High, ref rects, null); } img = bitmapImg; } } //e.Graphics.PageUnit = GraphicsUnit.Pixel; if (scaleMode == 1) { float hr = (img.HorizontalResolution > 0) ? img.HorizontalResolution : 200f; float vr = (img.VerticalResolution > 0) ? img.VerticalResolution : 200f; e.Graphics.DrawImage(img, 0f, 0f, img.Width * 100f / hr, img.Height * 100f / vr); } else { IntPtr hdc = e.Graphics.GetHdc(); int realwidth = GetDeviceCaps(hdc, HORZRES); int realheight = GetDeviceCaps(hdc, VERTRES); e.Graphics.ReleaseHdc(hdc); int rx = e.PageSettings.PrinterResolution.X, ry = e.PageSettings.PrinterResolution.Y, rdef = 300; if (rx < 1) { rx = rdef; } if (ry < 1) { ry = rx; } if (realwidth > 0) { realwidth = realwidth * 100 / rx; } else { realwidth = e.PageBounds.Width * 100 / rx; } if (realheight > 0) { realheight = realheight * 100 / ry; } else { realheight = e.PageBounds.Height * 100 / ry; } bool isScale = (float)img.Width > (float)(realwidth) * img.VerticalResolution / 100f || img.Height > realheight * img.HorizontalResolution / 100f; bool isWidth = img.Width / img.HorizontalResolution / realwidth > img.Height / img.VerticalResolution / realheight; if (isScale) { int swidth; int sheight; if (isWidth) { swidth = realwidth; sheight = (int)((float)swidth / (float)img.Width * img.HorizontalResolution / img.VerticalResolution * (float)img.Height); } else { sheight = realheight; swidth = (int)((float)sheight / (float)img.Height * img.VerticalResolution / img.HorizontalResolution * (float)img.Width); } e.Graphics.DrawImage(img, 0, 0, swidth, sheight); } else { e.Graphics.DrawImage(img, 0f, 0f, (float)img.Width * 100f / img.HorizontalResolution, (float)img.Height * 100f / img.VerticalResolution); } } } if (printingPage < endPage) { Image img = (images[printingPage - startPage] as Tiff.PageInfo).Image; SetOrientation(img.Width * img.VerticalResolution > img.Height * img.HorizontalResolution, e.PageSettings); e.HasMorePages = true; } else { e.HasMorePages = false; OnEndPrint(); } }
public virtual void DrawAnnotation(Graphics g, object tiffAnnotation, Hashtable markGroupsVisibleList, Bitmap bitmapImg, InterpolationMode CurrentInterpolationMode, ref Rectangle[] SelectedRectangles, System.Collections.Specialized.ListDictionary notesToSelectedRectangles) { if (notesToSelectedRectangles != null) { notesToSelectedRectangles.Clear(); } TiffAnnotation annotation = tiffAnnotation as TiffAnnotation; if (annotation != null) { ArrayList figuresList = annotation.GetFigures(false); foreach (object figure in figuresList) { TiffAnnotation.IBufferBitmap bb = figure as TiffAnnotation.IBufferBitmap; if (bb != null && markGroupsVisibleList.ContainsKey(bb.Attributes.OiGroup) && ((bool)markGroupsVisibleList[bb.Attributes.OiGroup])) { switch (figure.GetType().Name) { case "ImageEmbedded": TiffAnnotation.ImageEmbedded img = (TiffAnnotation.ImageEmbedded)figure; g.DrawImage(img.Img, img.LrBounds.Location.X, img.LrBounds.Location.Y, img.LrBounds.Size.Width, img.LrBounds.Size.Height); if (bb.Selected) { DrawSelectedRectangle(g, bb.Rect, ref SelectedRectangles); } break; case "StraightLine": TiffAnnotation.StraightLine line = (TiffAnnotation.StraightLine)figure; if (line.LinePoints == null) { continue; } g.DrawLine(new Pen(new SolidBrush(line.RgbColor1), Convert.ToSingle(line.ULineSize)), line.LinePoints[0], line.LinePoints[1]); break; case "FreehandLine": TiffAnnotation.FreehandLine fline = (TiffAnnotation.FreehandLine)figure; if (fline.LinePoints == null) { continue; } for (int i = 0; i < fline.LinePoints.Length; i += 2) { if (i != 0) { g.DrawLine(new Pen(new SolidBrush(fline.RgbColor1), Convert.ToSingle(fline.ULineSize)), fline.LinePoints[i - 1], fline.LinePoints[i]); } g.DrawLine(new Pen(new SolidBrush(fline.RgbColor1), Convert.ToSingle(fline.ULineSize)), fline.LinePoints[i], fline.LinePoints[i + 1]); } break; case "HollowRectangle": { TiffAnnotation.HollowRectangle rect = (TiffAnnotation.HollowRectangle)figure; Bitmap bitmapUp = bb.GetBitmap(bitmapImg, CurrentInterpolationMode); g.DrawImage(bitmapUp, rect.LrBounds.X, rect.LrBounds.Y); if (bb.Selected) { DrawSelectedRectangle(g, rect.LrBounds, ref SelectedRectangles); } } break; case "FilledRectangle": { TiffAnnotation.FilledRectangle frect = (TiffAnnotation.FilledRectangle)figure; Bitmap bitmapUp = bb.GetBitmap(bitmapImg, CurrentInterpolationMode); g.DrawImage(bitmapUp, frect.LrBounds.X, frect.LrBounds.Y); if (bb.Selected) { DrawSelectedRectangle(g, frect.LrBounds, ref SelectedRectangles); } } break; case "TypedText": TiffAnnotation.TypedText tt = (TiffAnnotation.TypedText)figure; StringFormat sf = new StringFormat(); System.Drawing.Drawing2D.Matrix mx = null; Rectangle newRect = tt.LrBounds; switch (tt.TextPrivateData.NCurrentOrientation) { case 900: mx = new System.Drawing.Drawing2D.Matrix(); newRect = new Rectangle(tt.LrBounds.X, tt.LrBounds.Y + tt.LrBounds.Height, tt.LrBounds.Height, tt.LrBounds.Width); mx.RotateAt(270, new PointF(newRect.X, newRect.Y)); g.Transform = mx; break; case 1800: mx = new System.Drawing.Drawing2D.Matrix(); newRect = tt.LrBounds; mx.RotateAt(180, new PointF(tt.LrBounds.Location.X + tt.LrBounds.Width / 2, tt.LrBounds.Location.Y + tt.LrBounds.Height / 2)); g.Transform = mx; break; case 2700: mx = new System.Drawing.Drawing2D.Matrix(); newRect = new Rectangle(tt.LrBounds.X + tt.LrBounds.Width, tt.LrBounds.Y, tt.LrBounds.Height, tt.LrBounds.Width); mx.RotateAt(90, new PointF(newRect.X, newRect.Y)); g.Transform = mx; break; } g.TextRenderingHint = tt.FontRenderingHint; sf.Trimming = StringTrimming.Word; using (Font f = new Font(tt.LfFont.FontFamily, tt.LfFont.SizeInPoints, tt.LfFont.Style)) g.DrawString(tt.TextPrivateData.SzAnoText, f, new SolidBrush(tt.RgbColor1), newRect, sf); g.ResetTransform(); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; if (bb.Selected) { DrawSelectedRectangle(g, bb.Rect, ref SelectedRectangles); Pen p = new Pen(Brushes.Black); p.DashStyle = DashStyle.Dash; g.DrawRectangle(p, new Rectangle(bb.Rect.X - 1, bb.Rect.Y - 1, bb.Rect.Width + 2, bb.Rect.Height + 2)); } break; case "TextStump": TiffAnnotation.TextStump ts = (TiffAnnotation.TextStump)figure; StringFormat sf3 = new StringFormat(); g.TextRenderingHint = ts.FontRenderingHint; g.DrawString(ts.TextPrivateData.SzAnoText, ts.LfFont, new SolidBrush(ts.RgbColor1), ts.LrBounds, sf3); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; break; case "TextFromFile": TiffAnnotation.TextFromFile tf = (TiffAnnotation.TextFromFile)figure; StringFormat sf2 = new StringFormat(); g.TextRenderingHint = tf.FontRenderingHint; g.DrawString(tf.TextPrivateData.SzAnoText, tf.LfFont, new SolidBrush(tf.RgbColor1), tf.LrBounds, sf2); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; break; case "AttachANote": TiffAnnotation.AttachANote an = (TiffAnnotation.AttachANote)figure; StringFormat sf1 = new StringFormat(); g.TextRenderingHint = an.FontRenderingHint; g.FillRectangle(Brushes.Black, an.LrBounds.X + 2, an.LrBounds.Y + 2, an.LrBounds.Width, an.LrBounds.Height); g.FillRectangle(new SolidBrush(an.RgbColor1), an.LrBounds); g.DrawRectangle(Pens.Black, an.LrBounds.X, an.LrBounds.Y, an.LrBounds.Width, an.LrBounds.Height); System.Drawing.Drawing2D.Matrix mx1 = null; Rectangle newRect1 = an.LrBounds; switch (an.TextPrivateData.NCurrentOrientation) { case 900: mx1 = new System.Drawing.Drawing2D.Matrix(); newRect1 = new Rectangle(an.LrBounds.X, an.LrBounds.Y + an.LrBounds.Height, an.LrBounds.Height, an.LrBounds.Width); mx1.RotateAt(270, new PointF(newRect1.X, newRect1.Y)); g.Transform = mx1; break; case 1800: mx1 = new System.Drawing.Drawing2D.Matrix(); newRect1 = an.LrBounds; mx1.RotateAt(180, new PointF(an.LrBounds.Location.X + an.LrBounds.Width / 2, an.LrBounds.Location.Y + an.LrBounds.Height / 2)); g.Transform = mx1; break; case 2700: mx1 = new System.Drawing.Drawing2D.Matrix(); newRect1 = new Rectangle(an.LrBounds.X + an.LrBounds.Width, an.LrBounds.Y, an.LrBounds.Height, an.LrBounds.Width); mx1.RotateAt(90, new PointF(newRect1.X, newRect1.Y)); g.Transform = mx1; break; } g.DrawString(an.TextPrivateData.SzAnoText, new Font(an.LfFont.FontFamily, an.LfFont.SizeInPoints, an.LfFont.Style), new SolidBrush(an.RgbColor2), newRect1, sf1); g.ResetTransform(); if (bb.Selected) { DrawSelectedRectangle(g, bb.Rect, ref SelectedRectangles); } g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; break; } if (notesToSelectedRectangles != null && SelectedRectangles != null && SelectedRectangles.Length > 0 && bb.Selected) { notesToSelectedRectangles.Add(bb, SelectedRectangles); } } } } }