protected override void OnDraw(Xwt.Drawing.Context ctx, Xwt.Rectangle dirtyRect) { base.OnDraw(ctx, dirtyRect); if (image != null) { ctx.DrawImage(image, 0, 0); if (filler != null) { int fillCount = (int)Math.Ceiling((Bounds.Width - image.Width) / filler.Width); double x = image.Width; while ((fillCount--) > 0) { ctx.DrawImage(filler, x, 0); x += filler.Width; } } } }
// 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(600, 600); g.DrawImage(bm, 600, 600); System.IO.MemoryStream s = new System.IO.MemoryStream(); gImg.Save(s, System.Drawing.Imaging.ImageFormat.Png); gImg.Save("test.png", System.Drawing.Imaging.ImageFormat.Png); // Xwt.Drawing.Image img = Xwt.Drawing.Image.FromStream(s); Xwt.Drawing.Image img = Xwt.Drawing.Image.FromFile("test.png"); xwtContext.DrawImage(img, new Xwt.Rectangle(0, 0, 600, 600), new Xwt.Rectangle(0, 0, 600, 600)); //Xwt.Drawing.TextLayout layout = new Xwt.Drawing.TextLayout(xwtContext); //layout.Font = xwtContext.Font; //layout.Text = "Test"; //xwtContext.DrawTextLayout(layout, 2, 4); }
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; } }
// 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); }