private void DrawImageSized(PageImage pi, Gdk.Pixbuf im, Cairo.Context g, Cairo.Rectangle r) { double height, width; // some work variables StyleInfo si = pi.SI; // adjust drawing rectangle based on padding // System.Drawing.RectangleF r2 = new System.Drawing.RectangleF(r.Left + PixelsX(si.PaddingLeft), // r.Top + PixelsY(si.PaddingTop), // r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), // r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); Cairo.Rectangle r2 = new Cairo.Rectangle(r.X + PixelsX(si.PaddingLeft), r.Y + PixelsY(si.PaddingTop), r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); Cairo.Rectangle ir; // int work rectangle switch (pi.Sizing) { case ImageSizingEnum.AutoSize: // // Note: GDI+ will stretch an image when you only provide // // the left/top coordinates. This seems pretty stupid since // // it results in the image being out of focus even though // // you don't want the image resized. // if (g.DpiX == im.HorizontalResolution && // g.DpiY == im.VerticalResolution) float imwidth = PixelsX(im.Width); float imheight = PixelsX(im.Height); ir = new Cairo.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), imwidth, imheight); // else // ir = new Cairo.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), // Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height)); //g.DrawImage(im, ir); im = im.ScaleSimple((int)r2.Width, (int)r2.Height, Gdk.InterpType.Hyper); g.DrawPixbufRect(im, ir); break; case ImageSizingEnum.Clip: // Region saveRegion = g.Clip; g.Save(); // Region clipRegion = new Region(g.Clip.GetRegionData()); // clipRegion.Intersect(r2); // g.Clip = clipRegion; g.Rectangle(r2); g.Clip(); // if (dpiX == im.HorizontalResolution && // dpiY == im.VerticalResolution) ir = new Cairo.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), im.Width, im.Height); // else // ir = new Cairo.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), // Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height)); // g.DrawImage(im, ir); g.DrawPixbufRect(im, ir); // g.Clip = saveRegion; g.Restore(); break; case ImageSizingEnum.FitProportional: double ratioIm = (float)im.Height / (float)im.Width; double ratioR = r2.Height / r2.Width; height = r2.Height; width = r2.Width; if (ratioIm > ratioR) { // this means the rectangle width must be corrected width = height * (1 / ratioIm); } else if (ratioIm < ratioR) { // this means the ractangle height must be corrected height = width * ratioIm; } r2 = new Cairo.Rectangle(r2.X, r2.Y, width, height); g.DrawPixbufRect(im, r2); break; case ImageSizingEnum.Fit: default: g.DrawPixbufRect(im, r2); break; } }
public ActionResult Get(int?width, int?height, string file, int page, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, int?rotate, int?zoom) { if (Utils.IsValidUrl(file)) { file = Utils.DownloadToStorage(file); } ViewerImageHandler handler = Utils.CreateViewerImageHandler(); ImageOptions options = new ImageOptions(); options.PageNumbersToRender = new List <int>(new int[] { page }); options.PageNumber = page; options.CountPagesToRender = 1; if (watermarkText != "") { options.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } if (width.HasValue) { int w = Convert.ToInt32(width); if (zoom.HasValue) { w = w + zoom.Value; } options.Width = w; } if (height.HasValue) { if (zoom.HasValue) { options.Height = options.Height + zoom.Value; } } if (rotate.HasValue) { if (rotate.Value > 0) { if (width.HasValue) { int side = options.Width; DocumentInfoContainer documentInfoContainer = handler.GetDocumentInfo(file); int pageAngle = documentInfoContainer.Pages[page - 1].Angle; if (pageAngle == 90 || pageAngle == 270) { options.Height = side; } else { options.Width = side; } } options.Transformations = Transformation.Rotate; handler.RotatePage(file, new RotatePageOptions(page, rotate.Value)); } } else { options.Transformations = Transformation.None; handler.RotatePage(file, new RotatePageOptions(page, 0)); } using (new InterProcessLock(file)) { List <PageImage> list = handler.GetPages(file, options); PageImage pageImage = list.Single(_ => _.PageNumber == page); return(File(pageImage.Stream, "image/png")); } }
private void DrawImageBackground(PageImage pi, StyleInfo si, StringBuilder sb, Rectangle r) { //Stream strm = null; //System.Drawing.Image im = null; //try //{ // strm = new MemoryStream(pi.ImageData); // im = System.Drawing.Image.FromStream(strm); // Rectangle r2 = new Rectangle(r.Left + PixelsX(si.PaddingLeft), // r.Top + PixelsY(si.PaddingTop), // r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), // r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); // int repeatX = 0; // int repeatY = 0; // switch (pi.Repeat) // { // case ImageRepeat.Repeat: // repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); // repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); // break; // case ImageRepeat.RepeatX: // repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); // repeatY = 1; // break; // case ImageRepeat.RepeatY: // repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); // repeatX = 1; // break; // case ImageRepeat.NoRepeat: // default: // repeatX = repeatY = 1; // break; // } // //make sure the image is drawn at least 1 times // repeatX = Math.Max(repeatX, 1); // repeatY = Math.Max(repeatY, 1); // float startX = r2.Left; // float startY = r2.Top; // Region saveRegion = g.Clip; // Region clipRegion = new Region(g.Clip.GetRegionData()); // clipRegion.Intersect(r2); // g.Clip = clipRegion; // for (int i = 0; i < repeatX; i++) // { // for (int j = 0; j < repeatY; j++) // { // float currX = startX + i * pi.SamplesW; // float currY = startY + j * pi.SamplesH; // g.DrawImage(im, new Rectangle(currX, currY, pi.SamplesW, pi.SamplesH)); // } // } // g.Clip = saveRegion; //} //finally //{ // if (strm != null) // strm.Close(); // if (im != null) // im.Dispose(); //} }
private void DrawImageSized(PageImage pi, Xwt.Drawing.Image im, Xwt.Drawing.Context g, Xwt.Rectangle r) { double height, width; // some work variables StyleInfo si = pi.SI; Xwt.Rectangle r2 = new Xwt.Rectangle(r.X + PixelsX(si.PaddingLeft), r.Y + PixelsY(si.PaddingTop), r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); Xwt.Rectangle ir; // int work rectangle switch (pi.Sizing) { case ImageSizingEnum.AutoSize: float imwidth = PixelsX((float)im.Size.Width); float imheight = PixelsX((float)im.Size.Height); ir = new Xwt.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), imwidth, imheight); im.Scale((int)r2.Width, (int)r2.Height); g.DrawImage(im, ir); break; case ImageSizingEnum.Clip: g.Save(); g.Rectangle(r2); g.Clip(); ir = new Xwt.Rectangle(Convert.ToInt32(r2.X), Convert.ToInt32(r2.Y), im.Size.Width, im.Size.Height); g.DrawImage(im, ir); g.Restore(); break; case ImageSizingEnum.FitProportional: double ratioIm = (float)im.Size.Height / (float)im.Size.Width; double ratioR = r2.Height / r2.Width; height = r2.Height; width = r2.Width; if (ratioIm > ratioR) { // this means the rectangle width must be corrected width = height * (1 / ratioIm); } else if (ratioIm < ratioR) { // this means the ractangle height must be corrected height = width * ratioIm; } r2 = new Xwt.Rectangle(r2.X, r2.Y, width, height); g.DrawImage(im, r2); break; case ImageSizingEnum.Fit: default: g.DrawImage(im, r2); break; } }
void ReleaseDesignerOutlets() { if (DatePickerBar != null) { DatePickerBar.Dispose(); DatePickerBar = null; } if (DatePickerVacation != null) { DatePickerVacation.Dispose(); DatePickerVacation = null; } if (DateToolBar != null) { DateToolBar.Dispose(); DateToolBar = null; } if (DoneButton != null) { DoneButton.Dispose(); DoneButton = null; } if (EndDay != null) { EndDay.Dispose(); EndDay = null; } if (EndMonth != null) { EndMonth.Dispose(); EndMonth = null; } if (EndYear != null) { EndYear.Dispose(); EndYear = null; } if (Page != null) { Page.Dispose(); Page = null; } if (PageImage != null) { PageImage.Dispose(); PageImage = null; } if (StartDay != null) { StartDay.Dispose(); StartDay = null; } if (StartMonth != null) { StartMonth.Dispose(); StartMonth = null; } if (StartYear != null) { StartYear.Dispose(); StartYear = null; } if (StatusSegment != null) { StatusSegment.Dispose(); StatusSegment = null; } if (TypeImageView != null) { TypeImageView.Dispose(); TypeImageView = null; } if (TypeText != null) { TypeText.Dispose(); TypeText = null; } if (DatePickerToolbar != null) { DatePickerToolbar.Dispose(); DatePickerToolbar = null; } }
// render all the objects in a page (or any composite object private void ProcessPage(Graphics g, IEnumerable p, RectangleF clipRect, bool bHitList) { // TODO: (Peter) Support can grow and can shrink foreach (PageItem pi in p) { //if (pi is PageTextHtml) //{ // PageTextHtml is actually a composite object (just like a page) // if (SelectTool && bHitList) // { // RectangleF hr = new RectangleF(PixelsX(pi.X + _left - _hScroll), PixelsY(pi.Y + _top - _vScroll), // PixelsX(pi.W), PixelsY(pi.H)); // _HitList.Add(new HitListEntry(hr, pi)); // } // ProcessHtml(pi as PageTextHtml, g, clipRect, bHitList); // continue; //} if (pi is PageLine) { PageLine pl = pi as PageLine; DrawLine(pl.SI.BColorLeft, pl.SI.BStyleLeft, pl.SI.BWidthLeft, g, PixelsX(pl.X + _left - _hScroll), PixelsY(pl.Y + _top - _vScroll), PixelsX(pl.X2 + _left - _hScroll), PixelsY(pl.Y2 + _top - _vScroll)); continue; } RectangleF rect = new RectangleF(PixelsX(pi.X + _left - _hScroll), PixelsY(pi.Y + _top - _vScroll), PixelsX(pi.W), PixelsY(pi.H)); // Maintain the hit list //if (bHitList) //{ // if (SelectTool) // { // we need all PageText and PageImage items that have been displayed // if (pi is PageText || pi is PageImage) // { // _HitList.Add(new HitListEntry(rect, pi)); // } // } // // Only care about items with links and tips // else if (pi.HyperLink != null || pi.BookmarkLink != null || pi.Tooltip != null) // { // HitListEntry hle; // if (pi is PagePolygon) // hle = new HitListEntry(pi as PagePolygon, _left - _hScroll, _top - _vScroll, this); // else // hle = new HitListEntry(rect, pi); // _HitList.Add(hle); // } //} if ((pi is PagePolygon) || (pi is PageCurve)) { // intentionally empty; polygon's rectangles aren't calculated } else if (!rect.IntersectsWith(clipRect)) { continue; } if (pi.SI.BackgroundImage != null) { // put out any background image PageImage i = pi.SI.BackgroundImage; DrawImageBackground(i, pi.SI, g, rect); } if (pi is PageText) { // TODO: enable can shrink, can grow // 2005 spec file, page 9, in the text box has // CanGrow and CanShrink PageText pt = pi as PageText; DrawString(pt, g, rect); } else if (pi is PageImage) { PageImage i = pi as PageImage; DrawImage(i, g, rect); } else if (pi is PageRectangle) { this.DrawBackground(g, rect, pi.SI); } else if (pi is PageEllipse) { PageEllipse pe = pi as PageEllipse; DrawEllipse(pe, g, rect); } else if (pi is PagePie) { PagePie pp = pi as PagePie; DrawPie(pp, g, rect); } else if (pi is PagePolygon) { PagePolygon ppo = pi as PagePolygon; FillPolygon(ppo, g, rect); } else if (pi is PageCurve) { PageCurve pc = pi as PageCurve; DrawCurve(pc.SI.BColorLeft, pc.SI.BStyleLeft, pc.SI.BWidthLeft, g, pc.Points, pc.Offset, pc.Tension); } DrawBorder(pi, g, rect); } }
private static void DrawImageSized(PageImage pi, Image im, Graphics g, RectangleF r, CanvasProperties cp, PageItemManager cpim) { float height, width; // some work variables StyleInfo si = pi.SI; // adjust drawing rectangle based on padding //RectangleF r2 = new RectangleF(r.Left + PixelsX(si.PaddingLeft), // r.Top + PixelsY(si.PaddingTop), // r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), // r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); // http://www.fyireporting.com/forum/viewtopic.php?t=892 //A.S.> convert pt to px if needed(when printing we need px, when draw preview - pt) RectangleF r2; if (g.PageUnit == GraphicsUnit.Pixel) { r2 = new RectangleF(r.Left + (si.PaddingLeft * g.DpiX) / 72, r.Top + (si.PaddingTop * g.DpiX) / 72, r.Width - ((si.PaddingLeft + si.PaddingRight) * g.DpiX) / 72, r.Height - ((si.PaddingTop + si.PaddingBottom) * g.DpiX) / 72); } else { // adjust drawing rectangle based on padding r2 = new RectangleF(r.Left + si.PaddingLeft, r.Top + si.PaddingTop, r.Width - si.PaddingLeft - si.PaddingRight, r.Height - si.PaddingTop - si.PaddingBottom); } Rectangle ir; // int work rectangle ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height)); switch (pi.Sizing) { case ImageSizingEnum.AutoSize: // Note: GDI+ will stretch an image when you only provide // the left/top coordinates. This seems pretty stupid since // it results in the image being out of focus even though // you don't want the image resized. if (g.DpiX == im.HorizontalResolution && g.DpiY == im.VerticalResolution) { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), im.Width, im.Height); } g.DrawImage(im, ir); break; case ImageSizingEnum.Clip: Region saveRegion = g.Clip; Region clipRegion = new Region(g.Clip.GetRegionData()); clipRegion.Intersect(r2); g.Clip = clipRegion; if (g.DpiX == im.HorizontalResolution && g.DpiY == im.VerticalResolution) { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), im.Width, im.Height); } g.DrawImage(im, ir); g.Clip = saveRegion; break; case ImageSizingEnum.FitProportional: float ratioIm = (float)im.Height / (float)im.Width; float ratioR = r2.Height / r2.Width; height = r2.Height; width = r2.Width; if (ratioIm > ratioR) { // this means the rectangle width must be corrected width = height * (1 / ratioIm); } else if (ratioIm < ratioR) { // this means the ractangle height must be corrected height = width * ratioIm; } r2 = new RectangleF(r2.X, r2.Y, width, height); g.DrawImage(im, r2); break; case ImageSizingEnum.Fit: default: g.DrawImage(im, r2); break; } if (cpim.SelectToolEnabled && pi.AllowSelect && cpim.SelectedItemList.Contains(pi)) { g.FillRectangle(new SolidBrush(Color.FromArgb(50, cp.SelectItemColor)), ir); } return; }
public ActionResult GetDocumentPageImage(GetDocumentPageImageParameters parameters) { try { //parameters.IgnoreDocumentAbsence - not supported //parameters.InstanceIdToken - not supported string guid = parameters.Path; int pageIndex = parameters.PageIndex; int pageNumber = pageIndex + 1; /* * //NOTE: This feature is supported starting from version 3.2.0 * CultureInfo cultureInfo = string.IsNullOrEmpty(parameters.Locale) * ? new CultureInfo("en-Us") * : new CultureInfo(parameters.Locale); * * ViewerImageHandler viewerImageHandler = new ViewerImageHandler(viewerConfig, cultureInfo); */ var imageOptions = new ImageOptions { ConvertImageFileType = _convertImageFileType, Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor, parameters.WatermarkPosition, parameters.WatermarkWidth, parameters.WatermarkOpacity), Transformations = parameters.Rotate ? Transformation.Rotate : Transformation.None, CountPagesToRender = 1, PageNumber = pageNumber, JpegQuality = parameters.Quality.GetValueOrDefault(), PageNumbersToRender = new List <int>(new int[] { pageNumber }) }; if (parameters.Rotate && parameters.Width.HasValue) { DocumentInfoContainer documentInfoContainer = _imageHandler.GetDocumentInfo(guid); int side = parameters.Width.Value; int pageAngle = documentInfoContainer.Pages[pageIndex].Angle; if (pageAngle == 90 || pageAngle == 270) { imageOptions.Height = side; } else { imageOptions.Width = side; } } /* * //NOTE: This feature is supported starting from version 3.2.0 * if (parameters.Quality.HasValue) * imageOptions.JpegQuality = parameters.Quality.Value; */ using (new InterProcessLock(guid)) { List <PageImage> pageImages = _imageHandler.GetPages(guid, imageOptions); PageImage pageImage = pageImages.Single(_ => _.PageNumber == pageNumber); return(File(pageImage.Stream, GetContentType(_convertImageFileType))); } } catch (Exception e) { return(this.JsonOrJsonP(new FailedResponse { Reason = e.Message }, null)); } }
/// <summary> /// Renders all the objects in a Page or composite object. /// </summary> private static void ProcessPage(Graphics g, IEnumerable p, RectangleF clipRect, bool bHitList, CanvasProperties cp, ref PageItemManager cpim) { foreach (PageItem pi in p) { if (pi is PageTextHtml) { // PageTextHtml is actually a composite object (just like a page) if (cpim.SelectToolEnabled && bHitList) { RectangleF hr = new RectangleF( Measurement.PixelsFromPoints(pi.X + cp.Left - cp.Scroll.X, cp.Dpi.X), Measurement.PixelsFromPoints(pi.Y + cp.Scroll.X - cp.Scroll.Y, cp.Dpi.Y), Measurement.PixelsFromPoints(pi.W, cp.Dpi.X), Measurement.PixelsFromPoints(pi.H, cp.Dpi.Y)); cpim.HitList.Add(new HitListEntry(hr, pi)); } ProcessHtml(pi as PageTextHtml, g, clipRect, bHitList, cp, ref cpim); continue; } if (pi is PageLine) { PageLine pl = pi as PageLine; CanvasPainter.DrawLine( pl.SI.BColorLeft, pl.SI.BStyleLeft, pl.SI.BWidthLeft, g, Measurement.PixelsFromPoints(pl.X + cp.Left - cp.Scroll.X, cp.Dpi.X), Measurement.PixelsFromPoints(pl.Y + cp.Top - cp.Scroll.Y, cp.Dpi.Y), Measurement.PixelsFromPoints(pl.X2 + cp.Left - cp.Scroll.X, cp.Dpi.X), Measurement.PixelsFromPoints(pl.Y2 + cp.Top - cp.Scroll.Y, cp.Dpi.Y)); continue; } RectangleF rect = new RectangleF( Measurement.PixelsFromPoints(pi.X + cp.Left - cp.Scroll.X, cp.Dpi.X), Measurement.PixelsFromPoints(pi.Y + cp.Top - cp.Scroll.Y, cp.Dpi.Y), Measurement.PixelsFromPoints(pi.W, cp.Dpi.X), Measurement.PixelsFromPoints(pi.H, cp.Dpi.Y)); // Maintain the hit list if (bHitList) { if (cpim.SelectToolEnabled) { // we need all PageText and PageImage items that have been displayed if (pi is PageText || pi is PageImage) { cpim.HitList.Add(new HitListEntry(rect, pi)); } } // Only care about items with links and tips else if (pi.HyperLink != null || pi.BookmarkLink != null || pi.Tooltip != null) { HitListEntry hle; if (pi is PagePolygon) { hle = new HitListEntry(pi as PagePolygon, cp.Left - cp.Scroll.X, cp.Top - cp.Scroll.Y, ((Canvas)cp.Parent)); } else { hle = new HitListEntry(rect, pi); } cpim.HitList.Add(hle); } } if ((pi is PagePolygon) || (pi is PageCurve)) { // intentionally empty; polygon's rectangles aren't calculated } else if (!rect.IntersectsWith(clipRect)) { continue; } if (pi.SI.BackgroundImage != null) { // put out any background image PageImage i = pi.SI.BackgroundImage; CanvasPainter.DrawImageBackground(i, pi.SI, g, rect); } if (pi is PageText) { PageText pt = pi as PageText; CanvasPainter.DrawString(pt, g, rect, cp, cpim); } else if (pi is PageImage) { PageImage i = pi as PageImage; CanvasPainter.DrawImage(i, g, rect, cp, cpim); } else if (pi is PageRectangle) { CanvasPainter.DrawBackground(g, rect, pi.SI); } else if (pi is PageEllipse) { PageEllipse pe = pi as PageEllipse; CanvasPainter.DrawEllipse(pe, g, rect); } else if (pi is PagePie) { PagePie pp = pi as PagePie; CanvasPainter.DrawPie(pp, g, rect); } else if (pi is PagePolygon) { PagePolygon ppo = pi as PagePolygon; CanvasPainter.FillPolygon(ppo, g, rect, cp); } else if (pi is PageCurve) { PageCurve pc = pi as PageCurve; CanvasPainter.DrawCurve(pc.SI.BColorLeft, pc.SI.BStyleLeft, pc.SI.BWidthLeft, g, pc.Points, pc.Offset, pc.Tension, cp); } CanvasPainter.DrawBorder(pi, g, rect); } }
// render all the objects in a page (or any composite object private void ProcessPage(Graphics g, IEnumerable p, RectangleF clipRect, bool bHitList) { foreach (PageItem pi in p) { if (pi is PageTextHtml) { // PageTextHtml is actually a composite object (just like a page) ProcessHtml(pi as PageTextHtml, g, clipRect, bHitList); continue; } if (pi is PageLine) { PageLine pl = pi as PageLine; DrawLine(pl.SI.BColorLeft, pl.SI.BStyleLeft, pl.SI.BWidthLeft, g, PixelsX(pl.X + _left - _hScroll), PixelsY(pl.Y + _top - _vScroll), PixelsX(pl.X2 + _left - _hScroll), PixelsY(pl.Y2 + _top - _vScroll)); continue; } RectangleF rect = new RectangleF(PixelsX(pi.X + _left - _hScroll), PixelsY(pi.Y + _top - _vScroll), PixelsX(pi.W), PixelsY(pi.H)); // Maintain the hit list if (bHitList) { // Only care about items with links and tips if (pi.HyperLink != null || pi.BookmarkLink != null || pi.Tooltip != null) { _HitList.Add(new HitListEntry(rect, pi)); } } if (!rect.IntersectsWith(clipRect)) { continue; } if (pi.SI.BackgroundImage != null) { // put out any background image PageImage i = pi.SI.BackgroundImage; DrawImage(i, g, rect); } if (pi is PageText) { PageText pt = pi as PageText; DrawString(pt, g, rect); } else if (pi is PageImage) { PageImage i = pi as PageImage; DrawImage(i, g, rect); } else if (pi is PageRectangle) { this.DrawBackground(g, rect, pi.SI); } DrawBorder(pi, g, rect); } }
private void DrawImageSized(PageImage pi, Image im, Graphics g, RectangleF r) { float height, width; // some work variables StyleInfo si = pi.SI; // adjust drawing rectangle based on padding RectangleF r2 = new RectangleF(r.Left + PixelsX(si.PaddingLeft), r.Top + PixelsY(si.PaddingTop), r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); Rectangle ir; // int work rectangle switch (pi.Sizing) { case ImageSizingEnum.AutoSize: // Note: GDI+ will stretch an image when you only provide // the left/top coordinates. This seems pretty stupid since // it results in the image being out of focus even though // you don't want the image resized. if (g.DpiX == im.HorizontalResolution && g.DpiY == im.VerticalResolution) { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), im.Width, im.Height); } else { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height)); } g.DrawImage(im, ir); break; case ImageSizingEnum.Clip: Region saveRegion = g.Clip; Region clipRegion = new Region(g.Clip.GetRegionData()); clipRegion.Intersect(r2); g.Clip = clipRegion; if (g.DpiX == im.HorizontalResolution && g.DpiY == im.VerticalResolution) { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), im.Width, im.Height); } else { ir = new Rectangle(Convert.ToInt32(r2.Left), Convert.ToInt32(r2.Top), Convert.ToInt32(r2.Width), Convert.ToInt32(r2.Height)); } g.DrawImage(im, ir); g.Clip = saveRegion; break; case ImageSizingEnum.FitProportional: float ratioIm = (float)im.Height / (float)im.Width; float ratioR = r2.Height / r2.Width; height = r2.Height; width = r2.Width; if (ratioIm > ratioR) { // this means the rectangle width must be corrected width = height * (1 / ratioIm); } else if (ratioIm < ratioR) { // this means the ractangle height must be corrected height = width * ratioIm; } r2 = new RectangleF(r2.X, r2.Y, width, height); g.DrawImage(im, r2); break; case ImageSizingEnum.Fit: default: g.DrawImage(im, r2); break; } return; }
public HttpResponseMessage Get(int?width, string file, int page, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, int?rotate, int?zoom, int?height = null) { if (Utils.IsValidUrl(file)) { file = Utils.DownloadToStorage(file); } ViewerImageHandler handler = Utils.CreateViewerImageHandler(); if (rotate.HasValue) { if (rotate.Value > 0) { handler.ClearCache(file); } } ImageOptions options = new ImageOptions(); options.PageNumbersToRender = new List <int>(new int[] { page }); options.PageNumber = page; options.CountPagesToRender = 1; if (Path.GetExtension(file).ToLower().StartsWith(".xls")) { options.CellsOptions.OnePagePerSheet = false; options.CellsOptions.CountRowsPerPage = 150; } if (watermarkText != "") { options.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } if (width.HasValue) { int w = Convert.ToInt32(width); if (zoom.HasValue) { w = w + zoom.Value; } options.Width = w; } if (height.HasValue) { if (zoom.HasValue) { options.Height = options.Height + zoom.Value; } } if (rotate.HasValue) { if (rotate.Value > 0) { if (width.HasValue) { int side = options.Width; DocumentInfoContainer documentInfoContainer = handler.GetDocumentInfo(file); int pageAngle = documentInfoContainer.Pages[page - 1].Angle; if (pageAngle == 90 || pageAngle == 270) { options.Height = side; } else { options.Width = side; } } options.Transformations = Transformation.Rotate; handler.RotatePage(file, new RotatePageOptions(page, rotate.Value)); } } else { options.Transformations = Transformation.None; handler.RotatePage(file, new RotatePageOptions(page, 0)); } List <PageImage> list = handler.GetPages(file, options); PageImage pageImage = list.Single(_ => _.PageNumber == page); Stream stream = pageImage.Stream; var result = new HttpResponseMessage(HttpStatusCode.OK); Image image = Image.FromStream(stream); MemoryStream memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); result.Content = new ByteArrayContent(memoryStream.ToArray()); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return(result); }
protected void Page_Load(object sender, EventArgs e) { _config = new ViewerConfig { StoragePath = _storagePath, UseCache = true }; ViewerImageHandler imageHandler = (ViewerImageHandler)HttpContext.Current.Session["imageHandler"]; ViewerHtmlHandler htmlHandler = (ViewerHtmlHandler)HttpContext.Current.Session["htmlHandler"]; GetDocumentPageImageParameters parameters = new GetDocumentPageImageParameters(); foreach (String key in Request.QueryString.AllKeys) { if (!string.IsNullOrEmpty(Request.QueryString[key])) { var propertyInfo = parameters.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); propertyInfo.SetValue(parameters, ChangeType(Request.QueryString[key], propertyInfo.PropertyType), null); } } string guid = parameters.Path; int pageIndex = parameters.PageIndex; int pageNumber = pageIndex + 1; var displayName = parameters.Path; /* * //NOTE: This feature is supported starting from version 3.2.0 * CultureInfo cultureInfo = string.IsNullOrEmpty(parameters.Locale) * ? new CultureInfo("en-Us") * : new CultureInfo(parameters.Locale); * * ViewerImageHandler viewerImageHandler = new ViewerImageHandler(viewerConfig, cultureInfo); */ var imageOptions = new ImageOptions { //ConvertImageFileType = _convertImageFileType, ConvertImageFileType = ConvertImageFileType.JPG, JpegQuality = 100, Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor, parameters.WatermarkPosition, parameters.WatermarkWidth), Transformations = parameters.Rotate ? Transformation.Rotate : Transformation.None }; if (parameters.Rotate && parameters.Width.HasValue) { DocumentInfoContainer documentInfoContainer = imageHandler.GetDocumentInfo(guid); int side = parameters.Width.Value; int pageAngle = documentInfoContainer.Pages[pageIndex].Angle; if (pageAngle == 90 || pageAngle == 270) { imageOptions.Height = side; } else { imageOptions.Width = side; } } /* * //NOTE: This feature is supported starting from version 3.2.0 * if (parameters.Quality.HasValue) * imageOptions.JpegQuality = parameters.Quality.Value; */ using (new InterProcessLock(guid)) { List <PageImage> pageImages = imageHandler.GetPages(guid, imageOptions); PageImage pageImage = pageImages.Single(_ => _.PageNumber == pageNumber); var fileStream = pageImage.Stream; // return File(pageImage.Stream, GetContentType(_convertImageFileType)); byte[] Bytes = new byte[fileStream.Length]; fileStream.Read(Bytes, 0, Bytes.Length); string contentDispositionString = "attachment; filename=\"" + displayName + "\""; contentDispositionString = new ContentDisposition { FileName = displayName, Inline = true }.ToString(); HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.AddHeader("Content-Disposition", contentDispositionString); HttpContext.Current.Response.AddHeader("Content-Length", fileStream.Length.ToString()); try { HttpContext.Current.Response.OutputStream.Write(Bytes, 0, Bytes.Length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } catch (HttpException x) { // Ignore it. } } }
// render all the objects in a page (or any composite object private void ProcessPage(System.Drawing.Graphics g, IEnumerable p) { // TODO: (Peter) Support can grow and can shrink foreach (PageItem pi in p) { if (pi is PageTextHtml) { // PageTextHtml is actually a composite object (just like a page) ProcessHtml(pi as PageTextHtml, g); continue; } if (pi is PageLine) { PageLine pl = pi as PageLine; DrawLine(pl.SI.BColorLeft, pl.SI.BStyleLeft, pl.SI.BWidthLeft, g, PixelsX(pl.X + _left - _hScroll), PixelsY(pl.Y + _top - _vScroll), PixelsX(pl.X2 + _left - _hScroll), PixelsY(pl.Y2 + _top - _vScroll)); continue; } RectangleF rect = new RectangleF(PixelsX(pi.X + _left - _hScroll), PixelsY(pi.Y + _top - _vScroll), PixelsX(pi.W), PixelsY(pi.H)); if ((pi is PagePolygon) || (pi is PageCurve)) { // intentionally empty; polygon's rectangles aren't calculated } if (pi.SI.BackgroundImage != null) { // put out any background image PageImage i = pi.SI.BackgroundImage; DrawImageBackground(i, pi.SI, g, rect); } if (pi is PageText) { // TODO: enable can shrink, can grow // 2005 spec file, page 9, in the text box has // CanGrow and CanShrink PageText pt = pi as PageText; DrawString(pt, g, rect); } else if (pi is PageImage) { PageImage i = pi as PageImage; DrawImage(i, g, rect); } else if (pi is PageRectangle) { this.DrawBackground(g, rect, pi.SI); } else if (pi is PageEllipse) { PageEllipse pe = pi as PageEllipse; DrawEllipse(pe, g, rect); } else if (pi is PagePie) { PagePie pp = pi as PagePie; DrawPie(pp, g, rect); } else if (pi is PagePolygon) { PagePolygon ppo = pi as PagePolygon; FillPolygon(ppo, g, rect); } else if (pi is PageCurve) { PageCurve pc = pi as PageCurve; DrawCurve(pc.SI.BColorLeft, pc.SI.BStyleLeft, pc.SI.BWidthLeft, g, pc.Points, pc.Offset, pc.Tension); } DrawBorder(pi, g, rect); } // TO: convert System.Drawing.Graphics to Xwt.Drawing.Context and draw it to this.g Bitmap bm = new Bitmap(gImg.Width, gImg.Height); g.DrawImage(bm, gImg.Width, gImg.Height); // Xwt.Drawing.Image.FromStream does not work. It crashes with both wpf and gtk // As a work around save the image to a temporary file and load it into xwt using the // FromFile method. System.IO.MemoryStream s = new System.IO.MemoryStream(); gImg.Save(s, System.Drawing.Imaging.ImageFormat.Png); string fileName = System.IO.Path.GetTempFileName(); gImg.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); // Xwt.Drawing.Image img = Xwt.Drawing.Image.FromStream(s); Xwt.Drawing.Image img = Xwt.Drawing.Image.FromFile(fileName); xwtContext.DrawImage(img, new Xwt.Rectangle(0, 0, gImg.Width, gImg.Height), new Xwt.Rectangle(0, 0, gImg.Width, gImg.Height)); img.Dispose(); System.IO.File.Delete(fileName); }
private void ProcessPage(Cairo.Context g, IEnumerable p) { foreach (PageItem pi in p) { if (pi is PageTextHtml) { // PageTextHtml is actually a composite object (just like a page) ProcessHtml(pi as PageTextHtml, g); continue; } if (pi is PageLine) { PageLine pl = pi as PageLine; DrawLine( pl.SI.BColorLeft.ToCairoColor(), pl.SI.BStyleLeft, pl.SI.BWidthLeft, g, PixelsX(pl.X), PixelsY(pl.Y), PixelsX(pl.X2), PixelsY(pl.Y2) ); continue; } // RectangleF rect = new RectangleF(PixelsX(pi.X), PixelsY(pi.Y), PixelsX(pi.W), PixelsY(pi.H)); Cairo.Rectangle rect = new Cairo.Rectangle(PixelsX(pi.X), PixelsY(pi.Y), PixelsX(pi.W), PixelsY(pi.H)); if (pi.SI.BackgroundImage != null) { // put out any background image PageImage i = pi.SI.BackgroundImage; DrawImage(i, g, rect); continue; } if (pi is PageText) { PageText pt = pi as PageText; DrawString(pt, g, rect); } if (pi is PageImage) { PageImage i = pi as PageImage; DrawImage(i, g, rect); } if (pi is PageRectangle) { //DrawBackground(g, rect, pi.SI); } // else if (pi is PageEllipse) // { // PageEllipse pe = pi as PageEllipse; // DrawEllipse(pe, g, rect); // } // else if (pi is PagePie) // { // PagePie pp = pi as PagePie; // DrawPie(pp, g, rect); // } // else if (pi is PagePolygon) // { // PagePolygon ppo = pi as PagePolygon; // FillPolygon(ppo, g, rect); // } // else if (pi is PageCurve) // { // PageCurve pc = pi as PageCurve; // DrawCurve(pc.SI.BColorLeft, pc.SI.BStyleLeft, pc.SI.BWidthLeft, // g, pc.Points, pc.Offset, pc.Tension); // } // DrawBorder(pi, g, rect); } }
private static void DrawImageBackground(PageImage pi, StyleInfo si, Graphics g, RectangleF r) { Stream strm = null; System.Drawing.Image im = null; try { strm = new MemoryStream(pi.ImageData); im = System.Drawing.Image.FromStream(strm); //RectangleF r2 = new RectangleF(r.Left + PixelsX(si.PaddingLeft), // r.Top + PixelsY(si.PaddingTop), // r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), // r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); // http://www.fyireporting.com/forum/viewtopic.php?t=892 //A.S.> convert pt to px if needed(when printing we need px, when draw preview - pt) RectangleF r2; if (g.PageUnit == GraphicsUnit.Pixel) { r2 = new RectangleF(r.Left + (si.PaddingLeft * g.DpiX) / 72, r.Top + (si.PaddingTop * g.DpiX) / 72, r.Width - ((si.PaddingLeft + si.PaddingRight) * g.DpiX) / 72, r.Height - ((si.PaddingTop + si.PaddingBottom) * g.DpiX) / 72); } else { // adjust drawing rectangle based on padding r2 = new RectangleF(r.Left + si.PaddingLeft, r.Top + si.PaddingTop, r.Width - si.PaddingLeft - si.PaddingRight, r.Height - si.PaddingTop - si.PaddingBottom); } int repeatX = 0; int repeatY = 0; switch (pi.Repeat) { case ImageRepeat.Repeat: repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); break; case ImageRepeat.RepeatX: repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); repeatY = 1; break; case ImageRepeat.RepeatY: repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); repeatX = 1; break; case ImageRepeat.NoRepeat: default: repeatX = repeatY = 1; break; } //make sure the image is drawn at least 1 times repeatX = Math.Max(repeatX, 1); repeatY = Math.Max(repeatY, 1); float startX = r2.Left; float startY = r2.Top; Region saveRegion = g.Clip; Region clipRegion = new Region(g.Clip.GetRegionData()); clipRegion.Intersect(r2); g.Clip = clipRegion; for (int i = 0; i < repeatX; i++) { for (int j = 0; j < repeatY; j++) { float currX = startX + i * pi.SamplesW; float currY = startY + j * pi.SamplesH; g.DrawImage(im, new RectangleF(currX, currY, pi.SamplesW, pi.SamplesH)); } } g.Clip = saveRegion; } finally { if (strm != null) { strm.Close(); } if (im != null) { im.Dispose(); } } }
private void SaveAsPdf(Report report, OneFileStreamGen sg) { Pages pgs = report.BuildPages(); FileStream strm = null; System.Drawing.Image im = null; // Handle any parameters float x = 0; // x position of image float y = 0; // y position of image float h = 0; // height of image float w = 0; // width position of image string fname = null; int index = _StampInfo.LastIndexOf('?'); bool bClip = false; // we force clip if either height or width not specified if (index >= 0) { // Get all the arguments for sizing the image ListDictionary ld = this.GetParameters(_StampInfo.Substring(index + 1)); fname = _StampInfo.Substring(0, index); string ws = (string)ld["x"]; x = Size(ws); ws = (string)ld["y"]; y = Size(ws); ws = (string)ld["h"]; if (ws == null) { bClip = true; ws = "12in"; // just give it a big value } h = Size(ws); ws = (string)ld["w"]; if (ws == null) { bClip = true; ws = "12in"; // just give it a big value } w = Size(ws); } else { fname = _StampInfo; // force size bClip = true; h = Size("12in"); w = Size("12in"); } // Stamp the first page foreach (Page p in pgs) // we loop then break after obtaining one { try { strm = new FileStream(fname, System.IO.FileMode.Open, FileAccess.Read); im = System.Drawing.Image.FromStream(strm); int height = im.Height; int width = im.Width; MemoryStream ostrm = new MemoryStream(); /* Replaced with high quality JPEG encoder * 06122007AJM */ ImageFormat imf = ImageFormat.Jpeg; //im.Save(ostrm, imf); System.Drawing.Imaging.ImageCodecInfo[] info; info = ImageCodecInfo.GetImageEncoders(); EncoderParameters encoderParameters; encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); System.Drawing.Imaging.ImageCodecInfo codec = null; for (int i = 0; i < info.Length; i++) { if (info[i].FormatDescription == "JPEG") { codec = info[i]; break; } } im.Save(ostrm, codec, encoderParameters); // end change byte[] ba = ostrm.ToArray(); ostrm.Close(); PageImage pi = new PageImage(imf, ba, width, height); pi.SI = new StyleInfo(); // defaults are ok; don't want border, etc // Set location, height and width pi.X = x; pi.Y = y; pi.H = h; pi.W = w; pi.Sizing = bClip? ImageSizingEnum.Clip: ImageSizingEnum.FitProportional; p.InsertObject(pi); } catch (Exception e) { // image failed to load, continue processing Console.WriteLine("Stamping image failed. {0}", e.Message); } finally { if (strm != null) { strm.Close(); } if (im != null) { im.Dispose(); } } break; // only stamp the first page } // Now create the PDF report.RunRenderPdf(sg, pgs); }
private void DrawImageBackground(PageImage pi, StyleInfo si, Graphics g, RectangleF r) { Stream strm = null; System.Drawing.Image im = null; try { strm = new MemoryStream(pi.ImageData); im = System.Drawing.Image.FromStream(strm); RectangleF r2 = new RectangleF(r.Left + PixelsX(si.PaddingLeft), r.Top + PixelsY(si.PaddingTop), r.Width - PixelsX(si.PaddingLeft + si.PaddingRight), r.Height - PixelsY(si.PaddingTop + si.PaddingBottom)); int repeatX = 0; int repeatY = 0; switch (pi.Repeat) { case ImageRepeat.Repeat: repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); break; case ImageRepeat.RepeatX: repeatX = (int)Math.Floor(r2.Width / pi.SamplesW); repeatY = 1; break; case ImageRepeat.RepeatY: repeatY = (int)Math.Floor(r2.Height / pi.SamplesH); repeatX = 1; break; case ImageRepeat.NoRepeat: default: repeatX = repeatY = 1; break; } //make sure the image is drawn at least 1 times repeatX = Math.Max(repeatX, 1); repeatY = Math.Max(repeatY, 1); float startX = r2.Left; float startY = r2.Top; Region saveRegion = g.Clip; Region clipRegion = new Region(g.Clip.GetRegionData()); clipRegion.Intersect(r2); g.Clip = clipRegion; for (int i = 0; i < repeatX; i++) { for (int j = 0; j < repeatY; j++) { float currX = startX + i * pi.SamplesW; float currY = startY + j * pi.SamplesH; g.DrawImage(im, new RectangleF(currX, currY, pi.SamplesW, pi.SamplesH)); } } g.Clip = saveRegion; } finally { if (strm != null) { strm.Close(); } if (im != null) { im.Dispose(); } } }