static void Main() { // The using statement ensures that potentially heavy objects // are disposed immediately. using (ImageSurface draw = new ImageSurface(Format.Argb32, 70, 150)) { using (Context gr = new Context(draw)) { gr.Antialias = Antialias.Subpixel; // sets the anti-aliasing method gr.LineWidth = 9; // sets the line width gr.SetSourceColor(new Color(0, 0, 0, 1)); // red, green, blue, alpha gr.MoveTo(10, 10); // sets the Context's start point. gr.LineTo(40, 60); // draws a "virtual" line from 5,5 to 20,30 gr.Stroke(); //stroke the line to the image surface gr.Antialias = Antialias.Gray; gr.LineWidth = 8; gr.SetSourceColor(new Color(1, 0, 0, 1)); gr.LineCap = LineCap.Round; gr.MoveTo(10, 50); gr.LineTo(40, 100); gr.Stroke(); gr.Antialias = Antialias.None; //fastest method but low quality gr.LineWidth = 7; gr.MoveTo(10, 90); gr.LineTo(40, 140); gr.Stroke(); draw.WriteToPng("antialias.png"); //save the image as a png image. } } }
public static Gtk.Image create_empty_image(int width, int height) { using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height)) { surface.WriteToPng(tmp_image_path); Gtk.Image img = new Gtk.Image(tmp_image_path); System.IO.File.Delete(tmp_image_path); return img; } }
static void Main () { Surface s = new ImageSurface (Format.ARGB32, 500, 500); Cairo.Context g = new Cairo.Context (s); draw (g, 500, 500); s.WriteToPng ("clip.png"); }
public static Gtk.Image create_dot_image(int r, int g, int b, int a, int width, int height) { using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height)) { using (Cairo.Context context = new Context(surface)) { draw(context, width, height, new Cairo.Color(((double)r)/255, ((double)g)/255, ((double)b)/255, ((double)a)/255)); surface.WriteToPng(tmp_image_path); Gtk.Image img = new Gtk.Image(tmp_image_path); System.IO.File.Delete(tmp_image_path); return img; } } }
public void CreatePng () { const int Width = 480; const int Height = 160; const int CheckSize = 10; const int Spacing = 2; // Create an Image-based surface with data stored in ARGB32 format and a context, // "using" is used here to ensure that they are Disposed once we are done using (ImageSurface surface = new ImageSurface (Format.ARGB32, Width, Height)) using (Context cr = new Cairo.Context (surface)) { // Start drawing a checkerboard int i, j, xcount, ycount; xcount = 0; i = Spacing; while (i < Width) { j = Spacing; ycount = xcount % 2; // start with even/odd depending on row while (j < Height) { if (ycount % 2 != 0) cr.SetSourceRGB (1, 0, 0); else cr.SetSourceRGB (1, 1, 1); // If we're outside the clip, this will do nothing. cr.Rectangle (i, j, CheckSize, CheckSize); cr.Fill (); j += CheckSize + Spacing; ++ycount; } i += CheckSize + Spacing; ++xcount; } // Select a font to draw with cr.SelectFontFace ("serif", FontSlant.Normal, FontWeight.Bold); cr.SetFontSize (64.0); // Select a color (blue) cr.SetSourceRGB (0, 0, 1); // Draw cr.MoveTo (20, 100); cr.ShowText ("Hello, World"); surface.WriteToPng ("test.png"); } }
public static void Main(string[] args) { // call the snippets Snippets snip = new Snippets(); foreach (string snippet in Snippets.snippets) { string filename = "./" + snippet + ".png"; Surface surface = new ImageSurface(Format.ARGB32, IMAGE_WIDTH, IMAGE_WIDTH); Context cr = new Context(surface); cr.Save(); Snippets.InvokeSnippet(snip, snippet, cr, IMAGE_WIDTH, IMAGE_HEIGHT); surface.WriteToPng(filename); cr.Restore(); } }
internal static void DrawNode(Node node, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.Argb32, (int)node.Rect.Width, (int)node.Rect.Height)) using (Cairo.Context context = new Cairo.Context(surface)) { Draw(context, node); if (!System.IO.Directory.Exists(OutputPath)) { System.IO.Directory.CreateDirectory(OutputPath); } string filePath = OutputPath + "\\" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss-fff_") + surface.GetHashCode() + memberName + ".png"; surface.WriteToPng(filePath); Util.OpenImage(filePath); } }
public static Gtk.Image create_big_starred_image(string pic_path) { using (Cairo.ImageSurface img_surface = new ImageSurface(pic_path)) { using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2)) { using (Cairo.Context context = new Context(surface)) { Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path); if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available... context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); context.Fill(); context.Stroke(); } context.SetSource(img_surface, 1, 1); context.Paint(); // evil hack because the interface to cairo is pretty bad Assembly asm = Assembly.GetCallingAssembly(); Stream s = asm.GetManifestResourceStream("big_star.png"); FileStream fs = new System.IO.FileStream(tmp_image_path, FileMode.Create); for (int i = 0; i < s.Length; ++i) fs.WriteByte((byte)s.ReadByte()); fs.Close(); using (Cairo.ImageSurface star_surface = new ImageSurface(tmp_image_path)) { System.IO.File.Delete(tmp_image_path); context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height); context.Paint(); surface.WriteToPng(tmp_image_path); Gtk.Image img = new Gtk.Image(tmp_image_path); System.IO.File.Delete(tmp_image_path); return img; } } } } }
public void HandleExport(object o, EventArgs ev) { FileChooserDialog chooser = new FileChooserDialog( "Export Handwriting to PNG", null, FileChooserAction.Save); chooser.SetCurrentFolder(Environment.GetFolderPath( Environment.SpecialFolder.Personal)); chooser.AddButton(Gtk.Stock.Cancel, ResponseType.Cancel); chooser.AddButton(Gtk.Stock.Ok, ResponseType.Ok); chooser.DefaultResponse = ResponseType.Ok; if (chooser.Run() == (int)ResponseType.Ok) { string path = (new Uri(chooser.Uri)).AbsolutePath; Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, handwriting.Allocation.Width, handwriting.Allocation.Height); handwriting.DrawToSurface(surface, handwriting.Allocation); Console.WriteLine("PNG saved to {0}", path); surface.WriteToPng(path); } chooser.Destroy(); }
/// <summary> /// Exports the specified <see cref="PlotModel" /> to a png file. /// </summary> /// <param name="model">The model.</param> /// <param name="fileName">Name of the output file.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="background">The background color.</param> public static void Export(IPlotModel model, string fileName, int width, int height, Pattern background = null) { using (var bm = new ImageSurface(Format.ARGB32, width, height)) { using (var g = new Context(bm)) { if (background != null) { g.Save(); g.SetSource(background); g.Rectangle(0, 0, width, height); g.Fill(); g.Restore(); } var rc = new GraphicsRenderContext { RendersToScreen = false }; rc.SetGraphicsTarget(g); model.Update(true); model.Render(rc, width, height); bm.WriteToPng(fileName); } } }
private void Save(string filename,bool bSource,bool bDrawings) { Surface pngSurface = new ImageSurface(Format.ARGB32,sourceWidth,sourceHeight); using(Context c = new Context(pngSurface)) { if(bSource) { c.SetSourceSurface(source,0,0); c.Paint(); } if(bDrawings) { c.SetSourceSurface(drawings,0,0); c.PaintWithAlpha(transparency); } } pngSurface.WriteToPng(filename); }
public static void RotatePng(ParsedPath pngPath, ImageRotation rotation) { if (rotation == ImageRotation.None) return; using (ImageSurface originalImage = new ImageSurface(pngPath)) { int w; int h; if (rotation == ImageRotation.Left || rotation == ImageRotation.Right) { w = originalImage.Height; h = originalImage.Width; } else { w = originalImage.Width; h = originalImage.Height; } double[] rotationRadians = {0, -Math.PI / 2, Math.PI / 2, Math.PI }; using (ImageSurface rotatedImage = new ImageSurface(Format.Argb32, w, h)) { using (Cairo.Context g = new Cairo.Context(rotatedImage)) { g.Translate(rotatedImage.Width / 2.0, rotatedImage.Height / 2.0); g.Rotate(rotationRadians[(int)rotation]); g.Translate(-originalImage.Width / 2.0, -originalImage.Height / 2.0); g.SetSourceSurface(originalImage, 0, 0); g.Paint(); } rotatedImage.WriteToPng(pngPath); } } }
public void ExportToPng(string path) { //ReadGepmetry int fX, fY, fWidth,fHeight,fDepth; GdkWindow.GetGeometry(out fX,out fY,out fWidth,out fHeight,out fDepth); using (ImageSurface draw = new ImageSurface (Format.Argb32, fWidth, fHeight)) { using (Context gr = new Context (draw)) { DrawPrimitives(_pro.Get (), gr); DrawLines(_pro.Lines(), gr); DrawConnectors(_pro.Connectors(), gr); draw.WriteToPng (path); } } }
protected void OnBtnConvertClicked(object sender, EventArgs e) { var pdfRectangles = new Dictionary<string, Dictionary<int, List<Tuple<Exameer.Rectangle, string>>> > (); // Merge rectangles foreach (PdfNode pdf in StorePDF) { foreach (var png in pdf.Images) { foreach (var rectangle in png.Rectangles) { if (pdfRectangles.ContainsKey (pdf.Name)) { if (pdfRectangles [pdf.Name].ContainsKey (rectangle.ID)) { pdfRectangles [pdf.Name] [rectangle.ID].Add (new Tuple<Exameer.Rectangle, string> (rectangle, png.FileName)); } else { pdfRectangles [pdf.Name].Add (rectangle.ID, new List<Tuple<Exameer.Rectangle, string>> () { new Tuple<Exameer.Rectangle, string>(rectangle, png.FileName) } ); } } else { pdfRectangles.Add (pdf.Name, new Dictionary<int, List<Tuple<Exameer.Rectangle, string>>> () {{ rectangle.ID, new List<Tuple<Exameer.Rectangle, string>>() { new Tuple<Exameer.Rectangle, string>(rectangle, png.FileName) }}} ); } } } } int problemNbr = 0; foreach (var pdf in pdfRectangles) { var createdDestinationDir = Directory.CreateDirectory ("./" + pdf.Key.Replace (".pdf", "") + "/"); foreach (var pngrects in pdf.Value) { var surfaces = new List<Tuple<ImageSurface, Exameer.Rectangle>> (); foreach (var rectangle in pngrects.Value) { surfaces.Add (new Tuple<ImageSurface, Exameer.Rectangle> (new ImageSurface (rectangle.Item2), rectangle.Item1)); } var png = new ImageSurface (pngrects.Value [0].Item2); var rectsMaxWidth = pngrects.Value.Max (x => x.Item1.Width); var rectsMaxHeight = pngrects.Value.Select (x => x.Item1.Height).Sum (); var newRectWidth = (int)((rectsMaxWidth / (float)pngrects.Value [0].Item1.Parent.Width) * png.Width); var newRectHeight = (int)((rectsMaxHeight / (float)pngrects.Value [0].Item1.Parent.Height) * png.Height); //pngrects.Value.Aggregate(0, (sum, next) => sum + next.Item1.Height); ImageSurface newImg = new ImageSurface (Format.Argb32, newRectWidth, newRectHeight); Context cr = new Context (newImg); //cr.SetSourceRGBA (1, 1, 1, 1); //cr.Paint (); // Assume they are sorted by their appearance in PNGs. int lastY = 0; foreach (var surface in surfaces) { var dstx = 0; var dsty = lastY; var r = surface.Item2; var srcx = (int)((r.x1 / (float)r.Parent.Width) * png.Width); var srcy = ((int)((r.y1 / (float)r.Parent.Height) * png.Height)); var w = (int)((r.Width / (float)r.Parent.Width) * png.Width); var h = (int)((r.Height / (float)r.Parent.Height) * png.Height); cr.SetSourceSurface (surface.Item1, dstx - srcx, dsty - srcy); cr.Rectangle (dstx, dsty, w, h); cr.Fill (); //lastY = (int)((r.Height / (float)r.Parent.Height) * png.Height); lastY = dsty + h; } newImg.Flush (); newImg.WriteToPng ("./" + pdf.Key.Replace (".pdf", "") + "/" + (problemNbr++) + ".png"); newImg.Dispose (); //newImg.Dispose (); //surfaces.ForEach (x => x.Item1.Destroy ()); surfaces.ForEach (x => x.Item1.Dispose ()); png.Dispose (); //png.Destroy (); } problemNbr = 0; } }
public void HandleExport(object o, EventArgs ev) { FileChooserDialog chooser = new FileChooserDialog( "Export Handwriting to PNG", null, FileChooserAction.Save); chooser.SetCurrentFolder(Environment.GetFolderPath( Environment.SpecialFolder.Personal)); chooser.AddButton(Gtk.Stock.Cancel, ResponseType.Cancel); chooser.AddButton(Gtk.Stock.Ok, ResponseType.Ok); chooser.DefaultResponse = ResponseType.Ok; if(chooser.Run() == (int)ResponseType.Ok) { string path = (new Uri(chooser.Uri)).AbsolutePath; Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, handwriting.Allocation.Width, handwriting.Allocation.Height); handwriting.DrawToSurface(surface, handwriting.Allocation); Console.WriteLine("PNG saved to {0}", path); surface.WriteToPng(path); } chooser.Destroy(); }
private void PrerareReport(bool Client) { if (!Save()) return; string TempImagePath = System.IO.Path.Combine (System.IO.Path.GetTempPath (), String.Format("Cupboard{0}.png", ItemId)); int widght = 2244; int height = 1181; ImageSurface surf = new ImageSurface(Format.ARGB32, widght, height); Cairo.Context cr = new Context(surf); int MinCubeSizeForH = Convert.ToInt32(widght / (OrderCupboard.CubesH + 1.2)); int MinCubeSizeForV = Convert.ToInt32(height / (OrderCupboard.CubesV + 1.2)); int NeedCubePxSize = Math.Min(MinCubeSizeForH, MinCubeSizeForV); OrderCupboard.Draw(cr, widght, height, NeedCubePxSize, true); surf.Flush(); surf.WriteToPng(TempImagePath); logger.Debug("Writed {0}", TempImagePath); string param = "id=" + ItemId.ToString() + "&image=" + TempImagePath + "&basel=" + OrderCupboard.CubesH.ToString() + "&baseh=" + OrderCupboard.CubesV.ToString(); string ReportPath; if (Client) { ReportPath = System.IO.Path.Combine (Directory.GetCurrentDirectory (), "Reports", "order" + ".rdl"); reportviewer1.LoadReport (new Uri (ReportPath), param, QSMain.ConnectionString); } else { ReportPath = System.IO.Path.Combine (Directory.GetCurrentDirectory (), "Reports", "order_factory" + ".rdl"); reportviewer2.LoadReport(new Uri(ReportPath), param, QSMain.ConnectionString); } }
public static Gtk.Image create_small_starred_image(string pic_path) { // evil hack because the interface to cairo is pretty bad Assembly asm = Assembly.GetCallingAssembly(); Stream img_s = asm.GetManifestResourceStream(pic_path); FileStream img_fs = new System.IO.FileStream(tmp_image_path, FileMode.Create); for (int i = 0; i < img_s.Length; ++i) img_fs.WriteByte((byte)img_s.ReadByte()); img_fs.Close(); using (Cairo.ImageSurface img_surface = new ImageSurface(tmp_image_path)) { System.IO.File.Delete(tmp_image_path); using (Cairo.Context context = new Context(img_surface)) { Stream s = asm.GetManifestResourceStream("starred_right.png"); FileStream fs = new System.IO.FileStream(tmp_image_path, FileMode.Create); for (int i = 0; i < s.Length; ++i) fs.WriteByte((byte)s.ReadByte()); fs.Close(); using (Cairo.ImageSurface star_surface = new ImageSurface(tmp_image_path)) { System.IO.File.Delete(tmp_image_path); context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height); context.Paint(); img_surface.WriteToPng(tmp_image_path); Gtk.Image img = new Gtk.Image(tmp_image_path); System.IO.File.Delete(tmp_image_path); return img; } } } }
public static Gtk.Image create_image_with_border(string pic_path) { using (Cairo.ImageSurface img_surface = new ImageSurface(pic_path)) { using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2)) { using (Cairo.Context context = new Context(surface)) { Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path); if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available... context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); context.Fill(); context.Stroke(); } context.SetSource(img_surface, 1, 1); context.Paint(); surface.WriteToPng(tmp_image_path); Gtk.Image img = new Gtk.Image(tmp_image_path); System.IO.File.Delete(tmp_image_path); return img; } } } }
Knockout () { Surface s = new ImageSurface (Format.ARGB32, 400, 400); Context ctx = new Context (s); Draw (ctx, 400, 400); s.WriteToPng ("knockout.png"); }
static BitmapContent CreateBitmapContent( double bitmapWidth, double bitmapHeight, string fontName, FontSlant fontSlant, FontWeight fontWeight, double fontSize, List<CharacterData> cds, ParsedPath pngFile) { using (ImageSurface surface = new ImageSurface(Format.Argb32, (int)bitmapWidth, (int)bitmapHeight)) { using (Context g = new Context(surface)) { SetupContext(g, fontName, fontSlant, fontWeight, fontSize); double x = 0; for (int i = 0; i < cds.Count; i++) { CharacterData cd = cds[i]; if (cd.Location.Width == 0) continue; g.MoveTo(x + cd.Bearing.X, cd.Bearing.Y); g.ShowText(cd.Character); #if DEBUG g.Save(); g.Color = new Color(1.0, 0, 0, 0.5); g.Antialias = Antialias.None; g.LineWidth = 1; g.MoveTo(x + 0.5, 0.5); g.LineTo(x + cd.Location.Width - 0.5, 0); g.LineTo(x + cd.Location.Width - 0.5, cd.Location.Height - 0.5); g.LineTo(x + 0.5, cd.Location.Height - 0.5); g.LineTo(x + 0.5, 0.5); g.Stroke(); g.Restore(); #endif x += cd.Location.Width; } g.Restore(); } if (pngFile != null) surface.WriteToPng(pngFile); return new BitmapContent(SurfaceFormat.Color, surface.Width, surface.Height, surface.Data); } }
public static void CombinePngs(List<ImagePlacement> placements, ParsedPath pngPath) { try { int w = 0; int h = 0; foreach (var placement in placements) { int wt = (int)placement.TargetRectangle.Width; int ht = (int)placement.TargetRectangle.Height; if (wt > w) w = wt; if (ht > h) h = ht; } using (ImageSurface combinedImage = new ImageSurface(Format.Argb32, w, h)) { using (Cairo.Context g = new Cairo.Context(combinedImage)) { foreach (var placement in placements) { using (ImageSurface image = new ImageSurface(placement.ImageFile)) { int x = (int)placement.TargetRectangle.X; int y = (int)placement.TargetRectangle.Y; g.SetSourceSurface(image, x, y); g.Paint(); } } } combinedImage.WriteToPng(pngPath); } } catch (Exception e) { throw new ArgumentException("Unable to combine images into file '{0}'".CultureFormat(pngPath), e); } }
/// <summary> /// Exports the specified <see cref="PlotModel" /> to the specified <see cref="Stream" />. /// </summary> /// <param name="model">The model.</param> /// <param name="stream">The output stream.</param> public void Export(IPlotModel model, Stream stream) { using (var bm = new ImageSurface(Format.ARGB32, this.Width, this.Height)) { using (var g = new Context(bm)) { if (this.Background.IsVisible()) { g.Save(); using (var pattern = new SolidPattern(this.Background.R, this.Background.G, this.Background.B, this.Background.A)) { g.SetSource(pattern); g.Rectangle(0, 0, this.Width, this.Height); g.Fill(); } g.Restore(); } var rc = new GraphicsRenderContext { RendersToScreen = false }; rc.SetGraphicsTarget(g); model.Update(true); model.Render(rc, this.Width, this.Height); // write to a temporary file var tmp = Guid.NewGuid() + ".png"; bm.WriteToPng(tmp); var bytes = File.ReadAllBytes(tmp); // write to the stream stream.Write(bytes, 0, bytes.Length); // delete the temporary file File.Delete(tmp); } } }
public static void GenerateGraph (List <ResultDbEntry> resultList, string filename) { // FIXME Exception if more than 50 results... ImageSurface surface = new ImageSurface (Format.Rgb24, 103, 52); Context context = new Context (surface); // Fill with grad LinearGradient grad = new LinearGradient (0.0, 0.0, 0.0, 52.0); grad.AddColorStopRgb (0.0, new Color (1.0, 1.0, 1.0)); grad.AddColorStopRgb (1.0, new Color (0.8, 0.8, 0.9)); context.Pattern = grad; context.Paint (); // Frame context.SetSourceRGB (0, 0, 0); context.LineWidth = 1.0; context.Rectangle (0.5, 0.5, 102.0, 51.0); context.Stroke (); long denominator = (long) (FindBiggestResult (resultList) * 1.2); context.LineWidth = 1.5; // FIXME Reverse to copy resultList.Reverse (); double x = 100.5 - ((resultList.Count - 1) * 2.0); bool hasPrevResult = false; long prevResult = 0; foreach (ResultDbEntry entry in resultList) { if (entry.Failure) { x += 2.0; continue; } double sz = ((double) entry.Time / denominator) * 50.0; if (hasPrevResult && UtilFu.GetValueDifference (prevResult, entry.Time) > 0.1) context.SetSourceRGB (1.0, 0.0, 0.0); else if (hasPrevResult && UtilFu.GetValueDifference (prevResult, entry.Time) < -0.1) context.SetSourceRGB (0.0, 1.0, 0.0); else context.SetSourceRGB (0.4, 0.4, 0.4); context.MoveTo (x, 51); context.LineTo (x, 51 - sz); context.Stroke (); x += 2.0; hasPrevResult = true; prevResult = entry.Time; } surface.WriteToPng (filename); resultList.Reverse (); ((IDisposable) context).Dispose (); ((IDisposable) surface).Dispose (); }