From 14d296240f4be5299086736d8785699468010104 Mon Sep 17 00:00:00 2001 From: zwcloud Date: Mon, 17 Oct 2016 13:04:01 +0800 Subject: [PATCH] Add save-as-png function for Windows/CairoSamples. --- Demo/Windows/CairoSamples/Form1.Designer.cs | 40 +++++++++++++-------- Demo/Windows/CairoSamples/Form1.cs | 36 +++++++++++++++++++ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/Demo/Windows/CairoSamples/Form1.Designer.cs b/Demo/Windows/CairoSamples/Form1.Designer.cs index 94c0897..1381d60 100644 --- a/Demo/Windows/CairoSamples/Form1.Designer.cs +++ b/Demo/Windows/CairoSamples/Form1.Designer.cs @@ -49,16 +49,18 @@ private void InitializeComponent() this.textToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.textAlignCenterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.textExtentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveAsPngToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.selectASampleToolStripMenuItem}); + this.selectASampleToolStripMenuItem, + this.saveAsPngToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(574, 24); + this.menuStrip1.Size = new System.Drawing.Size(259, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // @@ -91,84 +93,84 @@ private void InitializeComponent() // arcToolStripMenuItem // this.arcToolStripMenuItem.Name = "arcToolStripMenuItem"; - this.arcToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.arcToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.arcToolStripMenuItem.Text = "arc"; this.arcToolStripMenuItem.Click += new System.EventHandler(this.arcToolStripMenuItem_Click); // // arcNegativeToolStripMenuItem // this.arcNegativeToolStripMenuItem.Name = "arcNegativeToolStripMenuItem"; - this.arcNegativeToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.arcNegativeToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.arcNegativeToolStripMenuItem.Text = "arc negative"; this.arcNegativeToolStripMenuItem.Click += new System.EventHandler(this.arcNegativeToolStripMenuItem_Click); // // clipToolStripMenuItem // this.clipToolStripMenuItem.Name = "clipToolStripMenuItem"; - this.clipToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.clipToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.clipToolStripMenuItem.Text = "clip"; this.clipToolStripMenuItem.Click += new System.EventHandler(this.clipToolStripMenuItem_Click); // // clipImageToolStripMenuItem // this.clipImageToolStripMenuItem.Name = "clipImageToolStripMenuItem"; - this.clipImageToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.clipImageToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.clipImageToolStripMenuItem.Text = "clip image"; this.clipImageToolStripMenuItem.Click += new System.EventHandler(this.clipImageToolStripMenuItem_Click); // // curveRectangleToolStripMenuItem // this.curveRectangleToolStripMenuItem.Name = "curveRectangleToolStripMenuItem"; - this.curveRectangleToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.curveRectangleToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.curveRectangleToolStripMenuItem.Text = "curve rectangle"; this.curveRectangleToolStripMenuItem.Click += new System.EventHandler(this.curveRectangleToolStripMenuItem_Click); // // curveToToolStripMenuItem // this.curveToToolStripMenuItem.Name = "curveToToolStripMenuItem"; - this.curveToToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.curveToToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.curveToToolStripMenuItem.Text = "curve to"; this.curveToToolStripMenuItem.Click += new System.EventHandler(this.curveToToolStripMenuItem_Click); // // dashToolStripMenuItem // this.dashToolStripMenuItem.Name = "dashToolStripMenuItem"; - this.dashToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.dashToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.dashToolStripMenuItem.Text = "dash"; this.dashToolStripMenuItem.Click += new System.EventHandler(this.dashToolStripMenuItem_Click); // // fillAndStroke2ToolStripMenuItem // this.fillAndStroke2ToolStripMenuItem.Name = "fillAndStroke2ToolStripMenuItem"; - this.fillAndStroke2ToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.fillAndStroke2ToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.fillAndStroke2ToolStripMenuItem.Text = "fill and stroke2"; this.fillAndStroke2ToolStripMenuItem.Click += new System.EventHandler(this.fillAndStroke2ToolStripMenuItem_Click); // // fillStyleToolStripMenuItem // this.fillStyleToolStripMenuItem.Name = "fillStyleToolStripMenuItem"; - this.fillStyleToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.fillStyleToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.fillStyleToolStripMenuItem.Text = "fill style"; this.fillStyleToolStripMenuItem.Click += new System.EventHandler(this.fillStyleToolStripMenuItem_Click); // // gradientToolStripMenuItem // this.gradientToolStripMenuItem.Name = "gradientToolStripMenuItem"; - this.gradientToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.gradientToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.gradientToolStripMenuItem.Text = "gradient"; this.gradientToolStripMenuItem.Click += new System.EventHandler(this.gradientToolStripMenuItem_Click); // // imageToolStripMenuItem // this.imageToolStripMenuItem.Name = "imageToolStripMenuItem"; - this.imageToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.imageToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.imageToolStripMenuItem.Text = "image"; this.imageToolStripMenuItem.Click += new System.EventHandler(this.imageToolStripMenuItem_Click); // // imagepatternToolStripMenuItem // this.imagepatternToolStripMenuItem.Name = "imagepatternToolStripMenuItem"; - this.imagepatternToolStripMenuItem.Size = new System.Drawing.Size(155, 22); + this.imagepatternToolStripMenuItem.Size = new System.Drawing.Size(178, 22); this.imagepatternToolStripMenuItem.Text = "imagepattern"; this.imagepatternToolStripMenuItem.Click += new System.EventHandler(this.imagepatternToolStripMenuItem_Click); // @@ -221,11 +223,18 @@ private void InitializeComponent() this.textExtentsToolStripMenuItem.Text = "text extents"; this.textExtentsToolStripMenuItem.Click += new System.EventHandler(this.textExtentsToolStripMenuItem_Click); // + // saveAsPngToolStripMenuItem + // + this.saveAsPngToolStripMenuItem.Name = "saveAsPngToolStripMenuItem"; + this.saveAsPngToolStripMenuItem.Size = new System.Drawing.Size(81, 20); + this.saveAsPngToolStripMenuItem.Text = "Save as png"; + this.saveAsPngToolStripMenuItem.Click += new System.EventHandler(this.saveAsPngToolStripMenuItem_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(574, 405); + this.ClientSize = new System.Drawing.Size(259, 266); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; @@ -260,6 +269,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem textToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem textAlignCenterToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem textExtentsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveAsPngToolStripMenuItem; } } diff --git a/Demo/Windows/CairoSamples/Form1.cs b/Demo/Windows/CairoSamples/Form1.cs index 6558776..af7c8d8 100644 --- a/Demo/Windows/CairoSamples/Form1.cs +++ b/Demo/Windows/CairoSamples/Form1.cs @@ -13,6 +13,7 @@ public partial class Form1 : Form public Win32Surface Surface1 { get; private set; } private readonly byte[] romedalenPngData; + private string lastSelected = "none"; public Form1() { @@ -40,6 +41,7 @@ protected override void OnPaint(PaintEventArgs e) private void arcToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "arc"; OnPaintAction = cr => { double xc = 128.0; @@ -71,6 +73,7 @@ private void arcToolStripMenuItem_Click(object sender, EventArgs e) private void arcNegativeToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "arcNegative"; OnPaintAction = cr => { double xc = 128.0; @@ -102,6 +105,7 @@ private void arcNegativeToolStripMenuItem_Click(object sender, EventArgs e) private void clipToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "clip"; OnPaintAction = cr => { cr.Arc(128.0, 128.0, 76.8, 0, 2 * Math.PI); @@ -124,6 +128,7 @@ private void clipToolStripMenuItem_Click(object sender, EventArgs e) private void clipImageToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "clipImage"; OnPaintAction = cr => { int w, h; @@ -150,6 +155,7 @@ private void clipImageToolStripMenuItem_Click(object sender, EventArgs e) private void curveRectangleToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "curveRectangle"; OnPaintAction = cr => { /* a custom shape that could be wrapped in a function */ @@ -222,6 +228,7 @@ private void curveRectangleToolStripMenuItem_Click(object sender, EventArgs e) private void curveToToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "curveTo"; OnPaintAction = cr => { double x = 25.6, y = 128.0; @@ -247,6 +254,7 @@ private void curveToToolStripMenuItem_Click(object sender, EventArgs e) private void dashToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "dash"; OnPaintAction = cr => { double[] dashes = @@ -274,6 +282,7 @@ private void dashToolStripMenuItem_Click(object sender, EventArgs e) private void fillAndStroke2ToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "fillAndStroke2"; OnPaintAction = cr => { cr.MoveTo(128.0, 25.6); @@ -300,6 +309,7 @@ private void fillAndStroke2ToolStripMenuItem_Click(object sender, EventArgs e) private void fillStyleToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "fillStyle"; OnPaintAction = cr => { cr.LineWidth = 6; @@ -327,6 +337,7 @@ private void fillStyleToolStripMenuItem_Click(object sender, EventArgs e) private void gradientToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "gradient"; OnPaintAction = cr => { Gradient pat = new LinearGradient(0.0, 0.0, 0.0, 256.0); @@ -352,6 +363,7 @@ private void gradientToolStripMenuItem_Click(object sender, EventArgs e) private void imageToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "image"; OnPaintAction = cr => { int w, h; @@ -376,6 +388,7 @@ private void imageToolStripMenuItem_Click(object sender, EventArgs e) private void imagepatternToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "imagepattern"; OnPaintAction = cr => { int w, h; @@ -413,6 +426,7 @@ private void imagepatternToolStripMenuItem_Click(object sender, EventArgs e) private void multiSegmentCapsToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "multiSegmentCaps"; OnPaintAction = cr => { cr.MoveTo(50.0, 75.0); @@ -434,6 +448,7 @@ private void multiSegmentCapsToolStripMenuItem_Click(object sender, EventArgs e) private void roundedRectangleToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "roundedRectangle"; OnPaintAction = cr => { /* a custom shape that could be wrapped in a function */ @@ -466,6 +481,7 @@ private void roundedRectangleToolStripMenuItem_Click(object sender, EventArgs e) private void setLineCapToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "setLineCap"; OnPaintAction = cr => { cr.LineWidth = 30.0; @@ -493,6 +509,7 @@ private void setLineCapToolStripMenuItem_Click(object sender, EventArgs e) private void setLineJoinToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "setLineJoin"; OnPaintAction = cr => { cr.LineWidth = 40.96; @@ -520,6 +537,7 @@ private void setLineJoinToolStripMenuItem_Click(object sender, EventArgs e) private void textToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "text"; OnPaintAction = cr => { cr.SelectFontFace("Microsoft Sans Serif", FontSlant.Normal, FontWeight.Bold); @@ -549,6 +567,7 @@ private void textToolStripMenuItem_Click(object sender, EventArgs e) private void textAlignCenterToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "textAlignCenter"; OnPaintAction = cr => { string text = "cairo"; @@ -582,6 +601,7 @@ private void textAlignCenterToolStripMenuItem_Click(object sender, EventArgs e) private void textExtentsToolStripMenuItem_Click(object sender, EventArgs e) { + lastSelected = "textExtents"; OnPaintAction = cr => { string text = "cairo"; @@ -613,5 +633,21 @@ private void textExtentsToolStripMenuItem_Click(object sender, EventArgs e) Invalidate(); } + + private void saveAsPngToolStripMenuItem_Click(object sender, EventArgs e) + { + SaveFileDialog dialog = new SaveFileDialog(); + dialog.Filter = "PNG|*.png"; + dialog.FileName = lastSelected; + if (dialog.ShowDialog() == DialogResult.OK) + { + using (this.Graphics1 = this.CreateGraphics()) + using (Surface1 = new Win32Surface(Graphics1.GetHdc())) + using (Context1 = new Context(Surface1)) + { + Surface1.WriteToPng(dialog.FileName); + } + } + } } }